Social Media

Maven

Maven is a software project management tool. Its features include –

  • Build Tool
  • Dependency Management Tool
  • Deployment Tool
  • Documentation

It is also extensible thru its plugin architecture.

Archetypes
Archetypes are a good starting point for understanding maven. They are a templating system to allow you to quickly start your projects.

If you run –

mvn archetype:generate -DgroupId=com.glenware
                       -DartifactId=SampleWebApp
                       -DarchetypeArtifactId=maven-archetype-webapp
This will create the directory structure below –

SampleWebApp
   |-src
   |—main
   |—–resources
   |—–webapp
   |——-index.jsp
   |——-WEB-INF
   |———web.xml
   |-pom.xml

This gives a developer a good starting point, or if they prefer you can prepare the project to import into your favourite IDE

mvn eclipse:eclipse -Dwtpversion=2.0

The core of maven is the pom.xml –

[sourcecode language=”xml”] <project xmlns="<a href="http://maven.apache.org/POM/4.0.0">http://maven.apache.org/POM/4.0.0</a>" xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"
  xsi:schemaLocation="<a href="http://maven.apache.org/POM/4.0.0">http://maven.apache.org/POM/4.0.0</a> <a href="http://maven.apache.org/maven-v4_0_0.xsd">http://maven.apache.org/maven-v4_0_0.xsd</a>">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.glenware</groupId>
  <artifactId>SampleWebApp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SampleWebApp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>SampleWebApp</finalName>
  </build>
</project>
[/sourcecode]

The pom is highly configurable, as is shown here –

[sourcecode language=”xml”] <project xmlns="<a href="http://maven.apache.org/POM/4.0.0">http://maven.apache.org/POM/4.0.0</a>"
  xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"
  xsi:schemaLocation="<a href="http://maven.apache.org/POM/4.0.0">http://maven.apache.org/POM/4.0.0</a>
                      <a href="http://maven.apache.org/xsd/maven-4.0.0.xsd">http://maven.apache.org/xsd/maven-4.0.0.xsd</a>">
  <modelVersion>4.0.0</modelVersion></pre>
&nbsp;

  <!– The Basics –>
  <groupId>…</groupId>
  <artifactId>…</artifactId>
  <version>…</version>
  <packaging>…</packaging>
  <dependencies>…</dependencies>
  <parent>…</parent>
  <dependencyManagement>…</dependencyManagement>
  <modules>…</modules>
  <properties>…</properties>

  <!– Build Settings –>
  <build>…</build>
  <reporting>…</reporting>

  <!– More Project Information –>
  <name>…</name>
  <description>…</description>
  <url>…</url>
  <inceptionYear>…</inceptionYear>
  <licenses>…</licenses>
  <organization>…</organization>
  <developers>…</developers>
  <contributors>…</contributors>

  <!– Environment Settings –>
  <issueManagement>…</issueManagement>
  <ciManagement>…</ciManagement>
  <mailingLists>…</mailingLists>
  <scm>…</scm>
  <prerequisites>…</prerequisites>
  <repositories>…</repositories>
  <pluginRepositories>…</pluginRepositories>
  <distributionManagement>…</distributionManagement>
  <profiles>…</profiles>
</project>
[/sourcecode]

Big projects may have multiple poms, which can be linked – for example –

[sourcecode language=”xml”]     <parent>
        <groupId>org.sonatype.mavenbook.multispring</groupId>
        <artifactId>simple-parent</artifactId>
        <version>1.0</version>
    </parent>
[/sourcecode]

This produces an ‘effective-pom’ which can be seen –

mvn help:effective-pom

dependencies
dependencies in maven allow you to link your project to the correct versions of jar files required in your build.

For example if you need the servlet and jsp-api’s in your project you would add these dependencies –

[sourcecode language=”xml”]         <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
[/sourcecode]

To import the associated jars you run –

mvn install
     
Maven then has a search path –

  • Search log4j in Maven local repository
  • Search log4j in Maven central repository
  • Search log4j in Maven remote repository (if defined in pom.xml)

You can add extra repositories through –

[sourcecode language=”xml”]     
    <repositories>
       <repository>
           <id>java.net</id>
           <url>https://maven.java.net/content/repositories/public/</url>
       </repository>
    </repositories>
[/sourcecode]

You can also go offline with maven –

mvn dependency:go-offline

The bigger the pom means that you can have dependency conflicts.

These can be resolved thru –

mvn dependency:tree

mvn dependency:resolve

mvn dependency:resolve-plugins

mvn dependency:analyze – unused/undeclared dependencies
 plug-ins

[sourcecode language=”xml”] <build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <configuration>
             <maxmem>512m</maxmem>
         </configuration>
      </plugin>
   </plugins>
</build>
[/sourcecode]

Useful plugins –

 Execution Groups
Lifecycle – well recognised flow steps – mvn deploy
Phase – phase within a lifecycle
Plugin – Logical Grouping and distribution
Goal – Lowest unit – eg – mvn compile:compile jar:jar

 Built-in Maven Lifecycles
 mvn clean –
 
 pre-clean
 clean – Remove all generated/compiled attributes prior to build
 post-clean
 
 
default – 
 
 validate – Cross check all elements necessary for build are correct and present
 compile – Primary or mixed language resources
 test – Execute unit tests
 package – Bundle module as JAR, WAR or EAR
 verify – Inspect distribution packages
 install – Install to local repository
 deploy – Upload to remote repository
 
site – 
 
 pre-site – Similar to validate
 site – Generate site
 site-deploy – deploy to remote site
 
mvn install -Dmaven.test.skip=true – skip tests
 
Eclipse –

mvn eclipse:eclipse

  Default Goal

[sourcecode language=”xml”]  <project>
 …
    <build>
        <defaultGoal>install</defaultGoal>
    </build>
 …
 </project>
[/sourcecode]

 Profiles
Profiles allow you to create environment specific configurations – eg a test server vs a live server

mvn <yourgoal> -P YourProfile

mvn -Prelease-profile clean assembly:assembly
mvn -Pdemo clean assembly:assembly

 References
http://www.mkyong.com/maven/how-to-create-a-web-application-project-with-maven/

http://books.sonatype.com/mvnex-book

http://books.sonatype.com/mvnex-book/reference/appendix-license.html

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: