Java maven反应堆构建学习实践
实践环境
Apache Maven 3.0.5 (Red Hat 3.0.5-17)
应用示例
示例项目结构
maven示例项目组织结构如下
maven-study │ pom.xml │ ├─first-sub-module │ │ pom.xml │ │ │ ├─src │ │ ├─main │ │ │ └─java │ │ │ └─com │ │ │ └─shouke │ │ │ └─mvnstudy │ │ │ HelloMaven.java │ │ │ │ │ └─test │ │ └─java │ │ └─com │ │ └─shouke │ │ └─mvnstudy │ │ TestHelloMaven.java │ │ │ └─sub-sub-module │ │ pom.xml │ │ │ └─src │ ├─main │ │ └─java │ │ └─com │ │ └─shouke │ │ └─mvnstudy │ │ HelloMaven.java │ │ │ └─test │ └─java │ └─com │ └─shouke │ └─mvnstudy │ TestHelloMaven.java │ └─second-sub-module │ pom.xml │ └─src ├─main │ └─java │ └─com │ └─shouke │ └─mvnstudy │ HelloMaven.java │ └─test └─java └─com └─shouke └─mvnstudy TestHelloMaven.java
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.shouke.mvnstudy</groupId> <artifactId>parent-module</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <dependencies> <!-- junit 依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> <modules> <module>first-sub-module</module> <module>second-sub-module</module> </modules> </project> </xml>
first-sub-module/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.shouke.mvnstudy</groupId> <artifactId>first-sub-module</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <!-- junit 依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>com.shouke.mvnstudy</groupId> <artifactId>second-sub-module</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.shouke.mvnstudy</groupId> <artifactId>sub-sub-module</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> </dependencies> <parent> <groupId>com.shouke.mvnstudy</groupId> <!--父项目id--> <artifactId>parent-module</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> </project> </xml>
first-sub-module/sub-sub-module/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.shouke.mvnstudy</groupId> <artifactId>sub-sub-module</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <!-- junit 依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project> </xml>
second-sub-module/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.shouke.mvnstudy</groupId> <artifactId>second-sub-module</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <!-- junit 依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> <parent> <groupId>com.shouke.mvnstudy</groupId> <!--父项目id--> <artifactId>parent-module</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> </project> </xml>
学习实践
-pl
选项应用
# mvn clean -Dautoconfig.skip=true -Dmaven.test.skip=true -pl first-sub-module compile [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building first-sub-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.219s [INFO] Finished at: Fri Dec 24 11:20:23 CST 2021 [INFO] Final Memory: 17M/1447M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal on project first-sub-module: Could not resolve dependencies for project com.shouke.mvnstudy:first-sub-module:jar:1.0.0-SNAPSHOT: Could not find artifact com.shouke.mvnstudy:second-sub-module:jar:1.0.0-SNAPSHOT -> [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/DependencyResolutionException
-am
选项应用
# mvn clean -Dautoconfig.skip=true -Dmaven.test.skip=true -pl first-sub-module -am compile [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] parent-module [INFO] second-sub-module [INFO] first-sub-module [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building parent-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ parent-module --- [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building second-sub-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ second-sub-module --- [INFO] Deleting /root/maven-study/second-sub-module/target [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ second-sub-module --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/maven-study/second-sub-module/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ second-sub-module --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/maven-study/second-sub-module/target/classes [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building first-sub-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ first-sub-module --- [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ first-sub-module --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/maven-study/first-sub-module/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ first-sub-module --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/maven-study/first-sub-module/target/classes [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] parent-module ..................................... SUCCESS [0.146s] [INFO] second-sub-module ................................. SUCCESS [0.576s] [INFO] first-sub-module .................................. SUCCESS [0.346s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.187s [INFO] Finished at: Fri Dec 24 11:22:06 CST 2021 [INFO] Final Memory: 22M/1096M [INFO] ------------------------------------------------------------------------
-amd
选项应用
对比示例
# mvn clean -Dautoconfig.skip=true -Dmaven.test.skip=true -pl second-sub-module compile [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building second-sub-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ second-sub-module --- [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ second-sub-module --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/maven-study/second-sub-module/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ second-sub-module --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/maven-study/second-sub-module/target/classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.976s [INFO] Finished at: Fri Dec 24 11:21:18 CST 2021 [INFO] Final Memory: 23M/1447M [INFO] ------------------------------------------------------------------------
# mvn clean -Dautoconfig.skip=true -Dmaven.test.skip=true -pl second-sub-module -am compile [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] parent-module [INFO] second-sub-module [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building parent-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ parent-module --- [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building second-sub-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ second-sub-module --- [INFO] Deleting /root/maven-study/second-sub-module/target [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ second-sub-module --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/maven-study/second-sub-module/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ second-sub-module --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/maven-study/second-sub-module/target/classes [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] parent-module ..................................... SUCCESS [0.132s] [INFO] second-sub-module ................................. SUCCESS [0.536s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.771s [INFO] Finished at: Fri Dec 24 11:24:13 CST 2021 [INFO] Final Memory: 23M/1447M [INFO] ------------------------------------------------------------------------
-amd
应用示例
# mvn clean -Dautoconfig.skip=true -Dmaven.test.skip=true -pl second-sub-module -amd compile [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] second-sub-module [INFO] first-sub-module [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building second-sub-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ second-sub-module --- [INFO] Deleting /root/maven-study/second-sub-module/target [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ second-sub-module --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/maven-study/second-sub-module/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ second-sub-module --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/maven-study/second-sub-module/target/classes [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building first-sub-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ first-sub-module --- [INFO] Deleting /root/maven-study/first-sub-module/target [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ first-sub-module --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/maven-study/first-sub-module/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ first-sub-module --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/maven-study/first-sub-module/target/classes [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] second-sub-module ................................. SUCCESS [0.652s] [INFO] first-sub-module .................................. SUCCESS [0.344s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.100s [INFO] Finished at: Fri Dec 24 11:24:33 CST 2021 [INFO] Final Memory: 24M/1157M [INFO] ------------------------------------------------------------------------
嵌套子模块构建
# mvn clean -Dautoconfig.skip=true -Dmaven.test.skip=true -pl first-sub-module/sub-sub-module compile [INFO] Scanning for projects... [ERROR] Could not find the selected project in the reactor: first-sub-module/sub-sub-module -> [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/MavenExecutionException
修改first-sub-module/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.shouke.mvnstudy</groupId> <artifactId>first-sub-module</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <dependencies> <!-- junit 依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>com.shouke.mvnstudy</groupId> <artifactId>second-sub-module</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> </dependencies> <parent> <groupId>com.shouke.mvnstudy</groupId> <!--父项目id--> <artifactId>parent-module</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <modules> <module>sub-sub-module</module> </modules> </project> </xml>
修改first-sub-module/sub-sub-module/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.shouke.mvnstudy</groupId> <artifactId>sub-sub-module</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <!-- junit 依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> <parent> <groupId>com.shouke.mvnstudy</groupId> <!--父项目id--> <artifactId>first-sub-module</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> </project> </xml>
# mvn clean -Dautoconfig.skip=true -Dmaven.test.skip=true -pl first-sub-module/sub-sub-module -am compile [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] parent-module [INFO] second-sub-module [INFO] first-sub-module [INFO] sub-sub-module [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building parent-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ parent-module --- [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building second-sub-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ second-sub-module --- [INFO] Deleting /root/maven-study/second-sub-module/target [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ second-sub-module --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/maven-study/second-sub-module/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ second-sub-module --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/maven-study/second-sub-module/target/classes [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building first-sub-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ first-sub-module --- [INFO] Deleting /root/maven-study/first-sub-module/target [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building sub-sub-module 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ sub-sub-module --- [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ sub-sub-module --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/maven-study/first-sub-module/sub-sub-module/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ sub-sub-module --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/maven-study/first-sub-module/sub-sub-module/target/classes [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] parent-module ..................................... SUCCESS [0.148s] [INFO] second-sub-module ................................. SUCCESS [0.728s] [INFO] first-sub-module .................................. SUCCESS [0.005s] [INFO] sub-sub-module .................................... SUCCESS [0.370s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.370s [INFO] Finished at: Fri Dec 24 11:39:07 CST 2021 [INFO] Final Memory: 22M/1098M [INFO] ------------------------------------------------------------------------
结论
注意-pl
只适用在“父子”结构的项目中,构建子项目
针对以上结论,修改相关pom.xml
,再次测试验证
修改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.shouke.mvnstudy</groupId> <artifactId>parent-module</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <!-- junit 依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project> </xml>
修改second-sub-module/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.shouke.mvnstudy</groupId> <artifactId>second-sub-module</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <!-- junit 依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project> </xml>
# mvn clean -Dautoconfig.skip=true -Dmaven.test.skip=true -pl second-sub-module compile [INFO] Scanning for projects... [ERROR] Could not find the selected project in the reactor: second-sub-module -> [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/MavenExecutionException
如上,提示找不到模块。
-------------------------------文字太少,占位符---------------------------------
-------------------------------文字太少,占位符---------------------------------