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

Convention over configuration


Convention over configuration is one of the main design philosophies behind Apache Maven. Let's go through a few examples.

A complete Maven project can be created using the following configuration file (pom.xml):

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.packt</groupId>
  <artifactId>sample-one</artifactId>
  <version>1.0.0</version>
</project>

Note

The Maven POM file starts with the <project> element. Always define the <project> element with the schema. Some tools can't validate the file without it:

<project xmlns=http://maven.apache.org/POM/4.0.0
         xmlns:xsi=………
         xsi:schemaLocation="…">

The pom.xml file is the heart of any Maven project and is discussed in detail in Chapter 2, Understanding the Project Object Model (POM). Copy the previous configuration element and create a pom.xml file out of it. Then, place it in a directory called chapter-01, and then create the following child directories under it:

  • chapter-01/src/main/java

  • chapter-01/src/test/java

Now, you can place your Java code under chapter-01/src/main/java and test cases under chapter-01/src/test/java. Use the following command to run the Maven build from where the pom.xml is:

$ mvn clean install

This little configuration that you found in the sample pom.xml file is tied up with many conventions:

  • Java source code is available at {base-dir}/src/main/java

  • Test cases are available at {base-dir}/src/test/java

  • The type of the artifact produced is a JAR file

  • Compiled class files are copied to {base-dir}/target/classes

  • The final artifact is copied to {base-dir}/target

  • http://repo.maven.apache.org/maven2, is used as the repository URL.

If someone needs to override the default, conventional behavior of Maven, then it is possible too. The following sample pom.xml file shows how to override some of the preceding default values:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.packt</groupId>
  <artifactId>sample-one</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <build>    
    <sourceDirectory>${basedir}/src/main/java</sourceDirectory>              
    <testSourceDirectory>${basedir}/src/test/java               
                                         </testSourceDirectory>     
    <outputDirectory>${basedir}/target/classes
                                             </outputDirectory>     
  </build>
</project>