Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Maven Essentials

You're reading from   Maven Essentials Get started with the essentials of Apache Maven and get your build automation system up and running quickly

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781783986767
Length 184 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Russell E Gold Russell E Gold
Author Profile Icon Russell E Gold
Russell E Gold
Prabath Siriwardena Prabath Siriwardena
Author Profile Icon Prabath Siriwardena
Prabath Siriwardena
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Apache Maven Quick Start FREE CHAPTER 2. Understanding the Project Object Model (POM) 3. Maven Archetypes 4. Maven Plugins 5. Build Lifecycles 6. Maven Assemblies 7. Best Practices 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>
You have been reading a chapter from
Maven Essentials
Published in: Dec 2015
Publisher:
ISBN-13: 9781783986767
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime