Maven多模块——打包指定模块

迷南。 2023-06-26 05:51 105阅读 0赞

mvn -h 查看命令及其用途:

  1. E:\nicole\workspace\test_parent>mvn -h
  2. usage: mvn [options] [<goal(s)>] [<phase(s)>]
  3. Options:
  4. -am,--also-make If project list is specified, also
  5. build projects required by the
  6. list
  7. -amd,--also-make-dependents If project list is specified, also
  8. build projects that depend on
  9. projects on the list
  10. -B,--batch-mode Run in non-interactive (batch)
  11. mode (disables output color)
  12. -b,--builder <arg> The id of the build strategy to
  13. use
  14. -C,--strict-checksums Fail the build if checksums don't match -c,--lax-checksums Warn if checksums don't match
  15. -cpu,--check-plugin-updates Ineffective, only kept for
  16. backward compatibility
  17. -D,--define <arg> Define a system property
  18. -e,--errors Produce execution error messages
  19. -emp,--encrypt-master-password <arg> Encrypt master security password
  20. -ep,--encrypt-password <arg> Encrypt server password
  21. -f,--file <arg> Force the use of an alternate POM
  22. file (or directory with pom.xml)
  23. -fae,--fail-at-end Only fail the build afterwards;
  24. allow all non-impacted builds to
  25. continue
  26. -ff,--fail-fast Stop at first failure in
  27. reactorized builds
  28. -fn,--fail-never NEVER fail the build, regardless
  29. of project result
  30. -gs,--global-settings <arg> Alternate path for the global
  31. settings file
  32. -gt,--global-toolchains <arg> Alternate path for the global
  33. toolchains file
  34. -h,--help Display help information
  35. -l,--log-file <arg> Log file where all build output
  36. will go (disables output color)
  37. -llr,--legacy-local-repository Use Maven 2 Legacy Local
  38. Repository behaviour, ie no use of
  39. _remote.repositories. Can also be
  40. activated by using
  41. -Dmaven.legacyLocalRepo=true
  42. -N,--non-recursive Do not recurse into sub-projects
  43. -npr,--no-plugin-registry Ineffective, only kept for
  44. backward compatibility
  45. -npu,--no-plugin-updates Ineffective, only kept for
  46. backward compatibility
  47. -nsu,--no-snapshot-updates Suppress SNAPSHOT updates
  48. -ntp,--no-transfer-progress Do not display transfer progress
  49. when downloading or uploading
  50. -o,--offline Work offline
  51. -P,--activate-profiles <arg> Comma-delimited list of profiles
  52. to activate
  53. -pl,--projects <arg> Comma-delimited list of specified
  54. reactor projects to build instead
  55. of all projects. A project can be
  56. specified by [groupId]:artifactId
  57. or by its relative path
  58. -q,--quiet Quiet output - only show errors
  59. -rf,--resume-from <arg> Resume reactor from specified
  60. project
  61. -s,--settings <arg> Alternate path for the user
  62. settings file
  63. -t,--toolchains <arg> Alternate path for the user
  64. toolchains file
  65. -T,--threads <arg> Thread count, for instance 2.0C
  66. where C is core multiplied
  67. -U,--update-snapshots Forces a check for missing
  68. releases and updated snapshots on
  69. remote repositories
  70. -up,--update-plugins Ineffective, only kept for
  71. backward compatibility
  72. -v,--version Display version information
  73. -V,--show-version Display version information
  74. WITHOUT stopping build
  75. -X,--debug Produce execution debug output

假设Maven多模块项目如下:
在这里插入图片描述
test-parent pom.xml:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.nicole</groupId>
  6. <artifactId>test-parent</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <name>test-parent</name>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <modules>
  14. <module>test-common</module>
  15. <module>test-module1</module>
  16. <module>test-module2</module>
  17. <module>test-module3</module>
  18. </modules>
  19. <dependencyManagement>
  20. <dependencies>
  21. <dependency>
  22. <groupId>com.nicole</groupId>
  23. <artifactId>test-common</artifactId>
  24. <version>0.0.1-SNAPSHOT</version>
  25. </dependency>
  26. </dependencies>
  27. </dependencyManagement>
  28. </project>

test-common pom.xml:

  1. <?xml version="1.0"?>
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.nicole</groupId>
  7. <artifactId>test-parent</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <artifactId>test-common</artifactId>
  11. <name>test-common</name>
  12. <url>http://maven.apache.org</url>
  13. <packaging>jar</packaging>
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. </properties>
  17. </project>

test-module1 pom.xml:

  1. <?xml version="1.0"?>
  2. <project
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  4. xmlns="http://maven.apache.org/POM/4.0.0"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  6. <modelVersion>4.0.0</modelVersion>
  7. <parent>
  8. <groupId>com.nicole</groupId>
  9. <artifactId>test-parent</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. </parent>
  12. <artifactId>test-module1</artifactId>
  13. <packaging>war</packaging>
  14. <name>test-module1 Maven Webapp</name>
  15. <dependencies>
  16. <dependency>
  17. <groupId>com.nicole</groupId>
  18. <artifactId>test-common</artifactId>
  19. </dependency>
  20. </dependencies>
  21. <build>
  22. <finalName>test-module1</finalName>
  23. </build>
  24. </project>

test-common被test-module1,test-module2,test-module3给继承。
示例一、打包所有模块

  1. E:\nicole\workspace\test_parent>mvn clean install
  2. [INFO] ------------------------------------------------------------------------
  3. [INFO] Reactor Summary for test-parent 0.0.1-SNAPSHOT:
  4. [INFO]
  5. [INFO] test-parent ........................................ SUCCESS [ 0.623 s]
  6. [INFO] test-common ........................................ SUCCESS [ 3.274 s]
  7. [INFO] test-module1 Maven Webapp .......................... SUCCESS [ 0.966 s]
  8. [INFO] test-module2 Maven Webapp .......................... SUCCESS [ 0.434 s]
  9. [INFO] test-module3 Maven Webapp .......................... SUCCESS [ 0.678 s]
  10. [INFO] ------------------------------------------------------------------------
  11. [INFO] BUILD SUCCESS
  12. [INFO] ------------------------------------------------------------------------
  13. [INFO] Total time: 6.424 s
  14. [INFO] Finished at: 2019-12-30T18:01:30+08:00
  15. [INFO] ------------------------------------------------------------------------

示例二、-pl 打包指定模块

  1. E:\nicole\workspace\test_parent>mvn clean install -pl test-common,test-module1
  2. [INFO] ------------------------------------------------------------------------
  3. [INFO] Reactor Summary for test-common 0.0.1-SNAPSHOT:
  4. [INFO]
  5. [INFO] test-common ........................................ SUCCESS [ 3.494 s]
  6. [INFO] test-module1 Maven Webapp .......................... SUCCESS [ 1.056 s]
  7. [INFO] ------------------------------------------------------------------------
  8. [INFO] BUILD SUCCESS
  9. [INFO] ------------------------------------------------------------------------
  10. [INFO] Total time: 4.909 s
  11. [INFO] Finished at: 2019-12-30T18:02:39+08:00
  12. [INFO] ------------------------------------------------------------------------

示例三、-am 同时打包所指定模块的依赖模块

  1. E:\nicole\workspace\test_parent>mvn clean install -pl test-module1 -am
  2. [INFO] ------------------------------------------------------------------------
  3. [INFO] Reactor Summary for test-parent 0.0.1-SNAPSHOT:
  4. [INFO]
  5. [INFO] test-parent ........................................ SUCCESS [ 0.559 s]
  6. [INFO] test-common ........................................ SUCCESS [ 3.198 s]
  7. [INFO] test-module1 Maven Webapp .......................... SUCCESS [ 1.020 s]
  8. [INFO] ------------------------------------------------------------------------
  9. [INFO] BUILD SUCCESS
  10. [INFO] ------------------------------------------------------------------------
  11. [INFO] Total time: 5.127 s
  12. [INFO] Finished at: 2019-12-30T18:04:49+08:00
  13. [INFO] ------------------------------------------------------------------------

示例四、-amd 同时打包依赖于所指定模块的模块

  1. E:\nicole\workspace\test_parent>mvn clean install -pl test-common -amd
  2. [INFO] ------------------------------------------------------------------------
  3. [INFO] Reactor Summary for test-common 0.0.1-SNAPSHOT:
  4. [INFO]
  5. [INFO] test-common ........................................ SUCCESS [ 3.497 s]
  6. [INFO] test-module1 Maven Webapp .......................... SUCCESS [ 1.178 s]
  7. [INFO] test-module2 Maven Webapp .......................... SUCCESS [ 0.536 s]
  8. [INFO] test-module3 Maven Webapp .......................... SUCCESS [ 0.746 s]
  9. [INFO] ------------------------------------------------------------------------
  10. [INFO] BUILD SUCCESS
  11. [INFO] ------------------------------------------------------------------------
  12. [INFO] Total time: 6.330 s
  13. [INFO] Finished at: 2019-12-30T18:06:11+08:00
  14. [INFO] ------------------------------------------------------------------------

示例五、-rf 从所指定模块顺序开始打包

  1. E:\nicole\workspace\test_parent>mvn clean install -rf test-module2
  2. [INFO] ------------------------------------------------------------------------
  3. [INFO] Reactor Summary for test-module2 Maven Webapp 0.0.1-SNAPSHOT:
  4. [INFO]
  5. [INFO] test-module2 Maven Webapp .......................... SUCCESS [ 2.146 s]
  6. [INFO] test-module3 Maven Webapp .......................... SUCCESS [ 0.489 s]
  7. [INFO] ------------------------------------------------------------------------
  8. [INFO] BUILD SUCCESS
  9. [INFO] ------------------------------------------------------------------------
  10. [INFO] Total time: 3.128 s
  11. [INFO] Finished at: 2019-12-30T18:06:43+08:00
  12. [INFO] ------------------------------------------------------------------------

示例六、-pl -amd -rf 对裁剪后的模块堆再次裁剪

  1. E:\nicole\workspace\test_parent>mvn clean install -pl test-common -amd -rf test-module1
  2. [INFO] ------------------------------------------------------------------------
  3. [INFO] Reactor Summary for test-module1 Maven Webapp 0.0.1-SNAPSHOT:
  4. [INFO]
  5. [INFO] test-module1 Maven Webapp .......................... SUCCESS [ 2.195 s]
  6. [INFO] test-module2 Maven Webapp .......................... SUCCESS [ 0.523 s]
  7. [INFO] test-module3 Maven Webapp .......................... SUCCESS [ 0.633 s]
  8. [INFO] ------------------------------------------------------------------------
  9. [INFO] BUILD SUCCESS
  10. [INFO] ------------------------------------------------------------------------
  11. [INFO] Total time: 3.889 s
  12. [INFO] Finished at: 2019-12-30T18:07:46+08:00
  13. [INFO] ------------------------------------------------------------------------

-pl -amd 得到test-common,test-module1,test-module2,test-module3
rf 从test-module1开始打包

发表评论

表情:
评论列表 (有 0 条评论,105人围观)

还没有评论,来说两句吧...

相关阅读