在maven项目中有条件地从编译中排除java文件的最简单/最正确的方法是什么?
我希望能够在pom.xml中设置'boolean'属性:
<properties>
<IncludeMayBe>true</IncludeMayBe>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<excludes>
????
</excludes>
</configuration>
</plugin>
有没有办法用编译器插件来捣乱?或者我应该去个人资料?我觉得创建个人资料有点矫枉过正,但可能这是唯一的解决方案......
修改
我们已经确定配置文件是解决方案。对于pom.xml中的条件激活,可以使用以下命令:
<profiles>
<profile>
<activation>
<property>
<IncludeMayBe>true</IncludeMayBe>
</property>
</activation>
...
</profile>
</profiles>
答案 0 :(得分:5)
我建议您使用Build helper maven plugin。 使用它,您可以拥有多个源目录。 然后,您可以使用配置文件控制包含哪些源目录。
假设您在src / monitoring / java下有监控类,可以将以下内容添加到pom.xml中的元素
<profiles>
<profile>
<id>monitoring</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/monitoring/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
答案 1 :(得分:1)
您无法使用编译器插件执行此操作(请参阅下文)。
但即使你可以,也感觉不对。这意味着工件JAR文件中的文件将依赖于命令行开关和/或环境设置,这使得其他人更难以重现构建。
我的直觉是,您最好模块化您的maven项目并使用配置文件来确定构建哪些模块。
我看了一下编译器插件mojo的源代码,看起来没有办法配置源包含/排除过滤器。在某些时候,有人已经实现了过滤器,但相关的Map
对象是私有的,没有办法填充它们,因此无法使用此功能。
我想你可以破解你自己的插件版本......但这似乎是一个坏主意。