Maven best practices

Ref book

The Maven 2 POM demystified
Maven: The Definitive Guide
simple examples

excellent tutorial

good short tutorial about manifest and jar lib directory
bundle all dependencies automatically
bundle dependencies manually

matching pattern

pattern

Use dependency management for multi module projects.

http://www.bhaskarvk.info/blog/2007/06/maven-best-practices-use-dependency-management-for-multi-module-projects

How to define different profiles

http://www.sonatype.com/books/maven-book/reference/profiles-sect-tips-tricks.html

profiles can be defined in project pom.xml file, profiles.xml in the same directory of pom.xml, settings.xml

resource filtering

<build>
  <resources>
   <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
   </resource>
  </resources>
 </build>
</project>

note, If you had a src/main/resources/images that you didn't want to be filtered, then you would create one resource entry that handled the filtering of resources with an exclusion on the resources you wanted unfiltered. In addition you would add another resource entry, with filtering disabled, and an
inclusion of your images directory. The build element would look like the following:

<project>
...
 <build>
  <resources>
   <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <excludes>
     <exclude>images/**</exclude>
    </excludes>
   </resource>
   <resource>
    <directory>src/main/resources</directory>
    <includes>
     <include>images/**</include>
    </includes>
   </resource>
  </resources

property define in project pom.xml

<properties>
  <my.filter.value>hello</my.filter.value>
 </properties>
</project>

then refer it like ${my.filter.value} in resource file : application.properties file as follows:

# application.properties
application.name=${project.name}
application.version=${project.version}
message=${my.filter.value}

property defined in external file

First, create an external properties file and call it src/main/filters/filter.properties:

# filter.properties
my.filter.value=hello!

Next, add a reference to this new file in the pom.xml file:

[...]
 <build>
  <filters>
   <filter>src/main/filters/filter.properties</filter>
  </filters>
  <resources>
   <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
   </resource>
  </resources>
 </build>
[...]

property defined in command line

Filtering resources can also retrieve values from system properties; either the system properties built into Java (like java.version or user.home), or properties defined on the command line using the standard Java -D parameter.

mvn process-resources "-Dmy.filter.value=hello!"

Maven Exec plugin

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <configuration>
    <executable>java</executable>
    <arguments>
      <argument>-jar</argument>
      <argument>${geronimo.home}/bin/deployer.jar</argument>
      <argument>--user</argument>
      <argument>system</argument>
      <argument>--password</argument>
      <argument>manager</argument>
      <argument>deploy</argument>
      <argument>
        ${project.build.directory}/${project.build.finalName}.ear
      </argument>
      <argument>
        ${basedir}/src/main/deployment/geronimo/plan.xml
      </argument>
   </arguments>
 </configuration>
</plugin>

check effective pom.

mvn help:effective-pom

help for a plugin

mvn help:describe -Dplugin=assembly -Dfull

couple ways to use local library

system scope

this approach doesn't work if any project depends on the impl project.

1. in business implementation project:

        <dependency>
            <groupId>com.mytest.services</groupId>
            <artifactId>
                XYZService-impl
            </artifactId>
            <version>1.0.0</version>
            <scope>system</scope>
             <systemPath>${basedir}/lib/XYZService-impl-1.0.0.jar</systemPath> 
        </dependency>

2. copy libraries in phase:

<build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/xyzwebserv-war-1.0-SNAPSHOT/WEB-INF/lib</outputDirectory>
              <resources>          
                <resource>
                  <directory>${basedir}/../../lib</directory>
                  <filtering>false</filtering>
                </resource>
                <resource>
                  <directory>${basedir}/../../domain/lib</directory>
                  <filtering>false</filtering>
                </resource>
                <resource>
                  <directory>${basedir}/lib</directory>
                  <filtering>false</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

copy to maven local cache repository (.m2)

        <plugins> 
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>install-library-XYZService-impl</id>
                        <!-- note, you can change the phase to validate or whatever valid phases, so you can bind 
                             plugin goal to a particular phase you are interested           -->          
                        <phase>install</phase>                                 
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <groupId>com.mytest.services</groupId>
                            <artifactId>XYZService-impl</artifactId>
                            <version>1.0.0</version>
                            <packaging>jar</packaging>
                            <file>${basedir}/../../lib/XYZService-impl-1.0.0.jar</file>
                        </configuration>
                    </execution>
                    <execution>
                        <id>install-library-ABC</id>
                        <phase>install</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <groupId>com.mytest.services</groupId>
                            <artifactId>ABC</artifactId>
                            <version>1.0.0</version>
                            <packaging>jar</packaging>
                            <file>${basedir}/../../domain/lib/ABC-1.0.0.jar</file>
                        </configuration>
                    </execution>
                </executions>    
            </plugin>
        </plugins>

local maven repository

not working for me

   <repositories>  
         <repository>  
             <id>mylocal</id>  
             <url>file:///${basedir}/repository</url>  
         </repository>  
    </repositories>

found the reason, because my maven settings.xml has mirror of *, it will mask out all repositories.

<mirrors>
    <mirror>
      <id>xyz</id>
      <mirrorOf>*</mirrorOf>
      <url>http://repo.xyz.com/artifactory/repo</url>   <!-- or like  file:///c:/Workspaces/current/local  -->
    </mirror>
  </mirrors>

if I change the * to central, it will work. (the ID of the main Maven repository included by default is central)

http://maven.apache.org/guides/mini/guide-mirror-settings.html

build in maven properties

http://www.sonatype.com/books/maven-book/reference/resource-filtering-sect-properties.html

sample

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.surasint.test</groupId>
    <artifactId>maven01</artifactId>
    <version>1.0-SNAPSHOT</version>
<build>
 
<pluginManagement>
         <plugins>
             <plugin>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>exec-maven-plugin</artifactId>
                 <version>1.5.0</version>
             </plugin>
         </plugins>
     </pluginManagement>
 
 <plugins>
 
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
      <execution>
        <id>exec1 </id>
        <phase>pre-clean</phase>
        <goals>
           <goal>exec</goal>
        </goals>
        <configuration>
          <executable>pre-exec.sh</executable>
          <workingDirectory>${project.basedir}</workingDirectory>
          <arguments>
          </arguments>
        </configuration>
      </execution>
 
      <execution>
        <id>exec2</id>
        <phase>post-clean</phase>
        <goals>
           <goal>exec</goal>
        </goals>
 
        <configuration>
          <executable>post-exec.sh</executable>
          <workingDirectory>${project.basedir}</workingDirectory>
        </configuration>
      </execution>
 
       <execution>
        <id>exec3</id>
        <phase>install</phase>
        <goals>
           <goal>exec</goal>
        </goals>
 
        <configuration>
          <executable>mvn</executable>
          <workingDirectory>${project.basedir}</workingDirectory>
          <arguments>
                    <argument>exec:exec@exec1</argument>
                    <argument>exec:exec@exec2</argument>
          </arguments>
        </configuration>
      </execution>
 
      <execution>
        <id>exec4</id>
        <phase>install</phase>
        <goals>
           <goal>exec</goal>
        </goals>
 
        <configuration>
          <executable>cp</executable>
          <workingDirectory>${project.basedir}</workingDirectory>
          <arguments>
                    <argument>pom.xml</argument>
                    <argument>/tmp</argument>
          </arguments>
        </configuration>
      </execution>
    </executions>
  </plugin>
 
 <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>whatever</id>
                        <phase>pre-clean</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>pre-clean called</echo>
                            </tasks>
                        </configuration>
                    </execution>
                    <execution>
                        <id>whatever2</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>clean called</echo>
                            </tasks>
                        </configuration>
                    </execution>
                    <execution>
                        <id>whatever3</id>
                        <phase>post-clean</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>post-clean called</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
 
            </plugin>
 
       </plugins>
    </build>
 
</project>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License