天天看点

SpringBoot打包可执行文件SpringBoot打包可执行文件

SpringBoot打包可执行文件

1、点击clean

clean其实是清理之前已经打过的包,根据clean的日志我们发现一个路径,我们进去之后发现这个target是不存在的,因为已经被clean指令给删除了。

SpringBoot打包可执行文件SpringBoot打包可执行文件

2、点击package

SpringBoot打包可执行文件SpringBoot打包可执行文件

我们需要注意一下这个日志,里面有一段很重要的就是其中的test,仔细观察发现我们的测试类是全部执行一遍的。

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------< com:crud >------------------------------
[INFO] Building crud 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ crud ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 3 resources
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ crud ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 21 source files to /Users/zc/IdeaProjects/crud/target/classes
[WARNING] /Users/zc/IdeaProjects/crud/src/main/java/com/crud/utils/RedisUtils.java:[3,32] com.sun.corba.se.spi.ior.ObjectKey是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/zc/IdeaProjects/crud/src/main/java/com/crud/utils/RedisUtils.java:[3,32] com.sun.corba.se.spi.ior.ObjectKey是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/zc/IdeaProjects/crud/src/main/java/com/crud/utils/RedisUtils.java:[3,32] com.sun.corba.se.spi.ior.ObjectKey是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/zc/IdeaProjects/crud/src/main/java/com/crud/utils/RedisUtils.java:[3,32] com.sun.corba.se.spi.ior.ObjectKey是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/zc/IdeaProjects/crud/src/main/java/com/crud/utils/RedisUtils.java:[3,32] com.sun.corba.se.spi.ior.ObjectKey是内部专用 API, 可能会在未来发行版中删除
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ crud ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/zc/IdeaProjects/crud/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ crud ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to /Users/zc/IdeaProjects/crud/target/test-classes
[INFO] /Users/zc/IdeaProjects/crud/src/test/java/com/crud/bussiness/UserAndBlogInjectMockBeanTest.java: 某些输入文件使用或覆盖了已过时的 API。
[INFO] /Users/zc/IdeaProjects/crud/src/test/java/com/crud/bussiness/UserAndBlogInjectMockBeanTest.java: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
[INFO] /Users/zc/IdeaProjects/crud/src/test/java/com/crud/controller/RedisOneTest.java: /Users/zc/IdeaProjects/crud/src/test/java/com/crud/controller/RedisOneTest.java使用了未经检查或不安全的操作。
[INFO] /Users/zc/IdeaProjects/crud/src/test/java/com/crud/controller/RedisOneTest.java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ crud ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
16:28:41.791 [main] INFO  org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.crud.bussiness.UserAndBlogInjectMockBeanTest], using SpringBootContextLoader
16:28:41.801 [main] INFO  org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.crud.bussiness.UserAndBlogInjectMockBeanTest]: no resource found for suffixes {-context.xml, Context.groovy}.
16:28:41.802 [main] INFO  org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.crud.bussiness.UserAndBlogInjectMockBeanTest]: UserAndBlogInjectMockBeanTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
16:28:41.962 [main] INFO  org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.crud.CrudApplication for test class com.crud.bussiness.UserAndBlogInjectMockBeanTest
16:28:42.080 [main] INFO  org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@3688eb5b, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@69f1a286, org.springframework.test.context.event.ApplicationEv[email protected], org.springframework.test.context.support.DependencyInjectionTestExecutionListener@457c9034, org.springframework.test.context.support.DirtiesContextTestExecutionListener@345f69f3, org.springframework.test.context.event.EventPublishingTestExecutionListener@50de186c]
16:28:42.097 [main] INFO  org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.crud.bussiness.UserAndBlogTest], using SpringBootContextLoader
16:28:42.098 [main] INFO  org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.crud.bussiness.UserAndBlogTest]: no resource found for suffixes {-context.xml, Context.groovy}.
16:28:42.099 [main] INFO  org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.crud.bussiness.UserAndBlogTest]: UserAndBlogTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
16:28:42.101 [main] INFO  org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.crud.CrudApplication for test class com.crud.bussiness.UserAndBlogTest
16:28:42.104 [main] INFO  org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@413f69cc, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1f53a5dc, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@1b75c2e3, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1984b1f, org.springframework.test.context.support.DirtiesContextTestExecutionListener@3bd323e9, org.springframework.test.context.event.EventPublishingTestExecutionListener@39ac0c0a]
16:28:42.123 [main] INFO  org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.crud.controller.RedisOneTest], using SpringBootContextLoader
16:28:42.123 [main] INFO  org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.crud.controller.RedisOneTest]: no resource found for suffixes {-context.xml, Context.groovy}.
16:28:42.124 [main] INFO  org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.crud.controller.RedisOneTest]: RedisOneTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
16:28:42.143 [main] INFO  org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.crud.CrudApplication for test class com.crud.controller.RedisOneTest
16:28:42.145 [main] INFO  org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@e383572, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@5ddf0d24, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@363a52f, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@60856961, org.springframework.test.context.support.DirtiesContextTestExecutionListener@2fd953a6, org.springframework.test.context.event.EventPublishingTestExecutionListener@a4add54]
16:28:42.187 [main] ERROR com.crud.controller.Log4jTest - 2
16:28:42.188 [main] INFO  com.crud.controller.Log4jTest - 3
16:28:42.188 [main] WARN  com.crud.controller.Log4jTest - 4

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.0)

16:28:57.583 [background-preinit] INFO  org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 6.2.0.Final
16:29:02.608 [main] WARN  org.springframework.boot.StartupInfoLogger - InetAddress.getLocalHost().getHostName() took 5002 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
16:28:57.604 [main] INFO  com.crud.bussiness.UserAndBlogInjectMockBeanTest - Starting UserAndBlogInjectMockBeanTest using Java 1.8.0_291 on ZHR.local with PID 76813 (/Users/zc/IdeaProjects/crud/target/test-classes started by zc in /Users/zc/IdeaProjects/crud)
16:29:07.617 [main] INFO  com.crud.bussiness.UserAndBlogInjectMockBeanTest - The following profiles are active: test02
16:29:08.365 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode!
16:29:08.369 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data Redis repositories in DEFAULT mode.
16:29:08.393 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 10 ms. Found 0 Redis repository interfaces.
16:29:28.951 [main] INFO  com.crud.bussiness.UserAndBlogInjectMockBeanTest - Started UserAndBlogInjectMockBeanTest in 46.716 seconds (JVM running for 53.094)
16:29:28.953 [main] INFO  org.springframework.boot.availability.ApplicationAvailabilityBean - Application availability state LivenessState changed to CORRECT
16:29:28.955 [main] INFO  org.springframework.boot.availability.ApplicationAvailabilityBean - Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
hello mock
16:29:29.667 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
16:29:30.026 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
[Blog(id=8, user_id=5, title=文章一8, description=第一篇文章, content=第一篇文章第一篇文章第一篇文章, created=2021-06-18 13:49:04, status=0)]
16:29:33.368 [NettyClientSelector_1] INFO  RocketmqRemoting - closeChannel: close the connection to remote address[] result: true
16:29:33.368 [NettyClientSelector_1] INFO  RocketmqRemoting - closeChannel: close the connection to remote address[] result: true
userone:User(id=999, username=zhazha, avatar=www.baidu.com, [email protected], password=caicai, status=0, created=2021-06-18 10:36:42, last_login=2021-06-18 10:36:42)
[Blog(id=8, user_id=5, title=文章一8, description=第一篇文章, content=第一篇文章第一篇文章第一篇文章, created=2021-06-18 13:49:04, status=0)]
Before Class
[Blog(id=999, user_id=89, title=文章999, description=mock, content=mockmock, created=2021-06-17 17:36:11, status=0)]
16:29:58.290 [NettyClientSelector_1] INFO  RocketmqRemoting - closeChannel: close the connection to remote address[] result: true
16:29:58.295 [NettyClientSelector_1] INFO  RocketmqRemoting - closeChannel: close the connection to remote address[] result: true
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 97.73 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ crud ---
[INFO] Building jar: /Users/zc/IdeaProjects/crud/target/crud-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.5.0:repackage (repackage) @ crud ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:50 min
[INFO] Finished at: 2021-07-16T16:30:15+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0
           

3、启动jar包

我们进入到target目录下,发现有一个jar包,java -jar命令启动,稍等一下就看到了spring启动的信息,启动完成之后就可以调用接口了

ZHR:target zc$ pwd
/Users/zc/IdeaProjects/crud/target
ZHR:target zc$ ls
classes					maven-archiver
crud-0.0.1-SNAPSHOT.jar			maven-status
crud-0.0.1-SNAPSHOT.jar.original	surefire-reports
generated-sources			test-classes
generated-test-sources
ZHR:target zc$ java -jar crud-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.0)

15:30:06.543 [background-preinit] INFO  org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 6.2.0.Final
15:30:11.631 [main] WARN  org.springframework.boot.StartupInfoLogger - InetAddress.getLocalHost().getHostName() took 5012 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
15:30:06.617 [main] INFO  com.crud.CrudApplication - Starting CrudApplication v0.0.1-SNAPSHOT using Java 1.8.0_291 on ZHR.local with PID 76512 (/Users/zc/IdeaProjects/crud/target/crud-0.0.1-SNAPSHOT.jar started by zc in /Users/zc/IdeaProjects/crud/target)
15:30:16.642 [main] INFO  com.crud.CrudApplication - The following profiles are active: test02
15:30:17.427 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode!
15:30:17.431 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data Redis repositories in DEFAULT mode.
15:30:17.457 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 6 ms. Found 0 Redis repository interfaces.
15:30:18.544 [main] INFO  org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8082 (http)
15:30:18.557 [main] INFO  org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8082"]
15:30:18.557 [main] INFO  org.apache.catalina.core.StandardService - Starting service [Tomcat]
15:30:18.557 [main] INFO  org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.46]
15:30:18.629 [main] INFO  org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
15:30:18.634 [main] INFO  org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 1938 ms
15:30:37.149 [main] INFO  org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8082"]
15:30:37.175 [main] INFO  org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8082 (http) with context path ''
15:30:37.186 [main] INFO  com.crud.CrudApplication - Started CrudApplication in 46.057 seconds (JVM running for 52.402)
15:30:37.187 [main] INFO  org.springframework.boot.availability.ApplicationAvailabilityBean - Application availability state LivenessState changed to CORRECT
15:30:37.189 [main] INFO  org.springframework.boot.availability.ApplicationAvailabilityBean - Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
15:30:41.967 [NettyClientSelector_1] INFO  RocketmqRemoting - closeChannel: close the connection to remote address[] result: true
15:30:41.967 [NettyClientSelector_1] INFO  RocketmqRemoting - closeChannel: close the connection to remote address[] result: true
15:31:06.756 [NettyClientSelector_1] INFO  RocketmqRemoting - closeChannel: close the connection to remote address[] result: true
15:31:06.764 [NettyClientSelector_1] INFO  RocketmqRemoting - closeChannel: close the connection to remote address[] result: true
15:31:09.059 [http-nio-8082-exec-1] INFO  org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring DispatcherServlet 'dispatcherServlet'
15:31:09.060 [http-nio-8082-exec-1] INFO  org.springframework.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
15:31:09.061 [http-nio-8082-exec-1] INFO  org.springframework.web.servlet.DispatcherServlet - Completed initialization in 1 ms