Saturday Aug 31

Key Concepts

Build Helper Maven Plugin

PDFPrintE-mail
Saturday, 30 April 2011 16:54
AddThis Social Bookmark Button

Maven Tomcat Deployment Tutorial   

Build Helper Maven Plugin 


If you try to add a resource folder expecting to add another Java source directory you will have a bad surprise generating your Eclipse project. By default your eclipse plugin will exclude the java code using the pattern **/*.java.  This is a case where the build helper Maven Plugin an help

Within Maven, there are a number of common tasks which plugins can perform to alter the current project for changes occurring during the build. It is possible to have a plugin generate new source code and include it for compilation like ANTLR, even though the directory is not included in the POM file. The role of the Build Helper plugin (build-helper-maven-plugin) is to provide a set of goals that can help achieve a collection of small but common tasks for which it would not be worth writing a custom plugin.

Add another Java source directory in your Maven project

Use this example to add more source directories to your project, since pom.xml only allows one source directory.

Include the following snippet in the <build><plugins> section of your project's pom.xml-file:

Maven plugin configuration

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.5</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>target/src/generated</source>
                ...
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

To regenerate your Eclipse project definition call the following Maven command

Maven additional source eclipse project creation

mvn eclipse:clean eclipse:eclipse

Attaching licence artifact

Another usage of the build-helper-manve-plugin is the generation of additional artifacts that need to be attached to the build process. This means they use the same POM to define them, but are different types of related build artifacts, with their own classifier. The artifacts are installed and deployed to the repository alongside the original artifact.

Typically, this will be in the form of another JAR file, possibly generated by one of the scripting plugins that did not attach the artifact itself.

However, it could be used for any number of files that need to be stored in the repository alongside the main artifact. Consider the example of deploying the license to the repository—if you were to run the install phase on the given project, you would be able to have the license installed into the local repository alongside the main artifact and its POM.

In reality, this particular configuration may be overkill, especially if the licenses are identical across many projects, or can be derived from the POM. However, depending on your deployment needs this possibility can be helpful in ensuring the repository contains the information about an artifact that you need, at the time it was deployed, in addition to any extra build artifacts that might be generated.

Include the following snippet in the <build><plugins> section of your project's pom.xml-file:

Maven plugin configuration

<meta charset="utf-8" />
<plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>build-helper-maven-plugin</artifactId> 
     <version>1.1</version> 
     <configuration> 
         <artifacts> 
           <artifact> 
               <file>target/maven-shared-archive-resources/LICENSE</file> 
               <type>txt</type>  
               <classifier>license</classifier> 
           </artifact> 
         </artifacts> 
      </configuration> 
      <executions>
         <execution>
            <goals>
               <goal>attach-artifact</goal>
            </goals>
         </execution>
        </executions>
</plugin>

This goal will be executed after the packaging has occurred, but before installation so that it can be attached to the installation (and deployment) process. The file to attach is the license file generated earlier by the Remote Resources plugin with an extension of .txt and classifier of -license.

When running the install phase if the JSF  lifecycle, we now see the file being processed:

Other build helper features

The  build-helper-maven-plugin lets you do some other handy things such as purge the project’s artifacts from the local repository and add additional testSource folders.

Project Web site 

You can find at codehaus Build-Helper-Maven-Plugin more information about the supported goals.

 Other Maven Plugins : Maven Plugin List

Tags: maven, build, eclipse, project, plugin, plugins, source, repository, artifact

Add comment


Security code
Refresh

Java Tutorial on Facebook