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
Apache Maven Cookbook

You're reading from   Apache Maven Cookbook Over 90 hands-on recipes to successfully build and automate development life cycle tasks following Maven conventions and best practices

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher Packt
ISBN-13 9781785286124
Length 272 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started 2. IDE Integration with Maven FREE CHAPTER 3. Maven Lifecycle 4. Essential Maven Plugins 5. Dependency Management 6. Code Quality Plugins 7. Reporting and Documentation 8. Handling Typical Build Requirements 9. Multi-module Projects 10. Java Development with Maven 11. Advanced Maven Usage Index

Building a simple project with Maven

Let us now build the project that was created in the preceding section.

How to do it...

To build the previously created simple project with Maven, perform the following steps:

  1. Open the command prompt and run the following command, changing the directory to the folder the project was created:
    mvn package
    
  2. Observe the following things in the output:

    Notice the following warning (we will see how to resolve this later in this book):

    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ simple-project ---
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
    i.e. build is platform dependent!
    

    Check if the sources are compiled:

    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ simple-project
    

    Check if the tests are run:

    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ simple-project ---
    [INFO] Surefire report directory: C:\projects\apache-maven-cookbook\simple-project\target\surefire-reports
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.packt.cookbook.AppTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
    
  3. A JAR file is now created.

How it works...

In the mvn package command, the package parameter is a phase in the build lifecycle. Maven has a default build lifecycle that has a number of phases. Each phase will execute every phase prior to it in order along with the specified phase. In this case, the package phase executes in the following order:

  • Validate
  • Compile
  • Test
  • Package

The validate phase makes sure that the project (specifically the pom.xml file that describes the project) is in order and all the necessary information to run the project is available.

The compile phase compiles the sources.

The test phase compiles the test sources and then runs the test using a suitable test framework. In the earlier example, the JUnit framework is used to run the tests.

The package phase packages the artifacts to the format specified in the pom.xml file.

lock icon The rest of the chapter is locked
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