Creating maven project in Eclipse

In this article we are going to learn how to “Creating maven project in Eclipse” IDE. If you have not installed maven plugin in eclipse, then first of all install it. Follow this article in order to install maven plugin “M2E Settings 2.0.4” in eclipse IDE. Before creating project, we should know what archetype is?

What is maven archetype?

  • It is a maven project templating toolkit that helps users to create different types of maven project having maven supplied facilities and user supplied inputs.
  • Nearly 1500+ archetypes are supplied by Maven to create different types of project. The popular archetypes are

Archetype ArtifactIds Description
maven-archetype-quickstart An archetype to generate a sample Maven project.
maven-archetype-webapp An archetype to generate a sample Maven Webapp project.
maven-archetype-j2ee-simple An archetype to generate a simplifed sample J2EE application.

To know more about different archetypes and its functionalities visit https://maven.apache.org/guides/introduction/introduction-to-archetypes.html

Now let us see how to create a standalone maven project with the help of maven-archetype-quickstart.

Click on File menu -> New -> Maven Project

code-adda Creating maven project in Eclipse

A New Maven Project dialog will appear. Click on Next.

When clicked on Next , you will see number of archetypes. Since we are going to create a standalone java project, select maven-archetype-quickstart.

Click on Next button. Again dialog box will appear, asking to specify archetype parameters. Enter the following details (or specify these details as you wish).

When Finish button is clicked, a maven project with artifact Id “GreetingsApp” will be created as follows.

As you can see in the newly created project directory structure, the default JRE System Library [J2SE-1.5] (nothing but jdk 1.5) is taken even though you might have installed some other versions of jdk (may be jdk1.7 or jdk 1.8). According to maven documentation: “at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.

There are two ways to solve this issue (applicable both in case of standalone-quickstart and webapp kind of application).

  • Manually remove default JRE System Library [J2SE-1.5] and add library (some other jdk version as installed in your system).
  • Use maven-compiler-plugin to configure source and target compliance version.
  1. Manually remove default JRE System Library [J2SE-1.5] and add library (some other jdk version as installed in your system).

Right click on GreetingsApp -> Properties -> Properties For GreetingsApp dialog will appear.

Click on Java Build Path -> Select JRE System Library [J2SE-1.5] -> Hit Remove button.

JRE System Library [J2SE-1.5] will be removed. Now click on Add Library. Add Library dialog box will appear as follows.

Select JRE System Library and hit Next button.

Select Workspace default JRE (jdk 1.8.0_73) (or the default JDK installed in your machine). Hit Finish button. You will see the project directory structure with updated JRE System Library [jdk 1.8.0_73].

2. Use maven-compiler-plugin to configure source and target compliance version.

We need to add maven-compiler-plugin in project’s pom.xml.

<plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
 <source>1.8</source>
 <target>1.8</target>
 </configuration>
 </plugin>

Important Note: Since I have installed jdk 1.8 version so, I have cfg <source> and <target> to 1.8. You do the changes as per your Jdk installation.

Update the Maven project

Right click on the GreetingsApp -> Maven -> Update Project.. -> Update Maven Project dialog box will appear. Make sure GreetingsApp is checked -> Hit OK button. You are done…!!

In App.java

package com.codeAdda.GreetingsApp;
import java.util.Calendar;

/**
* This class contains method to greet based on the hour of the day.
* @author Chandra Mani Gupta
* @since 24/04/2018
*/
public class App {
public static void main(String[] args) {
System.out.println(greetMe("Varun"));
}

private static String greetMe(String name){
String wishMsg=null;
Calendar calendar= Calendar.getInstance();
int hr=calendar.get(Calendar.HOUR_OF_DAY);

if (hr&lt;=12)
wishMsg= "Good Morning "+name;

else if (hr&lt;=16)
wishMsg= "Good Afternoon "+name;

else if (hr&lt;=20)
wishMsg= "Good Evening "+name;

else
wishMsg= "Good Night "+name;

return wishMsg;
}
}

Use of Exec Maven Plugin

Now, we will compile and execute this standalone project with the help of maven. For this we need a plugin “execmavenplugin”. What actually is this plugin and what it does???

This plugin helps to execute the maven project. We can Official documentation says (http://www.mojohaus.org/exec-maven-plugin/)

Exec Maven Plugin provides 2 goals to help execute system and Java programs.

  • exec:exec execute programs and Java programs in a separate process.
  • exec:java execute Java programs in the same VM.

So, we need to provide this information in pom.xml of the our GreetingsApp project.

Add the following <plugin> under <build> tag in pom.xml specifying the <mainClass>.

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>exec-maven-plugin</artifactId>
 <version>1.4.0</version>
 <configuration>
 <mainClass>com.codeAdda.GreetingsApp.App</mainClass>
 </configuration>
 </plugin>

Updated pom.xml looks like the following.

In GreetingsApp/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion></pre>
<groupId>com.codeAdda</groupId>
<artifactId>GreetingsApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>GreetingsApp</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>GreetingsApp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>com.codeAdda.GreetingsApp.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

Execution

Right click on GreetingsApp -> Run As -> Maven Build.. -> Specify the Goals: (exec:java) -> Run

When Run button is clicked, maven will start to build the project. Main class that is configured in pom.xml will execute and we will get BUILD SUCCESS as the following.

I hope you enjoyed this article. In case of any issue, ping me in the comment section below. Kindly subscribe our blog to keep yourself updated with the upcoming article. Happy Coding.

 

Share Knowledge
This entry was posted in Java, Maven and tagged , , . Bookmark the permalink.

Leave a Reply

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