Book Image

Maven Essentials

By : Russell E Gold, Prabath Siriwardena
5 (1)
Book Image

Maven Essentials

5 (1)
By: Russell E Gold, Prabath Siriwardena

Overview of this book

Maven is the #1 build tool used by developers and it has been around for more than a decade. Maven stands out among other build tools due to its extremely extensible architecture, which is built on of the concept of convention over configuration. It’s widely used by many open source Java projects under Apache Software Foundation, Sourceforge, Google Code, and more. Maven Essentials is a fast-paced guide to show you the key concepts in Maven and build automation. We get started by introducing you to Maven and exploring its core concepts and architecture. Next, you will learn about and write a Project Object Model (POM) while creating your own Maven project. You will also find out how to create custom archetypes and plugins to establish the most common goals in build automation. After this, you’ll get to know how to design the build to prevent any maintenance nightmares, with proper dependency management. We then explore Maven build lifecycles and Maven assemblies. Finally, you will discover how to apply the best practices when designing a build system to improve developer productivity.
Table of Contents (15 chapters)
Maven Essentials
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

Hello Maven!


The easiest way to get started with a Maven project is to use the generate goal of the archetype plugin to generate a simple Maven project. Maven archetypes are discussed in detail in Chapter 3, Maven Archetypes, and plugins are covered in Chapter 4, Maven Plugins.

Let's start with a simple example:

$ mvn archetype:generate 
            -DgroupId=com.packt.samples  
            -DartifactId=com.packt.samples.archetype 
            -Dversion=1.0.0 
            -DinteractiveMode=false

This command will invoke the generate goal of the Maven archetype plugin to create a simple Java project. You will see that the following project structure is created with a sample POM file. The name of the root or the base directory is derived from the value of the artifactId parameter:

com.packt.samples.archetype 
               |-pom.xml
               |-src
               |-main/java/com/packt/samples/App.java
               |-test/java/com/packt/samples/AppTest.java    

The sample POM file will only have a dependency to the junit JAR file with test as the scope:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.packt.samples</groupId>
  <artifactId>com.packt.samples.archetype</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0</version>
  <name>com.packt.samples.archetype</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>
</project>

The generated App.java class will have the following template code. The name of the package is derived from the provided groupId parameter. If you want to have a different value as the package name, then you need to pass this value in the command itself as -Dpackage=com.packt.samples.application:

package com.packt.samples;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

To build the sample project, run the following command from the com.packt.samples.archetype directory, where the pom.xml file exists:

$ mvn clean install