Discussing the nuts and bolts of software development

Friday, September 18, 2009

 

Maven Compiler Tips and Tricks

Apache’s Maven is a great tool for managing a build environment: it keeps track of all project dependencies and provides a number of configurable build phases which can add depth to the build process. Building a project from the ground-up with Maven is a sure-fire way to keep it well organized and easy to maintain – but what about adding Maven on to an existing project, or worse, merging a non-Maven project into a project that already relies on Maven?

What follows is a look at some of the lessons I’ve learned from tweaking compiler plug-ins and digging through search results to debug various Maven-related issues. Hopefully it will be useful to the next developer who happens to hit similar issues, and if you have tips of your own be sure to leave a comment.

The maven-compiler-plugin <include> property

When overriding the default maven-compiler-plugin, the <include> tag may be used to force the compiler to include extra files into the build. There are a couple of interesting points to note here:

It is a filter. Many things in Maven expect a path to a directory, but not the <include> tag. If you have some extra java classes in src/main/java and you pass that to <include> it will fail silently – what you actually want is src/main/java/**/*.java.

It is for the compile-phase only. By default, in addition to using the maven-compiler-plugin during the compile phase, Maven will also use it during the test-compile phase. Most properties will apply to both, but <include> is not one of them; the test-compile phase requires a separate property, <testIncludes>, for any includables it requires[1].

The generate-sources and generate-resources phases

These phases are great for adding source (.java) and resource (.class) files to the compiler before the compile phase occurs. The snippet below shows how to use mojo's build-helper-plugin to add some obscure .class files to the classpath:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>../obscure/classes</directory>
</resource>
</configuration>
</execution>
</executions>
</plugin>

Now any .class files in ../obscure/classes (or any subdirectories) will be added to the classpath.

The same plug-in is used for adding .java files, simply tweak the generate-resources and add-resource values to generate-sources and add-source, and the <resources> and <resource> properties to <sources> and <source>.

The maven-compiler-plugin <compilerArgument> property

The <complierArgument> tag may be used to pass command-line arguments directly to the java compiler. Two examples:

<classpath> seems like it would let you add resources to the classpath, but near as I can tell, these aren’t actually used. Maven seems to prefer managing its own classpath, though we can still add/remove entries by overriding the default generate-resources phase (as explained above).

<sourcepath> is a great way to specify multiple source directories for compilation. It takes a semicolon-delimited list of top-level directories containing java files to compile. Alternately, an override of the generate-sources phase may be used here as well.

One gotcha with regards to sourcepath: to get this to work, I had to manually set <fork> to true on the maven-compiler-plugin. In fact, this gets even worse: when <fork> is true, Maven will use the %PATH% environment variable to determine which JRE to use, and this will fail with a totally non-descript error if the path to your JRE contains any spaces – very annoying to track down. This is actually a bug in Maven, logged here (http://jira.codehaus.org/browse/MCOMPILER-30).

The <sourceDirectory> property

A minor but important tag when playing around with various sources and resources, the <sourceDirectory> tag may be used to set the base directory for including java source files. It defaults to src/main/java, but I found when playing around with a lot of sources spread around various directories, it was easiest to set it to the current directory as follows:

<build>
<sourceDirectory>.</sourceDirectory>
{...}
</build>

Other useful debugging hints

When running into problems, it’s always good to have a few debug flags around to get a little more information out of Maven, which is generally not great at telling you what might be wrong.

Specifying the -e flag while running Maven will print out any exceptions Maven encounters, with the corresponding stack trace.

The <verbose> tag may be added inside any plug-in’s <configuration> property and when toggled to true (default is false) it will print some extra information, including the sourcepath and classpath being used by Maven’s compiler.

Specifying the -X flag while running Maven will document all kinds of intermediate steps Maven takes during the build – much more than -e and <verbose>.



[1] This makes perfect sense, of course: it’s unlikely that you’ll want the same includables for both the normal compiler and the testing compiler. It’s just counter-intuitive compared to the rest of the <configuration> properties.

Labels: , ,


Comments:
After visiting https://homeworkhelper.net/blog/homework-cheats you will learn more about homework cheats. It will help you to achieve success
 
These tips were very helpful as they got the job done. In addition, to read about the mortgage rates san Antonio you must read this here.
 
Thank you for sharing this amazing valuable information.
 
Building a venture from the floor-up with maven is a positive-fire manner to maintain it nicely prepared and clean to hold – however what about including maven on to a present mission, or worse, merging a non-maven venture right into a venture that already relies on maven? Cold Heading parts | Fasteners suppliers | Customized automotive bushing
 
Heater Installation Services Fort Worth TX systems that keep your home heating supplies perfect all year long. Maintenance plays a key role in ensuring that your heating system stays running smoothly and save your investment.
 
Its great opportunity to get know about, What is Google My Business.
 
Zhongshan Rami trading co. ltd established in 2018. Our production line includes machines and techniques such as silkscreen, film, etc. We are known as the best beauty packaging suppliers in China by the quality and well served.
 
Hey guys! I would love to recommend essay helper
you a very nice service for essay and homework! You can use it and enjoy! Because this guys really know how to do it! Good luck and have fun!
 
Wish you all the best for your new upcoming articles and kindly write on topic Ecommerce website and also on Web design thanks.
 
Struggling to Do My Dissertation for your dissertation? I offer expertise in qualitative data analysis software and can assist you in uncovering meaningful insights. (mydissertations.co.uk)
 
Thanks For these Tips and Tricks Its very useful for me and others.
 
It keeps track of all project dependencies and has a variety of build phases that may be configured to add depth to the build process. Building a project from the ground up using Maven ensures that it will be well organized and easy to manage - but what about adding Maven to an existing project, or worse, merging a non-Maven project into a project?The preceding is a really interesting and timely post. I appreciate the time you spent writing this. You must have done a lot of research and distillation to develop your work. The information examples and facts are quite useful. I'm convinced that many readers will find this content beneficial. Continue your fantastic effort! I'm excited to see more amazing factoids from you.
 
The source Directory tag is a small but crucial tag that can be used to establish the base directory for incorporating Java source files while experimenting with different sources and resources. It goes to src/main/java by default, but after experimenting with a number of sources dispersed across different directories, I discovered that setting it to the current directory as follows worked the best. I appreciate you giving your knowledge! Continue your fantastic effort! Keep sharing. I invite you to browse my website.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?