Calling Java classes from the Maven build lifecycle
I love Maven for its flexibility, but this flexibility at times makes it hard to figure out how to achieve certain tasks. In my case I wanted to execute a certain Java class that is included in the project at some point during the build lifecycle. While I can see many uses in my case the class itself compiles a SQL dump out of several source files so it can be included in the final artifact.
Integrating with Maven’s build lifecycle
Long story short, here is what you need to add to your
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2</version> <executions> <execution> <id>build-dump</id> <phase>process-classes</phase> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>com.example.BuildDump</mainClass> </configuration> </plugin> </plugins> </build> <pluginManagement>
org.codehaus.mojo.exec-maven-plugin is used to execute the class
com.example.BuildDump. The execution is done in the
process-classes lifecycle using the java goal of the plugin. The
id is arbitrary but has to be unique.
The whole idea of this snippet is to hook certain plugin’s goal into maven’s lifecycle at a certain phase. In that sense, the same effect can be achieved by calling (on the command line)
mvn exec:java -DmainClass=com.example.BuildDump
As you can see the goal in the execution of the
pom.xml corresponds to the part after the colon, the part before the colon references the plugin (
plugin:goal). If I recall correctly
org.codehaus.mojo.*-maven-plugins have a shorthand name which is the part before
The execution doesn’t have to take place in the
process-classes phase but it has to take place after the
compile phase so the class has already been compiled. It also has to take place before the
test phase if you need the files during testing. I do not know if the
process-classes phase is correct (it is used to post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.) but it fulfills these two requirements.
I love Maven for its flexibility, but this flexibility at times makes it hard to figure out how to achieve certain tasks. In my case I wanted to execute a certain Java class that is included in the project at some point during the build lifecycle. While I can see many uses in my ...
|Print article||This entry was posted by georg on January 4, 2011 at 7:56 pm, and is filed under java. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site.|
No comments yet.
No trackbacks yet.
about 1 year ago - No comments
I’m currently integrating JMS (Java Messaging Service) into a project. Being a true enterprise component it has a very complex nomenclature and consists itself of many modules to cover all kinds of topologies. To aid other newcomers here is an overview of what I’ve learned so far.
The Java Messaging Service is an official part …
about 2 years ago - No comments
This is sort of a continuation of a previous post about how to integrate your own Java classes into the Maven build lifecycle. This entry described how to generate files for inclusion in the final project artifact (ie. .jar, .war etc.). It took me some time to figure out how to access these files …
about 3 years ago - No comments
I am currently working on a Spring-based Neo4j (a graph database) application and needed to add user authentication. Since Spring Security allows integrating custom authentication providers it felt only natural to implement a provider based on Neo4j. A data structure to support users and groups is already provided in the Neo4j Wiki. …
about 3 years ago - 2 comments
Spring 3 allows for wonderfully simply forms and automatic binding of form values back to the command object. However if your command object has further nested objects that you want to make editable from the form as well things get a little tricky. In that case you can’t just use a standard JSTL forEach …
about 3 years ago - 2 comments
Thanks to Spring’s new MVC Annotations (@Controller, @RequestMapping, @RequestParam etc.) unit testing a Spring controller is now easier than ever before. Still, I had a hard time figuring out how to do this since most of your controller methods will not take a ServletRequest as input. Instead they use a form-backing / command object, …