1.安装java,假如没有安装,参考https://www.cnblogs.com/SakerLiu/p/9742259.html
2.安装maven
sudo apt-get install maven
3.安装Spring CLI,从https://repo.spring.io/milestone/org/springframework/boot/spring-boot-cli/下载想要的版本
将解压完成的后的文件重命名为spring,并移动到/usr/local/下,添加到PATH
export SPRING_HOME=/usr/local/spring export PATH=${SPRING_HOME}/bin:$PATH
输入spring --version验证,输出如下内容
Spring CLI v2.1.0.M3
4.测试Spring CLI
创建test.groovy,内容如下
@RestController class ThisWillActuallyRun { @RequestMapping("/") String home() { "Hello World!" } }
执行下述代码
spring run test.groovy
访问localhost:8080即可发现hello world的输出
####################################################
创建一个Spring boot 应用
1.创建一个父pom.xml文件,方便以后开发直接继承
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.M3</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- Add Spring repositories --> <!-- (you don‘t need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </repository> </repositories><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
2.输入下面代码,解决依赖
mvn dependency:tree
3.在pom.xml文件目录下,输入下面的指令创建文件夹
mdkri -p src/main/java
在java文件夹下创建Example.java文件,内容如下
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
4.执行mvn spring-boot:run并打开localhost:8080,输出hello world!
5.添加如下依赖,创建jar文件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
6.执行mvn package,解决包依赖
7.在target文件夹下,存在一个myproject-0.0.1-SNAPSHOT.jar 文件,执行
java -jar target/myproject-0.0.1-SNAPSHOT.jar
便可以和上述一样访问网页
假如想查看里面的东西,执行下面的命令
jar tvf target/myproject-0.0.1-SNAPSHOT.jar
就会发现出现一个压缩文件myproject-0.0.1-SNAPSHOT.jar.original,打开查看即可
原文地址:https://www.cnblogs.com/SakerLiu/p/9742574.html