When I was first introduced to maven, I was introduced as a build tool, an alternative to ant build which I was using for my SoapUI project builds. However, once I dwell deeper into maven and built my interest because of the wide range of features supported by Maven, only then I realized that maven is much more than just a build tool. It is a complete project management tool which allows the developers to have synergy in the project structure. It provides a defined structure to the project which allows developers to easily understand and help in case there is any transition on the project. The new developers who are familiar with maven structure can easily determine the class file, resources etc on the project.
In this post I am going to start with creating a simple maven project and then in later part I will explain how you can create a complex project in maven with multiple modules.
You can create a simple project with below command (here I am assuming that you already have configured JDK and Maven).
mvn archetype:generate -DgroupId=myProject.myCompany.com-DartifactId=myProject-DarchetypeArtifactId=maven-archetype-quickstart-DinteractiveMode=false
the above command will create a maven project structure where you will have
- pom.xml : This is the main and single configuration file that contains majority of the information used by maven to build the project.
- Directory structure as :
- src -
- main -
- java -
- myProject -
- myCompany -
- com -
- App.java : It is the class file
- test -
- java -
- myProject -
- myCompany -
- com -
- App.java : It is the JUnit test class file for testing the class created by maven automatically.
mvn installAbove command will compile your source code, perform test and then create a jar package in the directory as below :
- target
- myProject-1.0-SNAPSHOT.jar
- classes
- myProject
- myCompany
- com
- App.class
- maven-archiver
- pom.properties
- maven-status
- maven-compiler-plugin
- compile
- default-compile
- createdFiles.lst
- inputFiles.lst
- testCompile
- default-testCompile
- createdFiles.lst
- inputFiles.lst
- surefire-reports
- myProject.myCompany.com.AppTest.txt
- TEST-myProject.myCompany.com.AppTest.xml
- test-classes
- myProject
- myCompany
- com
- AppTest.class
As you can see that maven has compiled and packaged the project into a jar file (if it is web project then a war will be generated). You can run your package with following command from your project directory
java -cp target/myProject-1.0-SNAPSHOT.jar myProject.myCompany.com.AppSo, as you can see in the above example that it is very easy to create a maven project. Now we look into the pom.xml file that has been generated by maven
Hello world!
Following element in the pom file together are known as co-ordinates of the projct
- groupId
- artifactId
- packaging
- version
<packaging>pom</packaging>Now your master project is almost ready and you can create the modules inside using the similar command as you used for creating the master project file.
mvn archetype:generate -DgroupId=myProject.myCompany.com-DartifactId=module1-DarchetypeArtifactId=maven-archetype-quickstart-DinteractiveMode=false
The above two commands will create two modules or child projects within the master project and you will realize that the pom file inside the master project is updated to have below elementsmvn archetype:generate -DgroupId=myProject.myCompany.com-DartifactId=module2-DarchetypeArtifactId=maven-archetype-quickstart-DinteractiveMode=false
And the child project will have reference to the master project as below
For a complex project you only need to compile the master project and child projects will be launched on their own.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] myProject .......................................... SUCCESS [ 0.757 s]
[INFO] module1 ............................................ SUCCESS [ 5.211 s]
[INFO] module2 ............................................ SUCCESS [ 0.748 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.909 s
[INFO] Finished at: 2017-03-26T19:26:54+05:30
[INFO] Final Memory: 15M/37M
[INFO] -----------------------------------------------------------------------