1、前言
上一章 springboot 实战中已经解决了如何创建 maven 多模块,如何实现父模块与子模块之间的依赖集成以及不同子模块之间的相互调用。
这一章,我们来继续讲述一下 maven 多模块项目如何打包。
2、单模块打包
再聊多模块打包之前,先来谈谈单模块项目打包的流程,新建一个项目 packages,其目录和 pom 文件内容如下:
接下来对其进行打包。
2.1 打包流程
如上图所示,找到项目右侧 maven 框,找到要打包的模块 packages——》lifecycle——》clean——》package/install/deploy。
其中 clean 是清除项目缓存的,而 package,install 以及 deploy 都有打包的作用,不过略微有些区别:
- package 命令完成了项目编译、单元测试、打包功能,但没有把打好的可执行 jar 包(war 包或其它形式的包)布署到本地 maven 仓库和远程 maven 私服仓库
- install 命令完成了项目编译、单元测试、打包功能,同时把打好的可执行 jar 包(war 包或其它形式的包)布署到本地 maven 仓库,但没有布署到远程 maven 私服仓库
- deploy 命令完成了项目编译、单元测试、打包功能,同时把打好的可执行 jar 包(war 包或其它形式的包)布署到本地 maven 仓库和远程 maven 私服仓库
如果是单模块打包的话,用的最多的是 package,因为没有依赖,其他模块并不需要该模块的依赖包,所以也就不需要部署到本地 maven 仓库,更别说是远程 maven 私服仓库了。
2.2 clean 命令探究
OK,我们来看一下运行 clean 生成的代码:
本质的步骤,就是 maven 调用了一个 maven-clean-plugin 的工具,看名字应该是个插件,然后就打包完了。
2.3 package 命令探究
再运行一下 package,这个比较长就不贴图了,直接上代码:
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.lanya:packages >-------------------------
[INFO] Building packages 0.0.1-SNAPSHOT
//下面开始进入打包流程
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
//第一步:resources
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ packages ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
//第二步:compile
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ packages ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\ideaProject\packages\target\classes
[INFO]
//第三步:testResources
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ packages ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory E:\ideaProject\packages\src\test\resources
[INFO]
//第四步:testcompile
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ packages ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\ideaProject\packages\target\test-classes
[INFO]
//第五步:test
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ packages ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.lanya.packages.PackagesApplicationTests
10:13:00.455 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
10:13:00.463 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
10:13:00.489 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.lanya.packages.PackagesApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
10:13:00.497 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.lanya.packages.PackagesApplicationTests], using SpringBootContextLoader
10:13:00.500 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.lanya.packages.PackagesApplicationTests]: class path resource [com/lanya/packages/PackagesApplicationTests-context.xml] does not exist
10:13:00.500 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.lanya.packages.PackagesApplicationTests]: class path resource [com/lanya/packages/PackagesApplicationTestsContext.groovy] does not exist
10:13:00.500 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.lanya.packages.PackagesApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
10:13:00.501 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.lanya.packages.PackagesApplicationTests]: PackagesApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
10:13:00.528 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.lanya.packages.PackagesApplicationTests]
10:13:00.573 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [E:\ideaProject\packages\target\classes\com\lanya\packages\PackagesApplication.class]
10:13:00.574 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.lanya.packages.PackagesApplication for test class com.lanya.packages.PackagesApplicationTests
10:13:00.643 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.lanya.packages.PackagesApplicationTests]: using defaults.
10:13:00.643 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
10:13:00.658 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
10:13:00.658 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
10:13:00.658 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@4b741d6d, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@2eae8e6e, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@8f2ef19, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@470734c3, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@2cf3d63b, org.springframework.test.context.support.DirtiesContextTestExecutionListener@7674f035, org.springframework.test.context.event.EventPublishingTestExecutionListener@69e153c5, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@173ed316, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@25ce9dc4, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@74ea2410, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@17f62e33, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@76b1e9b8, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@27406a17]
10:13:00.661 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@74d1dc36 testClass = PackagesApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@7161d8d1 testClass = PackagesApplicationTests, locations = '{}', classes = '{class com.lanya.packages.PackagesApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@35ef1869, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2c767a52, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@43195e57, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@795cd85e, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@7c729a55, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@2ef5e5e3], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
10:13:00.684 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.5)
2021-04-26 10:13:00.845 INFO 26348 --- [ main] c.l.packages.PackagesApplicationTests : Starting PackagesApplicationTests using Java 1.8.0_152 on DESKTOP-VMPGGIA with PID 26348 (started by ShuoZhou in E:\ideaProject\packages)
2021-04-26 10:13:00.847 INFO 26348 --- [ main] c.l.packages.PackagesApplicationTests : No active profile set, falling back to default profiles: default
2021-04-26 10:13:01.517 INFO 26348 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-26 10:13:01.720 INFO 26348 --- [ main] c.l.packages.PackagesApplicationTests : Started PackagesApplicationTests in 1.038 seconds (JVM running for 1.659)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.422 s - in com.lanya.packages.PackagesApplicationTests
2021-04-26 10:13:01.877 INFO 26348 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
//第六步:jar包
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ packages ---
[INFO] Building jar: E:\ideaProject\packages\target\packages-0.0.1-SNAPSHOT.jar
[INFO]
第七步:repackage
[INFO] --- spring-boot-maven-plugin:2.4.5:repackage (repackage) @ packages ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
//最终运行结束
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.792 s
[INFO] Finished at: 2021-04-26T10:13:03+08:00
[INFO] ------------------------------------------------------------------------
仔细观察上面的代码,可以发现,其中关键步骤就那么几个:
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ packages ---
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ packages ---
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ packages ---
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ packages ---
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ packages ---
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ packages ---
[INFO] --- spring-boot-maven-plugin:2.4.5:repackage (repackage) @ packages ---
[INFO] BUILD SUCCESS
整个 package 步骤经历了:编译(resources,compile),单元测试(testResources,testCompile,test),打包(jar),打包后的清理替换(repackage)四个阶段。
OK,打包完后,依赖包所在目录如下:
2.4 install 命令探究
OK,我们再来看看 install 的打包过程:
D:\JDK\bin\java.exe -Dmaven.multiModuleProjectDirectory=E:\ideaProject\packages "-Dmaven.home=D:\IDEA\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=D:\IDEA\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=D:\IDEA\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\IDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=5046:D:\IDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IDEA\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar" org.codehaus.classworlds.Launcher -Didea.version2019.3.3 install
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.lanya:packages >-------------------------
[INFO] Building packages 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ packages ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ packages ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\ideaProject\packages\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ packages ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory E:\ideaProject\packages\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ packages ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\ideaProject\packages\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ packages ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.lanya.packages.PackagesApplicationTests
10:33:43.333 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
10:33:43.333 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
10:33:43.364 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.lanya.packages.PackagesApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
10:33:43.364 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.lanya.packages.PackagesApplicationTests], using SpringBootContextLoader
10:33:43.380 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.lanya.packages.PackagesApplicationTests]: class path resource [com/lanya/packages/PackagesApplicationTests-context.xml] does not exist
10:33:43.380 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.lanya.packages.PackagesApplicationTests]: class path resource [com/lanya/packages/PackagesApplicationTestsContext.groovy] does not exist
10:33:43.380 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.lanya.packages.PackagesApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
10:33:43.380 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.lanya.packages.PackagesApplicationTests]: PackagesApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
10:33:43.395 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.lanya.packages.PackagesApplicationTests]
10:33:43.442 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [E:\ideaProject\packages\target\classes\com\lanya\packages\PackagesApplication.class]
10:33:43.442 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.lanya.packages.PackagesApplication for test class com.lanya.packages.PackagesApplicationTests
10:33:43.520 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.lanya.packages.PackagesApplicationTests]: using defaults.
10:33:43.520 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
10:33:43.536 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
10:33:43.536 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
10:33:43.536 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@4b741d6d, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@2eae8e6e, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@8f2ef19, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@470734c3, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@2cf3d63b, org.springframework.test.context.support.DirtiesContextTestExecutionListener@7674f035, org.springframework.test.context.event.EventPublishingTestExecutionListener@69e153c5, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@173ed316, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@25ce9dc4, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@74ea2410, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@17f62e33, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@76b1e9b8, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@27406a17]
10:33:43.536 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@74d1dc36 testClass = PackagesApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@7161d8d1 testClass = PackagesApplicationTests, locations = '{}', classes = '{class com.lanya.packages.PackagesApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@35ef1869, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2c767a52, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@43195e57, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@795cd85e, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@7c729a55, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@2ef5e5e3], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
10:33:43.567 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.5)
2021-04-26 10:33:43.723 INFO 26124 --- [ main] c.l.packages.PackagesApplicationTests : Starting PackagesApplicationTests using Java 1.8.0_152 on DESKTOP-VMPGGIA with PID 26124 (started by ShuoZhou in E:\ideaProject\packages)
2021-04-26 10:33:43.723 INFO 26124 --- [ main] c.l.packages.PackagesApplicationTests : No active profile set, falling back to default profiles: default
2021-04-26 10:33:44.426 INFO 26124 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-26 10:33:44.629 INFO 26124 --- [ main] c.l.packages.PackagesApplicationTests : Started PackagesApplicationTests in 1.062 seconds (JVM running for 1.712)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.468 s - in com.lanya.packages.PackagesApplicationTests
2021-04-26 10:33:44.786 INFO 26124 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ packages ---
[INFO] Building jar: E:\ideaProject\packages\target\packages-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.5:repackage (repackage) @ packages ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
//上面步骤和package一致,下面这一步是多出来的install
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ packages ---
[INFO] Installing E:\ideaProject\packages\target\packages-0.0.1-SNAPSHOT.jar to C:\Users\ShuoZhou\.m2\repository\com\lanya\packages\0.0.1-SNAPSHOT\packages-0.0.1-SNAPSHOT.jar
[INFO] Installing E:\ideaProject\packages\pom.xml to C:\Users\ShuoZhou\.m2\repository\com\lanya\packages\0.0.1-SNAPSHOT\packages-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.979 s
[INFO] Finished at: 2021-04-26T10:33:46+08:00
[INFO] ------------------------------------------------------------------------
对比 package 的生成代码,install 只多了一个步骤:
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ packages ---
[INFO] Installing E:\ideaProject\packages\target\packages-0.0.1-SNAPSHOT.jar to C:\Users\ShuoZhou\.m2\repository\com\lanya\packages\0.0.1-SNAPSHOT\packages-0.0.1-SNAPSHOT.jar
[INFO] Installing E:\ideaProject\packages\pom.xml to C:\Users\ShuoZhou\.m2\repository\com\lanya\packages\0.0.1-SNAPSHOT\packages-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
这四行代码很简单,就是执行了 installing 操作,说白了就是复制操作,将刚刚打包好的 packages-0.0.1-SNAPSHOT.jar 依赖包和项目的 pom 文件,复制到本地的 maven 仓库中,我们来看一下复制生成的目录有哪些内容:
从目录上来看,并不是单纯的复制,因为还多了几个文件,和我们从 maven 服务器上的中央仓库拉取下来的依赖目录一致,看了使用 installing 十分贴切。
2.5 deploy 命令探究
我们再来看一下 deploy 执行代码:
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.lanya:packages >-------------------------
[INFO] Building packages 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ packages ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ packages ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\ideaProject\packages\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ packages ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory E:\ideaProject\packages\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ packages ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\ideaProject\packages\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ packages ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.lanya.packages.PackagesApplicationTests
10:40:37.200 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
10:40:37.200 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
10:40:37.231 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.lanya.packages.PackagesApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
10:40:37.247 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.lanya.packages.PackagesApplicationTests], using SpringBootContextLoader
10:40:37.247 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.lanya.packages.PackagesApplicationTests]: class path resource [com/lanya/packages/PackagesApplicationTests-context.xml] does not exist
10:40:37.247 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.lanya.packages.PackagesApplicationTests]: class path resource [com/lanya/packages/PackagesApplicationTestsContext.groovy] does not exist
10:40:37.247 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.lanya.packages.PackagesApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
10:40:37.247 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.lanya.packages.PackagesApplicationTests]: PackagesApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
10:40:37.278 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.lanya.packages.PackagesApplicationTests]
10:40:37.309 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [E:\ideaProject\packages\target\classes\com\lanya\packages\PackagesApplication.class]
10:40:37.309 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.lanya.packages.PackagesApplication for test class com.lanya.packages.PackagesApplicationTests
10:40:37.388 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.lanya.packages.PackagesApplicationTests]: using defaults.
10:40:37.388 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
10:40:37.403 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
10:40:37.403 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
10:40:37.403 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@4b741d6d, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@2eae8e6e, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@8f2ef19, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@470734c3, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@2cf3d63b, org.springframework.test.context.support.DirtiesContextTestExecutionListener@7674f035, org.springframework.test.context.event.EventPublishingTestExecutionListener@69e153c5, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@173ed316, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@25ce9dc4, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@74ea2410, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@17f62e33, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@76b1e9b8, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@27406a17]
10:40:37.403 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@74d1dc36 testClass = PackagesApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@7161d8d1 testClass = PackagesApplicationTests, locations = '{}', classes = '{class com.lanya.packages.PackagesApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@35ef1869, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2c767a52, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@43195e57, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@795cd85e, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@7c729a55, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@2ef5e5e3], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
10:40:37.434 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.5)
2021-04-26 10:40:37.591 INFO 11628 --- [ main] c.l.packages.PackagesApplicationTests : Starting PackagesApplicationTests using Java 1.8.0_152 on DESKTOP-VMPGGIA with PID 11628 (started by ShuoZhou in E:\ideaProject\packages)
2021-04-26 10:40:37.591 INFO 11628 --- [ main] c.l.packages.PackagesApplicationTests : No active profile set, falling back to default profiles: default
2021-04-26 10:40:38.340 INFO 11628 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-26 10:40:38.544 INFO 11628 --- [ main] c.l.packages.PackagesApplicationTests : Started PackagesApplicationTests in 1.106 seconds (JVM running for 1.746)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.5 s - in com.lanya.packages.PackagesApplicationTests
2021-04-26 10:40:38.684 INFO 11628 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ packages ---
[INFO] Building jar: E:\ideaProject\packages\target\packages-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.5:repackage (repackage) @ packages ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ packages ---
[INFO] Installing E:\ideaProject\packages\target\packages-0.0.1-SNAPSHOT.jar to C:\Users\ShuoZhou\.m2\repository\com\lanya\packages\0.0.1-SNAPSHOT\packages-0.0.1-SNAPSHOT.jar
[INFO] Installing E:\ideaProject\packages\pom.xml to C:\Users\ShuoZhou\.m2\repository\com\lanya\packages\0.0.1-SNAPSHOT\packages-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ packages ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.060 s
[INFO] Finished at: 2021-04-26T10:40:39+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project packages: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
对比 install 和 package 的执行代码,可以看到相比 package 多了 install 和 deploy 两个命令,相比 install 多了一个 deploy,并且这里 deploy 命令执行的时候报错了:
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ packages ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.060 s
[INFO] Finished at: 2021-04-26T10:40:39+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project packages: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
报错原因,是因为我们并没有在 pom 文件中配置私服仓库,这里要说下什么是私服仓库:
私服仓库就是远程仓库,另外一台电脑上的 maven 仓库,理论上也可以设置本地的另外一个文件夹为 maven 仓库——可以看做是私服仓库,但是没有意义。在多人团队开发的时候,一般会在一台公共服务器上创建一个 maven 仓库,每个人开发完自己的模块就会使用 git 提交到这台公共服务器,而这台公共服务器的 maven 仓库中会存储团队所有人需要的依赖包,相对于个人来说,自己计算机的 maven 仓库就是本地仓库,公共服务器上的 maven 仓库就是远程私服仓库。
至于如何配置私服仓库,因为暂时用不到这个功能,这里就不写了,配置私服仓库具体可以参考:maven 发布项目到私服-snapshot 快照库和 release 发布库的区别和作用及 maven 常用命令。
2.6 问题
问题一:三个命令打的 jar 包一样吗?
答案是一样的,为此我简单验证了一下,验证步骤:简单将项目改成了实战一中 helloworld 项目,依次使用三个命令打成三个 jar 包,依次运行后,发现运行结果都是一样的。
3、多模块打包
如果我们要对多模块项目打包怎么办呢?要知道多模块中一般是存在主模块(其他子模块都是为这个主模块服务,类似 java 类中的主函数),一般情况下都是打包这个主模块,但是如果在多模块中直接打包,会报错,以上面的 springboot-demo 项目为例,报错如下:
如上图,报错致命 demo-common 包不存在,因为 demo-admin 是调用了 demo-common 的函数,所以当时将 demo-common 作为依赖包写入了 demo-admin 的 pom 文件中,现在对 demo-admin 打包自然要将其所有依赖包都要打成一个 jar 包,然后才能独立运行。
但是 demo-admin 并没有打包到本地 maven 中,所以会报错找不到。
明白了原理,解决方案也就出来了——既然本地 maven 仓库中不存在,那就让它存在不就好了,现在我能想到的解决方案有两种:
- 第一种:使用功能 install 或者 deploy 将 demo-common 打包,都能将其 jar 部署到本地 maven 仓库中
- 第二种:给父模块 springboot-demo 打包,因为其打包类型为 pom,也就是会根据其 modules 罗列出来的所有模块都打包。
第二种方案有个问题,那就是 springboot-demo 采用 package 打包方式行不行?
我们先来看一下父模块的 package 命令代码(本身代表了打包方式为 pom 的命令代码):
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] springboot-demo [pom]
[INFO] demo-common [jar]
[INFO] demo-admin [jar]
[INFO]
//下面开始第一阶段,给父模块springboot-demo打包
[INFO] ---------------------< org.lanya:springboot-demo >----------------------
[INFO] Building springboot-demo 1.0-SNAPSHOT [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
//下面开始第二阶段,给子模块demo-common打包
[INFO] -----------------------< org.lanya:demo-common >------------------------
[INFO] Building demo-common 1.0-SNAPSHOT [2/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo-common ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo-common ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to E:\ideaProject\springboot-demo\demo-common\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ demo-common ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory E:\ideaProject\springboot-demo\demo-common\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo-common ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo-common ---
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ demo-common ---
[INFO] Building jar: E:\ideaProject\springboot-demo\demo-common\target\demo-common-1.0-SNAPSHOT.jar
[INFO]
//下面开始第三阶段,给子模块demo-admin打包
[INFO] ------------------------< org.lanya:demo-admin >------------------------
[INFO] Building demo-admin 1.0-SNAPSHOT [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo-admin ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo-admin ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to E:\ideaProject\springboot-demo\demo-admin\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ demo-admin ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory E:\ideaProject\springboot-demo\demo-admin\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo-admin ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo-admin ---
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ demo-admin ---
[INFO] Building jar: E:\ideaProject\springboot-demo\demo-admin\target\demo-admin-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for springboot-demo 1.0-SNAPSHOT:
[INFO]
[INFO] springboot-demo .................................... SUCCESS [ 0.000 s]
[INFO] demo-common ........................................ SUCCESS [ 1.468 s]
[INFO] demo-admin ......................................... SUCCESS [ 0.187 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.945 s
[INFO] Finished at: 2021-04-26T13:36:56+08:00
[INFO] ------------------------------------------------------------------------
可以很清楚地看到,整个流程被分为三部分:
- 第一步:springboot-demo 打包,打包方式为 pom,基本上没有运行,直接略过了,由此可见,pom 打包本身是个调用,调用子模块的 pom 文件,让它们一次打包;
- 第二步:demo-common 打包,打包方式为默认的 jar 包,运行包括:resources、compile、testResource、testCompile、test 以及 jar;
- 第三步:demo-admin 打包,打包方式为默认的 jar 包,运行步骤和第二步一样。
注意:这个打包顺序是按照相互依赖的顺序进行,与 pom 文件写的顺序并不一致。
从上面可以看到,无论是单模块还是父模块,package、install 和 deploy 运行效果都是一样的,所以想要解决依赖问题,必须是运行 install 或者 deploy。
OK,今天就到这里,更多精彩内容关注我的个人网站:蓝亚之舟博客。