Build New Maven Project

If you finished the Create New Maven Project tutorial, you will recall we went through the steps to create a brand new Maven project with Java. We can now go through the steps to package the project into a resulting executable jar file.


Define Maven Goal

In package explorer window in Eclipse, right click on your new maven project and select Run As » Run Configurations. In the popup window, click on Maven Build and Enter package. Click Run.


Maven Goals:

In the above step we picked package because that option will compiled the code and then package it the code into a single executable java jar file. The goals below, or phases, will determine how the project is built with Maven.

  • validate – validate the project is correct and all necessary information is available
  • compile – compile the source code of the project
  • test – test the compiled source code using a suitable unit testing framework
  • package – take the compiled code and package it into an executable JAR file
  • verify – run any checks on results of integration tests to ensure quality criteria are met
  • install – install the package into the local repository
  • deploy – copies the final package to the remote repository for sharing with other developers


Executable Jar File

After telling Maven to build this project, it will automatically generate a executable java jar file and place it in the project target folder.


Dependency Jars Auto Downloaded

If you recall our default Maven project included a single jar dependency (junit) in the pom.xml file. When we built this package, Maven automatically went out to the Maven Central Repository and downloaded the junit v3.8.1 package to your local .m2 directory. Recall the dependency in the pom.xml file for junit…

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency> 




Maven Package Console Output:

Asking Maven to package this project, the console window in Eclipse will generate the output that goes through the package process and if there is an error, you will see it on the screen. This project concluded with BUILD SUCCESS.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenProject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenProject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Admin\Documents\Workspace\MavenProject\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenProject ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenProject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Admin\Documents\Workspace\MavenProject\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenProject ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenProject ---
[INFO] Surefire report directory: C:\Users\Admin\Documents\Workspace\MavenProject\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.jcd.java.tutorials.MavenProject.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenProject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.311 s
[INFO] Finished at: 2018-01-15T05:27:12-05:00
[INFO] Final Memory: 9M/124M
[INFO] ------------------------------------------------------------------------


Summary

We have now seen two of the important features of a Maven built project. First the ability to include any dependency jar and version you need in the pom.xml file and Maven will automatically download it for you. Second is the automated generating of an executable jar file of your project.



References:

Add a Comment

Your email address will not be published. Required fields are marked *