What is the “right” way to (temporarily) exclude sources from the maven assembly, and is there an easy way to do this from Eclipse?

I am new to maven and I don't have much experience with Eclipse.

To exclude Java files from Eclipse, I simply click on the files and select "Build Path" → "Exclude". This is great for compiling "on-save" in Eclipse, but does not apply to the maven project, so when I create a project with mvn installed, it tries to compile excluded sources.

I did a few searches, and the results point me to a compiler plugin and "exclude" functionality, but editing maven project files to temporarily exclude a file from the assembly seems a bit uncomfortable.

What is the “right” way to (temporarily) exclude sources from the maven assembly, and is there an easy way to do this from Eclipse via the m2eclipse plugin or otherwise?

+4
source share
2 answers

You can use the < excludes > parameter in the 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 use the M2Eclipse plugin and run Maven-> Update Project Configuration, excluded files in pom should be automatically excluded from eclipse compilation.

+7
source

if you choose maven as project management then you really need to do this "maven way".

Eclipse creates a project based on the class path specified in the project properties and does not apply to the maven compiler plugin class path. "mvn compile" is only controlled by the configuration of the compiler plugin.

Typically, these “temporary” changes are handled by JVM parameters added to the maven target (the maven plugin / Mojo target that you use from cmd) that you create (custom) and save in “Run as”> Run configurations. I use commandline (shell), not m2eclipse for maven. changing options are faster for me.

To find out which parameters you can use, you can specify a specific Mojo (maven plugin) in your maven dependencies (only temporarily) and look at its sources directly in eclipse, you can see the parameters that can be specified using the "-D" JVM. Or you can check the documentation.

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

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

To summarize, your requirement is quite rare; excluding the java class from compilation is not a normal requirement.

I hope this helps

0
source

Source: https://habr.com/ru/post/1337001/


All Articles