只有這樣,才能控制哪些目標/個執行(不階段)期間Maven構建無論你的項目有模塊或不執行,是通過配置文件。
例如:
/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
</project>
/module1/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>module1</artifactId>
</project>
/module2/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>module2</artifactId>
<profiles>
<profile>
<id>module2:ignore-compile</id>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
在運行時mvn -Pmodule2:ignore-compile package
。你會注意到源代碼編譯(但只有這個目標/執行!)將被module2忽略。
您還可以使用activation
:
<profiles>
<profile>
<id>module2:ignore-compile</id>
<activation>
<property>
<name>module2:ignore-compile</name>
</property>
</activation>
....
</profile>
</profiles>
然後用命令:mvn -Dmodule2:ignore-compile package
最後,一個有趣的功能是通過配置文件來改變模塊:
/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
</modules>
<profiles>
<profile>
<id>with:module2</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>module2</module>
</modules>
</profile>
</profiles>
</project>
忽略module2
:mvn '-P!with:module2' package