Creating a simple maven project (java)

Maven is a very useful and a very capable tool, which is used to manage and build java based projects, making the developer’s life much more easier than it usually suppose to be. Once you instruct maven how to manage things related to the project―such as dependencies, maintenance of project related information and packaging preferences―it will take care of everything for you, having followed a standard way of building the product.
Once you instructed properly,
Maven takes care of the business―at a level of perfection.
Since we will be using 'Apache Maven' to build most of the applications in future posts, lets have a look into how could we create and package a simple java application using 'Maven'. There are so many IDE (Integrated Development Environment) tools available on the web such as Netbeans, that provides native Maven project support. However, in this example, I won’t be using any IDE for development, and we will only be using the 'Terminal' window and a text editor to create the Maven based Java application.

First of all, open the Terminal application on LINUX, and goto your own 'projects' directory.

Eg:
    cd ~/projects

Then execute the command below in order to get your maven java project skeleton generated.
    mvn archetype:generate -DgroupId=com.teachingbad -DartifactId=teaching-bad -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This 'mvn archetype:generate' command will generate the project directory structure and a few files inside those directories. Note the parameters that has been passed into.

-DgroupId=com.teachingbad
[This will be selected as the default java package (project's unique identifier)]

-DartifactId=teaching-bad
[Maven will create a directory with this name and all the files will be created inside this directory. And this will be the name of the *.jar archive file that is created ultimately]

-DarchetypeArtifactId=maven-archetype-quickstart
[This is an archetype name that instruct maven to create a simple java project. There are many archetypes available with maven, and you can pick it according to the nature of the project that you are trying to create. Eg : 'maven-archetype-webapp' for jsp based web application project.

-DinteractiveMode=false
[Unless you set this property to 'false'; maven will prompt you to enter project version and prompt for a confirmation on group id etc. If you set this to false, it will pick those information from a file called 'pom.xml', which has already been created by maven and available in your java project directory ( in this case, it is 'teaching-bad')]

With LINUX ‘tree’ command, you can list
the directory/file hierarchy.
When you execute the above command, it will create/build the new project and display that the build was successful.

Then list your directories in your projects directory, using 'ls' command or 'tree' (if having it installed).

Then you will see that there is new directory called 'teaching-bad' in it, which was created by maven with the above mentioned execution.

Also there will be child directories and files, inside that directory; those were created my maven.


Goto this directory (the your java project directory)

    cd teaching-bad

Open and observe the pom.xml file.

    gedit pom.xml

....
com.teachingbad
teaching-bad
jar
1.0-SNAPSHOT
....

Note the above section of the file. The groupId and the artifactId. The packaging attribute obviously indicates that it would create a *.jar archive file packaging the project, and the version attribute will be appended to the name of the *.jar file.

Then execute the command below and observe what is there.

    gedit src/main/java/com/teachingbad/App.java

This opens the basic java file which maven has just created, and it is nothing but just a Hello World java application.

    package com.teachingbad;
public class App{
public static void main( String[] args ){
System.out.println( "Hello World!" );
}
}

Then close the gedit window and execute the command below to build the java application, and create a *.jar archive.

    mvn package

When it indicates that the build was successful, another directory called 'target' will be created within the project directory, and you can find the relevant application package (teaching-bad-1.0-SNAPSHOT.jar) inside that directory. Now execute this command to run the java application.

java -cp target/teaching-bad-1.0-SNAPSHOT.jar com.teachingbad.App

Now it will print,

Hello World!

On the terminal console, which confirms that the application has been built successfully. In future articles, we possibly will build applications using maven, and then we will be discussing about other underlying concepts related to maven, such as '.m2' (maven) repository.

In the very next article, I was willing to discuss about the IDEs, that can be used for java based developments, with a brief explanation of matters that helped me while deciding what my favorite IDE is. But later, I decided not to dedicate a post for an IDE comparison, because it would create a debate rather than providing something useful to learn. IDE preference depends on the person and I have observed that people have their reasons to pick their IDE. For me Netbeans worked like no other IDE did; because, it provides out of the box Maven project support, and I am a developer who loves both Front-end and Back-end developments. In that case, I saw that Netbeans had good code intelli-sense(completion) support for most of the languages I use such as Java, PHP, HTML5, Javascript(including libraries).

However, if I decide to post something related to java based developments with Netbeans IDE, I am sure that Maven will make it's way to the spotlight again then: While we keep doing more and more developments, you will understand how does maven make your life―as a developer―is an easier one, just like I mentioned at the very beginning.

Comments