Maven多模块情况下只编译单个子模块

例如有三个模块,聚合模块Parent,和模块Son1以及模块Son2,其中Son2又依赖Son1。

Parent.pom

<?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>org.example</groupId>
  <artifactId>Parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>../Son1</module>
    <module>../Son2</module>
  </modules>

  <name>Parent</name>
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

Son1.pom

<?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>org.example</groupId>
  <artifactId>Son1</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>Son1</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>


</project>

Son2.pom

<?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>org.example</groupId>
  <artifactId>Son2</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>Son2</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.example</groupId>
      <artifactId>Son1</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

</project>

分别编译这三个模块,会是下面的现象:

1.编译Parent,同时会编译Son1和Son2。

[INFO] Reactor Build Order:
[INFO] 
[INFO] Son1                                                               [jar]
[INFO] Son2                                                               [jar]
[INFO] Parent                                                             [pom]

2.编译Son1,则只会编译Son1。

[INFO] Building Son1 1.0-SNAPSHOT

3.编译Son2,如果先Son1先install,再编译Son2则成功。

如果不先install Son1,那么编译Son2的时候会因为找不到Son1失败。

[INFO] Building Son2 1.0-SNAPSHOT
[WARNING] The POM for org.example:Son1:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] BUILD FAILURE

所以针对这种情况可以在Parent.pom层级下执行:

mvn clean install -pl ../Son2 -am 或者 mvn clean install -pl :Son2 -am 

[INFO] Reactor Build Order:
[INFO] 
[INFO] Son1                                                               [jar]
[INFO] Son2                                                               [jar]

-pl, --projects
        Build specified reactor projects instead of all projects
-am, --also-make
        If project list is specified, also build projects required by the list
        (Maven 将构建指定项目所依赖的所有项目(直接或间接))

Ref:

java - Maven Modules + Building a Single Specific Module - Stack Overflowhttps://stackoverflow.com/questions/1114026/maven-modules-building-a-single-specific-moduleMaven Tips and Tricks: Advanced Reactor Optionshttps://blog.sonatype.com/2009/10/maven-tips-and-tricks-advanced-reactor-options/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值