My linux world » Maven Survival Guide

Maven Survival Guide


Contents

Maven options

Before starting, let’s define maven options:

  export MAVEN_OPTS="-Xmx1024m -Xms1024m"

Settings.xml

To use an external repository, you have to do the followings:

* Define the profiles for the libraries and the plugins:

  <profile>
	<id>myProfileID</id>
	<activation>
		<activeByDefault>true</activeByDefault>
	</activation>
 
	<repositories>
		<repository>
			<id>myRepo1</id>
			<name>my 1st repository</name>
			<url>http://host/my1strepository/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>myRepo2</id>
			<name>my 2nd repository</name>
			<url>http://host/my2ndrepository/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>myRepo1</id>
			<name>my 1st repository</name>
			<url>http://host/my1strepository/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
		<pluginRepository>
			<id>myRepo2</id>
			<name>my 2nd repository</name>
			<url>http://host/my2ndrepository/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>
</profile>

* Define the proxy authentification informations

<proxies>
 
   (...)
 
   <proxy>
      <id>myRepo1</id>
      <active>true</active>
      <protocol>http</protocol>
      <username></username>
      <password></password>
      <host>host</host>
      <port>port</port>
      <nonProxyHosts></nonProxyHosts>
   </proxy>
 
   <proxy>
      <id>myRepo2</id>
      <active>true</active>
      <protocol>http</protocol>
      <username></username>
      <password></password>
      <host>host</host>
      <port>port</port>
      <nonProxyHosts></nonProxyHosts>
   </proxy>
 
   (...)
 
</proxies>

* Define the administration informations:

<servers>
 
   (...)
 
   <server>
      <id>myRepo1</id>
      <username></username>
      <password></password>
  </server>
 
  <server>
      <id>myRepo1</id>
      <username></username>
      <password></password>
  </server>
 
   (...)
</servers>

Creating an archetype

mvn archetype:create -DgroupId=<myGroupId> -DartifactId=<myArtefaktID>  -Dversion=<myInitialVersion> -Dpackaging=<myPackaging>
 -DarchetypeArtifactId=maven-archetype-webapp

* myGroupId: for example, com.company
* myArtefaktID : for example, myProjectName
* myPackaging: pom, jar, war

Example:

mvn archetype:create -DgroupId=org.mycompany -DartifactId=parent -Dversion=1.0.0-SNAPSHOT -Dpackaging=pom
 -DarchetypeArtifactId=maven-archetype-webapp

POM Parent

Reference all your common dependencies/plugins in the parent pom. In this way, it would be easier to manage them.

The pom parent file (copy all in your ‘pom.xml’ file):

 
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.mycompany</groupId>
	<artifactId>parent</artifactId>
	<packaging>pom</packaging>
	<version>1.0.0-SNAPSHOT</version>
	<name>parent</name>
	<url>http://maven.apache.org</url>
 
	<!-- Encoding sources in utf-8 -->
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
 
	<build>
		<!-- Plugin Management -->
		<pluginManagement>
			<plugins>
				<!-- Maven compiler -->
				<plugin>
					<artifactId>maven-compiler-plugin</artifactId>
					<configuration>
						<source>1.5</source>
						<target>1.5</target>
					</configuration>
				</plugin>
 
				<!-- war plugin -->
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-war-plugin</artifactId>
					<version>2.1</version>
					<configuration>
						<archive>
							<manifestEntries>
								<ProductName>${project.name}</ProductName>
								<FileDescription>${project.description}</FileDescription>
								<InternalName>${project.name}</InternalName>
								<ProductVersion>${project.version}.${buildNumber}</ProductVersion>
								<CompanyName>${project.companyname}</CompanyName>
								<LegalCopyright>Copyright (c) mycompany</LegalCopyright>
							</manifestEntries>
						</archive>
					</configuration>
				</plugin>
 
				<!-- jar plugin -->
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-jar-plugin</artifactId>
					<version>2.1</version>
					<configuration>
						<archive>
							<manifestEntries>
								<ProductName>${project.name}</ProductName>
								<FileDescription>${project.description}</FileDescription>
								<InternalName>${project.name}</InternalName>
								<ProductVersion>${project.version}.${buildNumber}</ProductVersion>
								<CompanyName>${project.companyname}</CompanyName>
								<LegalCopyright>Copyright (c)</LegalCopyright>
							</manifestEntries>
							<manifest>
								<addClasspath>true</addClasspath>
								<mainClass>org.mycompany.App</mainClass>
								<classpathPrefix>lib/</classpathPrefix>
							</manifest>
						</archive>
					</configuration>
				</plugin>
 
 
				<!-- eclipse plugin -->
				<plugin>
					<artifactId>maven-eclipse-plugin</artifactId>
					<version>2.4</version>
					<configuration>
						<additionalProjectnatures>
							<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
						</additionalProjectnatures>
						<additionalBuildcommands>
							<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
						</additionalBuildcommands>
						<downloadSources>true</downloadSources>
						<downloadJavadocs>true</downloadJavadocs>
						<wtpversion>1.5</wtpversion>
					</configuration>
				</plugin>
 
				<!-- release plugin -->
				<plugin>
					<artifactId>maven-release-plugin</artifactId>
					<version>2.1</version>
				</plugin>
 
				<!-- buildnumber plugin -->
				<plugin>
					<groupId>org.codehaus.mojo</groupId>
					<artifactId>buildnumber-maven-plugin</artifactId>
					<version>1.0-beta-4</version>
					<executions>
						<execution>
							<phase>validate</phase>
							<goals>
								<goal>create</goal>
							</goals>
						</execution>
					</executions>
					<configuration>
						<doCheck>false</doCheck>
						<doUpdate>false</doUpdate>
						<providerImplementations>
							<svn>javasvn</svn>
						</providerImplementations>
					</configuration>
				</plugin>
 
 
				<!-- To copy dependencies: use 'mvn dependency:copy-dependencies' -->
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-dependency-plugin</artifactId>
					<executions>
						<execution>
							<id>copy-dependencies</id>
							<phase>package</phase>
							<goals>
								<goal>copy-dependencies</goal>
							</goals>
							<configuration>
								<outputDirectory>${project.build.directory}/lib</outputDirectory>
								<overWriteReleases>false</overWriteReleases>
								<overWriteSnapshots>false</overWriteSnapshots>
								<overWriteIfNewer>true</overWriteIfNewer>
							</configuration>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
 
	<!-- Dependency Management -->
	<dependencyManagement>
		<dependencies>
			<!-- Log4j -->
			<dependency>
				<groupId>log4j</groupId>
				<artifactId>log4j</artifactId>
				<version>1.2.14</version>
				<optional>false</optional>
			</dependency>
			<!-- To parse XML -->
			<dependency>
				<groupId>jdom</groupId>
				<artifactId>jdom</artifactId>
				<version>1.1</version>
				<optional>false</optional>
			</dependency>
		</dependencies>
	</dependencyManagement>
 
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
 
	<!-- Reporting, use 'mvn site' -->
	<reporting>
		<plugins>
			<!-- Javadoc -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<version>2.7</version>
				<configuration>
					<minmemory>128m</minmemory>
					<maxmemory>512m</maxmemory>
				</configuration>
			</plugin>
			<!-- Quality code with checkstyle -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-checkstyle-plugin</artifactId>
				<version>2.5</version>
			</plugin>
			<!-- Quality code with pmd -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-pmd-plugin</artifactId>
				<version>2.5</version>
				<configuration>
					<linkXref>true</linkXref>
					<sourceEncoding>utf-8</sourceEncoding>
					<minimumTokens>100</minimumTokens>
					<targetJdk>1.5</targetJdk>
				</configuration>
			</plugin>
			<!-- Metrics -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>jdepend-maven-plugin</artifactId>
				<version>2.0-beta-2</version>
			</plugin>
			<!-- Identify which parts of your Java program are lacking test coverage -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>cobertura-maven-plugin</artifactId>
				<version>2.4</version>
			</plugin>
			<!-- Tags -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>taglist-maven-plugin</artifactId>
				<version>2.4</version>
			</plugin>
			<!-- Generate Surfire report -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-report-plugin</artifactId>
				<version>2.5</version>
			</plugin>
		</plugins>
	</reporting>
 
</project>

POM Child

Referencing pom parent

 
<project>
 
(...)   
 
  <parent>
    <groupId>org.mycompany</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>
 
(...)  
 
</project>

Use dependency Management

<project>
 
(...)   
 
  <dependencies>  
    ...
 
    <dependency>
        <dependency>
          <groupId>groupId</groupId>
          <artifactId>artifactId</artifactId>          
        </dependency>
    </dependency>
    ...
  </dependencies>
 
(...)  
 
</project>

Use plugin Management

<project>
  (...)
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>groupId</groupId>
        <artifactId>artifactId</artifactId>
      </plugin>
    </plugins>
    ...
  </build>
  (...)
</project>

Usefull commands

Install file in your local repository

  mvn install:install-file -DgroupId=mygroupId -DartifactId=myartifactId -Dversion=myversion -Dpackaging=mypackaging -Dfile=myfile -DgeneratePom=true

** packaging: pom, war, jar
** file: the pom/jar/war you want to install in you local repository
** generatePom: possibility to specify a pom.xml file with the argument ‘-f’

Install file in your remote repository

  mvn deploy:deploy-file -DgroupId=mygroupId -DartifactId=myartifactId -Dversion=myversion -Dpackaging=mypackaging -Dfile=myfile -DgeneratePom=true -Durl=myremoterepository -DrepositoryId=myrepositoryId

** packaging: pom, war, jar
** file: the pom/jar/war you want to install in you local repository
** generatePom: possibility to specify a pom.xml file with the argument ‘-f’

Build project

  mvn clean install

Generate a site

  mvn site

Use multi threads

  # add option -T x; x is number of threads
  mvn clean package -T 2

Non recursive

For example, if you only want to build multi-module parent, you can juste do :

mvn -N clean install

Usefull configuration

Timestamp

 
<project>
  (...)
  <properties>
    <maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
  </properties>
  (...)
</project>

Now you can use anywere the timestamp in your pom.xml like this:

${maven.build.timestamp}

Read more


Copyright © 2023 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.

Fork me on GitHub