开发者

What's the "right" way to (temporarily) exclude sources from a maven build, and is there an easy way to do it from Eclipse?

I'm new to maven, and I don't have a whole lot of experience with Eclipse either.

To exclude java files from building in Eclipse, I right click the files and choose Build Path -> Exclude. This works great for the "on-save" compilation in Eclipse, but it does not propagate to the maven project, so when I build my project with mvn install, it tries to compile the excluded sources.

I've done a few searches and the results point me to the compiler plugin and the <excludes> functionality, but editing maven project files in order to temporarily exclude a file from the build seems a bit awkward.

What's the "right" way to (temporarily) exclude sources from a maven build, and i开发者_开发百科s there an easy way to do it from Eclipse, via the m2eclipse plugin or otherwise?


You could use the <excludes> parameter in Maven Compiler plugin to temporarily exclude files from compilation.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <excludes>
                    <exclude>**/model/*.java</exclude>
                </excludes>
            </configuration>
        </plugin>

If you are using M2Eclipse plugin and run Maven->Update Project Configuration, the excluded files in the pom should automagically get excluded from eclipse compilation as well.


if you choose maven as project management, then you really have to do it the "maven way".

Eclipse builds the project based on the classpath specified in project properties and it doesn't relate to classpath of maven compiler plugin. "mvn compile" is driven only by configuration of compiler plugin.

Usually these "temporary" changes are dealt with by JVM parameters appended to the maven goal (maven plugin/Mojo goal that you are running from cmd), that you create (custom one) and save in "Run as" > "Run configurations". I use commandline (shell) rather than m2eclipse for maven. changing parameters is quicker for me.

To find out what parameters you can use, you can either specify the particular Mojo (maven plugin) in you maven dependencies (just temporarily) and look at its sources right in eclipse, you can see parameters that can be specified via "-D" JVM parameters. Or you can check the documentation.

In compiler plugin there is a parameter private Set<String> excludes = new HashSet<String>(); but unfortunately collection parameters cannot be specified as JVM parameters... So the only option left is configure the plugin declaration in pom.xml.

Then there are also profiles, but they are not useful for this case.

To summarize it, your requirement is rather rare, excluding a java class from compilation is not a usual requirement.

I hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜