Spring boot 的 maven 打包过程

发布于:2025-06-15 ⋅ 阅读:(19) ⋅ 点赞:(0)

Java的 Spring boot 项目,要上线必须经过打包过程。

在 maven 项目中,打包是 package 。

首先,编译 compile 一次;然后运行打包。

[INFO] --- jar:3.4.2:jar (default-jar)

[INFO] --- spring-boot:3.5.0:repackage (repackage) 

打包需要 

在 <build> 标签进行,

  <build>
    <plugins>
<!--   打包   -->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>3.5.0</version>
      </plugin>
    </plugins>
  </build>

target 文件夹下,

news-1.0-SNAPSHOT.jar 

这个文件就是打好的 jar 包。

运行 Jar 包

 java -jar news-1.0-SNAPSHOT.jar

 java -jar news-1.0-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.5.0)

2025-06-13T05:15:12.445-04:00  INFO 10325 --- [           main] biz.baijing.NewsApplication              : Starting NewsApplication v1.0-SNAPSHOT using Java 21.0.7 with PID 10325 (//news/target)
2025-06-13T05:15:12.448-04:00  INFO 10325 --- [           main] biz.baijing.NewsApplication              : No active profile set, falling back to 1 default profile: "default"
2025-06-13T05:15:13.551-04:00  INFO 10325 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2025-06-13T05:15:13.555-04:00  INFO 10325 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2025-06-13T05:15:13.599-04:00  INFO 10325 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 19 ms. Found 0 Redis repository interfaces.
2025-06-13T05:15:14.376-04:00  INFO 10325 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-06-13T05:15:14.400-04:00  INFO 10325 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-06-13T05:15:14.401-04:00  INFO 10325 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.41]
2025-06-13T05:15:14.458-04:00  INFO 10325 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-06-13T05:15:14.459-04:00  INFO 10325 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1956 ms
2025-06-13T05:15:15.422-04:00  INFO 10325 --- [           main] biz.baijing.config.WebConfigure          : webConfigeure 登录与否 biz.baijing.interceptors.LoginInterceptor@9f9146d
2025-06-13T05:15:16.008-04:00  INFO 10325 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-06-13T05:15:16.054-04:00  INFO 10325 --- [           main] biz.baijing.NewsApplication              : Started NewsApplication in 4.288 seconds (process running for 5.251)

运行结果。 

部署的时候调整端口等配置项:

 java -jar news-1.0-SNAPSHOT.jar --server -port=9909

还,可以通过环境变量配置相关事项。

更完善的配置:

配置文件,在 jar 包目录提供一个 yml 的配置文件。