What's the "right" way to (temporarily) exclude sources from a maven build, and is there an easy way to do it from Eclipse? - 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 is 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

Related

Creating second customized POM in eclipse

Within a maven project under eclipse, I want to have a second(or customized) pom.xml in which I can use plugings like the assembly-plugin.
The problem with this plugin is that it requieres an outputh path which is only interesting for me.
Since I'm using git to push to a remote repository, I don't want to pollute the version controlled pom.xml with my private paths and other stuff.
I read about inheritance and multi-mode possibilities, but I only need two poms:
1) One for the public with general settings
2) One only for me with cusotimzed build options
I tried to create a second pom file and wanted to build the project with a new run configuration, but I don't know how to pass the -f parameter(which should call a different pom) in that dialog.
Thanks for hints or best practices.
Example of what I want to put in the custom pom:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<outputDirectory>some\private\path</outputDirectory>
<finalName>SomeName</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
Select the second POM in the Package Explorer, right click -> Run As -> Maven Build...
That should run Maven with the custom POM.
If you don't get the Maven options in the "Run As" menu, go to the "Content Types" preferences page -> Text -> XML -> Maven POM XML.
Add the name of your custom POM so Eclipse understands that this is also a POM (I'm not 100% sure it will look inside a file to determine the type).
If that also fails, you can use a trick: Write a small tool that takes the unmodified POM, adds the XML which you need and then runs Maven. On Linux, you can use shell scripts for that. On Windows, a small Java program might be easier. Or have a look at PowerShell.

Eclipse JDT and Maven/Tycho do not agree on warnings

This is related to How to avoid Eclipse importing a class when putting the class name in the comments, so that checkstyle does not complain later?, but is a different issue.
I have a bunch of Javadoc references with import statements, as describe in the referenced question. Eclipse does not warn about this, but I still get compiler warnings when building my code with Maven/Tycho. I thought that specifying <compilerId>jdt</compilerId> should make Maven use the same compiler as Eclipse does, and there by generate identical sets of warnings.
I understand that I can use the fully qualified name in the Javadoc tag to avoid the import statement, but what I wonder here is how do I get the same set of compiler warnings when building with Maven/Tycho as when I build in Eclipse?
Tycho uses the JDT compiler by default, so you don't need to set the <compilerId>. However the compiler settings defaults may be different in Tycho and in Eclipse, or you may have changed the default settings in your workspace. You could try to configure the compiler in Tycho via the <compilerArgs> parameter to match your workspace settings, but this may get quite tricky.
With Tycho 0.22.0 (cf. bug 404633) there is much easier way to get the exact same compiler settings in Eclipse and in Tycho:
Enable project-specific Java compiler settings in Eclipse and configure them in the way you want them. This creates a file .settings/org.eclipse.jdt.core.prefs in the project.
Add the following Maven configuration:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<useProjectSettings>true</useProjectSettings>
</configuration>
</plugin>
This makes the JDT compiler in Tycho use the Eclipse project settings whenever they are present.
Note that you need to put the .settings/org.eclipse.jdt.core.prefs files under version control to make your build reproducible.

GWT maven compiler output directory

I need to set output directory location for GWT compiler using maven plugin.
I've investigated the GWT compiler and gwt-maven-plugin docs and did not find any options for this.
For example:
I have two modules named editor and console and gwt-maven-plugin compiles them to %ROOT%/editor and %ROOT%/console dirs.
But I need to have these modules compiled into %ROOT%/ria/editor and %ROOT%/ria/console dirs to reduce effort to maintain the URL based security and other stuff.
Is it possible to set ria directory in plugin configuration to avoid unneeded new ANT task creation for moving compiled modules stuff to needed place?
I had to un-check the checkbox highlighted in the below screenshot
<plugin>
<artifactId>gwt-maven-plugin</artifactId>
<configuration>
<webappDirectory>${project.build.directory}/${project.build.finalName}/ria</webappDirectory>
</configuration>
</plugin>
See http://mojo.codehaus.org/gwt-maven-plugin/compile-mojo.html#webappDirectory
It won't play well with the DevMode though, unless you run in -noserver mode (which I'd recommend anyway for any middle-sized/big project)

Problems by import of a multiple modules maven 2 project into eclipse workspace

I was wondering if someone has experienced the same problem as me and can help me.
I have a maven project which contains 6 modules. Some of modules are depending on each other. The project is written in Java and builds to jars, wars and aar. I've been trying to import it to Eclipse with the m2eclipse plug-in. It seems to work fine until the project builds. During the build process I get hundreds of errors complaining about missing Java files which are generated. As I found out eclipse can't recognize that some of generated packages should be interpreted as source code. I don't realy know what to do with it as I spent a lot of time already trying to solve this issue. The project is building fine with command line. My target is to debug the whole project on Tomcat server that's why I want to use eclipse as it has a pretty good integration with Tomcat.
Every help would be greatly appreciated.
Thank you!
As documented in the Why generated source folders are not added to classpath entry of the FAQ:
Maven plugins used to generate source
code from resources or other sources
can register additional source folders
to Maven project during the build.
Usually such plugins are bound to
process-resources (or
process-test-resources) build phase
(for example jaxb, modello or xdoclet
plugins). This means that to get those
source folders for generated sources,
we have to run corresponding Maven
build phase.
Not all projects using generated
sources, so for performance reasons,
m2eclipse does not run any Maven goals
by default on project import. This can
be changed in the Maven settings in
"Window > Preferences... > Maven >
Goals to run on project import" (e.g.
you can specify "process-resources"
build phase or specific plugins in
that field).
Alternatively you can run "Maven >
Update project configuration" action
from the project popup menu, which is
configured to run "process-resources"
by default and it can be also changed
on the same preference page.
So either add the goal to which the source generation process is bound to the list of goals to run on import or generate sources by running maven and update the project configuration.
Try using mvn eclipse:eclipse
Under the project where you have additionally generated source. When this is generated by maven it is normally under target folder.
Therefor eclipse:eclipse will recognize this and add as a source folder.
Rembember to refresh the project after this.
Use build-helper-maven-plugin (sample bellow) to tell Eclipse to add a generated folder to the build path :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/cxf</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Add this in the pom of each project that generates sources...

maven - use generics in a eclipse's maven project

I have a maven website project generated by archtype plugin with eclipse IDE. The problem is when i need to use generics or any thing of java 5 or 6, i changed the project compiler to 1.6 and i got a error mark on the project icon in the package explorer but everything is fine. I think it is cause by maven but I dont know how to get rid of it.
Thank you
Don't forget to configure the maven-compiler-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- Lock down plugin version for build reproducibility -->
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
I second with what Mike has said.
Even after changing the entries in pom, i was still getting the compiler error.
and it worked onlt after deleting the project and importing it again in the workspace
You need to make sure Eclipse is compiling with the Java 1.6.
Go to your project-specific settings: Project > Properties. Type "Java Compiler" in the text box to the left.
Ensure that all those drop-downs under "JDK Compliance" are set to 1.6. Otherwise check the box to 'Enable project-specific settings' and manually set them to JDK 1.6.
You may also want to enforce this through Windows > Properties, Java > Compiler, Compiler compliance level > 1.6, for global default settings.
Also make sure that the version of the JDK on your build path is the one you want. Hope this helps.
This is kludgy, but I've run into similar issues and worked around them by deleting the project (and not the sources obviously). Then I would import the maven project into my workspace.
m2eclipse ends up resolving the project again and sets up the project correctly.