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
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Apache Maven 3

You're reading from   Mastering Apache Maven 3 Enhance developer productivity and address exact enterprise build requirements by extending Maven

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher
ISBN-13 9781783983865
Length 298 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. Apache Maven Quick Start FREE CHAPTER 2. Demystifying Project Object Model 3. Maven Configuration 4. Build Lifecycles 5. Maven Plugins 6. Maven Assemblies 7. Maven Archetypes 8. Maven Repository Management 9. 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 code snippet in pom.xml file:

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

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Note

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

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

Copy the previous configuration element and create a pom.xml file out of it. Then, place it in a directory called chapter-01 and 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:

$ mvn clean install

This little configuration is tied up with many conventions:

  • The Java source code is available at {base-dir}/src/main/java
  • Test cases are available at {base-dir}/src/test/java
  • A JAR file type of artifact is produced
  • Compiled class files are copied into {base-dir}/target/classes
  • The final artifact is copied into {base-dir}/target
  • The linkhttp://repo.maven.apache.org/maven2is used as the repository

If someone needs to override the default, conventional behavior of Maven, that 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
Mastering Apache Maven 3
Published in: Dec 2014
Publisher:
ISBN-13: 9781783983865
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
Banner background image