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:
- Open the command prompt and run the following command, changing the directory to the folder the project was created:
mvn package
- 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
- 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.