多模块 Maven 项目 - 如何构建一些模块

问题描述 投票:0回答:1

我们有以下场景

父pom包含

<packaging>pom</packaging>
  <modules>
    <module>mod1</module> <!--<packaging>jar</packaging>-->
    <module>mod2</module> <!--<packaging>pom</packaging>-->
    <module>mod3</module> <!--<packaging>jar</packaging>-->
    <module>mod4</module> <!--<packaging>jar</packaging>-->
  </modules>

mod2 依赖于 mod1 并包含

<packaging>pom</packaging>
  <modules>
    <module>mod21</module> <!--<packaging>jar</packaging>-->
    <module>mod22</module> <!--<packaging>jar</packaging>-->
  </modules>

需要构建 mod1 和 mod2 中的两个 mod(所有 jar),而不构建所有其他(mod3、mod4 等) 跑步

mvn clean install -pl mod1,mod2

为mod1生成jar,但不为mod21/mod22生成jar

实现它的正确maven命令是什么?

作为解决方法,我们使用配置文件,但想知道是否可以采取不同的做法

UPD1:

mvn clean install -pl mod1,mod2
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] mod1                                                               [jar]
[INFO] mod2                                                               [pom]
[INFO] 
[INFO] --------------------------< org.example:mod1 >--------------------------
[INFO] Building mod1 1.0-SNAPSHOT                                         [1/2]
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- clean:3.2.0:clean (default-clean) @ mod1 ---
[INFO] 
[INFO] --- resources:3.3.1:resources (default-resources) @ mod1 ---
[INFO] Copying 0 resource from src/main/resources to target/classes
[INFO] 
[INFO] --- compiler:3.11.0:compile (default-compile) @ mod1 ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 1 source file with javac [debug target 17] to target/classes
[INFO] 
[INFO] --- resources:3.3.1:testResources (default-testResources) @ mod1 ---
[INFO] skip non existing resourceDirectory /home/uzer/workspace/multi-module/mod1/src/test/resources
[INFO] 
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ mod1 ---
[INFO] Changes detected - recompiling the module! :dependency
[INFO] 
[INFO] --- surefire:3.2.2:test (default-test) @ mod1 ---
[INFO] 
[INFO] --- jar:3.3.0:jar (default-jar) @ mod1 ---
[INFO] Building jar: /home/uzer/workspace/multi-module/mod1/target/mod1-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- install:3.1.1:install (default-install) @ mod1 ---
[INFO] Installing /home/uzer/workspace/multi-module/mod1/pom.xml to /home/uzer/.m2/repository/org/example/mod1/1.0-SNAPSHOT/mod1-1.0-SNAPSHOT.pom
[INFO] Installing /home/uzer/workspace/multi-module/mod1/target/mod1-1.0-SNAPSHOT.jar to /home/uzer/.m2/repository/org/example/mod1/1.0-SNAPSHOT/mod1-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --------------------------< org.example:mod2 >--------------------------
[INFO] Building mod2 1.0-SNAPSHOT                                         [2/2]
[INFO]   from /home/uzer/workspace/multi-module/mod2/pom.xml
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- clean:3.2.0:clean (default-clean) @ mod2 ---
[INFO] 
[INFO] --- install:3.1.1:install (default-install) @ mod2 ---
[INFO] Installing /home/uzer/workspace/multi-module/mod2/pom.xml to /home/uzer/.m2/repository/org/example/mod2/1.0-SNAPSHOT/mod2-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for mod1 1.0-SNAPSHOT:
[INFO] 
[INFO] mod1 ............................................... SUCCESS [  0.657 s]
[INFO] mod2 ............................................... SUCCESS [  0.005 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.721 s
[INFO] Finished at: 2024-05-18T15:40:07+01:00
[INFO] ------------------------------------------------------------------------```
maven maven-3
1个回答
0
投票

如果您确切知道要构建

mod2
及其所有子模块,则可以使用
mvn -f mod2 clean install
。 选项
-f
帮助您指定 Maven 考虑的根 POM。在上面的建议中,它“聚焦”maven 这个模块。它将构建
mod2
及其子模块
mod21
mod22

但是,假设

mod3
也有子模块,因为你只能指定一个根文件,所以你不能使用选项
-f
来构建
mod2
AND
mod3
及其子模块。

© www.soinside.com 2019 - 2024. All rights reserved.