控制哪些目标/执行(不是阶段) 在 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