本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名原文链接~~~

命令行构建并运行一个SpringBoot应用

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:


1、安装 JDK 8 和 Maven,并添加它们的可执行文件到环境变量,效果如下:

$ mvn -version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /Users/zhangzhongen/dev/maven/apache-maven-3.6.1
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"

2、编辑 Maven 的 settings.xml 配置 Maven 使用阿里源:

  <mirrors>
    <mirror>
        <id>alimaven</id>
       <name>aliyun maven</name>
       <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
       <mirrorOf>central</mirrorOf>        
    </mirror>
  </mirrors>

3、创建项目:

$ mvn archetype:generate -DgroupId=thinking-in-spring-boot -DartifactId=first-spring-boot-application -Dversion=1.0.0-SNAPSHOT -DinteractiveMode=false -Dpackage=thinking.in.spring.boot

执行过程中会下载很多依赖,完成后生成目录结构如下:

├── first-spring-boot-application.iml
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── thinking
│   │           └── in
│   │               └── spring
│   │                   └── boot
│   │                       └── App.java
│   └── test
│       └── java
│           └── thinking
│               └── in
│                   └── spring
│                       └── boot
│                           └── AppTest.java
└── target

4、修改 pom.xml 配置增加 SpringBoot 依赖与插件声明:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>thinking-in-spring-boot</groupId>
  <artifactId>first-spring-boot-application</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0-SNAPSHOT</version>
  <name>《Spring Boot 编程思想》第一个 Spring Boot 应用</name>
  <url>http://maven.apache.org</url>
  <parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.0.2.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

5、调整引导类 App.java 内容如下:

package thinking.in.spring.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello world!
 */
@RestController
@SpringBootApplication
public class App {
    @RequestMapping("/")
    public String index() {
        return "Welcome , my Buddy!!!";
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

6、检查依赖信息:

$ mvn dependency:tree -Dincludes=org.springframework*
[INFO] thinking-in-spring-boot:first-spring-boot-application:jar:1.0.0-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.0.2.RELEASE:compile
[INFO]    +- org.springframework.boot:spring-boot-starter:jar:2.0.2.RELEASE:compile
[INFO]    |  +- org.springframework.boot:spring-boot:jar:2.0.2.RELEASE:compile
[INFO]    |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.2.RELEASE:compile
[INFO]    |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.2.RELEASE:compile
[INFO]    |  \- org.springframework:spring-core:jar:5.0.6.RELEASE:compile
[INFO]    |     \- org.springframework:spring-jcl:jar:5.0.6.RELEASE:compile
[INFO]    +- org.springframework.boot:spring-boot-starter-json:jar:2.0.2.RELEASE:compile
[INFO]    +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.2.RELEASE:compile
[INFO]    +- org.springframework:spring-web:jar:5.0.6.RELEASE:compile
[INFO]    |  \- org.springframework:spring-beans:jar:5.0.6.RELEASE:compile
[INFO]    \- org.springframework:spring-webmvc:jar:5.0.6.RELEASE:compile
[INFO]       +- org.springframework:spring-aop:jar:5.0.6.RELEASE:compile
[INFO]       +- org.springframework:spring-context:jar:5.0.6.RELEASE:compile
[INFO]       \- org.springframework:spring-expression:jar:5.0.6.RELEASE:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.486 s
[INFO] Finished at: 2020-11-01T23:11:40+08:00
[INFO] ------------------------------------------------------------------------

7、运行项目:

$ mvn spring-boot:run
...
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.2.RELEASE)
 ...
 2020-11-01 23:12:50.524  INFO 92171 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-01 23:12:50.542  INFO 92171 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-01 23:12:50.542  INFO 92171 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
...

8、测试访问:

$ curl 127.0.0.1:8080
Welcome , my Buddy!!!

9、在 pom.xml 添加 spring-boot-maven-plugin 插件(用于打包可执行 JAR):

<project>
   ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

10、编译生成 JAR 包:

$ mvn package
$ ls target/*.jar
target/first-spring-boot-application-1.0.0-SNAPSHOT.jar

11、停止前面通过 mvn spring-boot:run 启动的应用,运行 JAR 包以启动 Spring Boot 应用:

$ java -jar target/first-spring-boot-application-1.0.0-SNAPSHOT.jar

12、测试访问:

$ curl 127.0.0.1:8080
Welcome , my Buddy!!!

如果这篇文章对您有帮助,可点击下方链接分享给你的朋友们😋,如果遇到问题欢迎评论、留言~~~😇

评论

公众号:zze_coding

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×