问题
What the easiest/right way to conditionally exclude a java file from compilation in a maven project?
I would like to be able to set a 'boolean' properties in the pom.xml:
<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>
Is there a way to fiddle something with the compiler plugin? Or should I go for profiles? I feel like creating a profile is overkill, but may be this is the only solution...
EDIT:
We have established that profiles are the solution. For conditional activation from within the pom.xml, one can use the following:
<profiles>
<profile>
<activation>
<property>
<IncludeMayBe>true</IncludeMayBe>
</property>
</activation>
...
</profile>
</profiles>
回答1:
I suggest you use the Build helper maven plugin. Using this, you can have several source directories. Then you can control what source directories are included using profiles.
Assuming you have your monitoring classes under src/monitoring/java you could add the following to the element in your 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>
回答2:
You cannot do this using the compiler plugin (see below).
But even if you could, it doesn't feel right. It would mean that the files in a artifact JAR file would depend on command line switches and/or environmental settings, and that makes it harder for other folks to reproduce builds.
My gut feeling is that you'd be better of modularizing your maven project and using profiles to determine what modules get built.
I had a look at the source code of the compiler plugin mojo, and it looks like there's no way to configure source include / exclude filters. At some point, someone has implemented filters, but the relevant Map objects are private and there is no way to populate them, and hence no way to use this functionality.
The code is here: http://svn.apache.org/viewvc/maven/plugins/tags/maven-compiler-plugin-2.3.2/src/main/java/org/apache/maven/plugin/
I guess you could hack your own version of the plugin ... but it seems like a bad idea.
来源:https://stackoverflow.com/questions/6025596/conditional-exclusion-of-file-from-compilation-in-maven-project