Friday, January 25, 2013

Maven Tips


  •  skip test while building
<!-- maven plugin to skip test while building -->
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
         <skipTests>true</skipTests>
      </configuration>
</plugin>

  •  dependency management
in the pom.xml of parent module, use <dependencyManagement>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

in the pom.xml of individual modules,

<dependencies>
<dependency>
<groupId>junit</groupId>
   <artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

It is not necessary to specify version, let parent pom.xml to manage the version. If there is any change in parent pom, all sub-modules will be changed as well.

and also need to add this element to import above dependencies in the individual modules' pom.xml

<dependencyManagement>
<dependencies>
<dependency>
<groupId>{parent groupId}</groupId>
<artifactId>{parent artifactId}</artifactId>
<version>{parent version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


  • source encoding
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>



  • copy generated jar file to another folder
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-jar</id>
<!-- here the phase you need -->
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/dist/lib</outputDirectory>
<resources>
<resource>
<directory>target</directory>
<includes>
<include>*.jar</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>




  • build for multiple environment
1) define envs

<profiles>
<profile>
<id>prod</id>
<properties>
<package.environment>prod</package.environment>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<package.environment>dev</package.environment>
</properties>
</profile>
<profile>
<id>sit</id>
<properties>
<package.environment>sit</package.environment>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<package.environment>uat</package.environment>
</properties>
</profile>
</profiles>

2) specify default env if build

<properties>
<package.environment>dev</package.environment>
</properties>



3) copy files based on env


<build>
<plugins>
<!-- maven plugin to skip test while building -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- copy files based on different environments -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/dist/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/${package.environment}</directory>

                                                                        <includes>
<include>log4j.properties</include>
<include>hibernate.xml</include>
</includes>

<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

if copy jar files,etc,  TAKE NOTE <filtering>false</filtering>
4) command to build for sit env:
 mvn clean install -P sit

No comments:

Post a Comment