Thursday, 13 July 2017

Getting started java project with maven

To begin a development of  java project managed with maven, you need two mandatory requirement: the Java Development Kit (JDK) and Apache Maven tool both installed. An onother optional requirement but I think it indispensable the usage of an Integrated Development Environment (IDE) tool as Eclipse. 

Installing JDK

If you don't already installed the JDK than you can install the Oracle JDK available in this web page

Installing Apache Maven

You can refer the official guide available in this web page. If you use an ubuntu OS as the virtual machine created in previous post than you can install the latest maven version using the following commands:
To get all the available Maven package.
$ apt-cache search maven
To install the latest Maven version.
$ sudo apt-get install maven
Check post installation.
$ mvn -v
If the installation is completed with success the output will be similary as:
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_73, vendor: Oracle Corporation
Java home: /home/user/jdk1.8.0_73/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.8.0-58-generic", arch: "amd64", family: "unix"

Create project by maven archetype

A maven archetype is a Maven project templating toolkit and it helps create Maven project templates for developers, and provides they with the means to generate parameterized versions of those project templates. You can use a simple maven archetype to create a new java project using a single maven command. 
Example:
$ mvn archetype:generate -DgroupId=com.blogspot.informationtechnologyarchive \
-DartifactId=first-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
....
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /home/user/workspace
[INFO] Parameter: package, Value: com.blogspot.informationtechnologyarchive
[INFO] Parameter: groupId, Value: com.blogspot.informationtechnologyarchive
[INFO] Parameter: artifactId, Value: first-app
[INFO] Parameter: packageName, Value: com.blogspot.informationtechnologyarchive
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/user/workspace/first-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.438 s
[INFO] Finished at: 2017-**********
[INFO] Final Memory: 19M/90M
[INFO] ------------------------------------------------------------------------
If you use a Linux OS you can perform the "tree command" in first-app directory the output will be:
.
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── blogspot
    │               └── informationtechnologyarchive
    │                   └── App.java
    └── test
        └── java
            └── com
                └── blogspot
                    └── informationtechnologyarchive
                        └── AppTest.java

11 directories, 3 files
Now to create an executable jar you use the maven command shown below. Maven will create a target directory with the jar called first-app-1.0-SNAPSHOT.jar.
$ mvn package
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ my-app ---
[INFO] Building jar: /home/user/workspace/first-app/target/first-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.299 s
[INFO] Finished at: 2017-******
[INFO] Final Memory: 10M/167M
[INFO] ------------------------------------------------------------------------ 
Finally you can run your jar:
$ java -cp target/first-app-1.0-SNAPSHOT.jar com.blogspot.informationtechnologyarchive.App
The output in this case will be:
 Hello World!

Principal maven goals

To start execute the following command to compile your application sources:
$ mvn compile
The first time maven will need to download all the plugins and related dependencies it needs to fulfill the command. If you execute the command again, Maven will now have what it needs, so it won't need to download anything new and will be able to execute the command much more quickly.
After the successfully compiling now you want to compile and execute some unit tests:
$ mvn test
Maven downloads more dependencies this time. These are the dependencies and plugins necessary for executing the tests. Before compiling and executing the tests Maven compiles the main code. If you want only compile the unit tests:
$ mvn test-compile
Making a JAR:
$ mvn package
Maven creates only jar in target folder.
Now you'll want to install the artifact you've generated (the JAR file) in your local repository (${user.home}/.m2/repository is the default location):
$ mvn install
To remove the target directory with all the build data before starting so that it is fresh:
$ mvn clean
You can concatenate more maven goals, for example:
$ mvn clean install
If you use eclipse IDE, use the following command before importing the maven project:
$ mvn eclipse:eclipse
Similarly if you use IDEA:
$ mvn idea:idea

Eclipse IDE 

Installing Eclipse IDE

All Eclipse versions are available in this web page. I used Eclipse Neon in this post but you can use any version of eclipse for java. 
Eclipse Neon

Eclipse Maven Plugin

All commands such as those shown in the previous paragraph can be executed in eclipse through its plugin. This eclipse plugin is installed in all eclipse distributions for java. 
The use is quite intuitive: select your project, right button, select "Run As" -> "Run Configurations...." as shown in screenshot below.
Eclipse Run Configuration Item.
When the "Run Configuration" windows is loaded, you can configure your maven goal, you can see the following screenshot as example of "mvn clean install".
Maven clean install configuration.

Internal Links

May be of interest to you:

External Links

  1. Oracle JDK download page
  2. Installing Apache maven - official page
  3. Eclipse Download - official page

No comments:

Post a Comment

Welcome

Hello everybody, Welcome in my blog called "Information technology archive". Obviously the topics will be related to Informatio...