Build Automation #11 - Mavenizing your project - part 5
Before you can start reading this part, kindly go through part 1 of Mavenizing your project. Just like rails, maven can help you in creating the initial structure of your project. Use the archetype plugin and it can create the initial project structure. The following command creates a HelloWorld project.
mvn archetype:create -DgroupId=com.compassites -DartifactId=helloworld
The archetype plugin has only one goal create and is used to create containers basically project templates. The second argument is a parameter (-D represents that we are going to supply a property) what the groupId would be and the third represents the artifactId.
Maven follows a file structure that has become the defacto and has been followed by the apache open source projects. You can look at the maven standard directory layout here. Although you could create your own structure, it is not recommended. When something has been tested and proven why not use it as it is rather than trying to define one on our own.
If you take a look at the files that Maven has created, you can see a pom.xml in the root of helloworld folder. If you look at the src\main\java you can see that Maven has by default create a java file called App.java which is basically a HelloWorld program. Note that it has already packaged it under com.compassites. Also you will find under src/test/main the junit test case. So by default we have the HelloWorld application without any effort. Lets try few more goals. Get into the helloworld folder where your pom is located. The following command compiles the java source.
mvn compile
and the following command runs the unit test.
mvn test
Now lets try packaging it. Note that like in Ant, the interdependent goals are executed automatically. The below command compiles, runs the test case and also creates the jar package.
mvn package
You can see that a file named HelloWorld-1.0-SNAPSHOT.jar has been created. As we said earlier a SNAPSHOT is a development version of the artifact. The version tag in the pom can be modified to reflect the version needed in case if it is a release build. OK now what if there are other projects which are dependent on this artifact?
mvn install
The above command will package the helloworld project and also create an artifact and store it in the local cache. Check it out in your user directory under .m2/repository/com.compassites/helloworld…
If any other project is depedent on the helloworld artifact, add the dependency in the project’s pom and maven picks it up from the local repository.
The archetype:create goal by default creates an artifact that can packaged into a jar. The below command helps in creating a war artifact.
mvn archetype:create archetype:create -DgroupId=com.compassites
-DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-webapp
There was one extra parameter there, which just says that we want a web application templage. Explore the structure that maven creates.









