Sunday, 13 March 2022

Create new maven project in Eclipse

In this post, I am going to explain how to create a new maven project in Eclipse.

 

Step 1: Open Eclipse.

 

Step 2: File ->New -> Maven Project.

 


In the ‘New Maven Project’ window, select the check box ‘Create a simple project (skip archetype selection)’




Click on Next> button.

 

 


In the ‘New Maven Project’ form

a.   Give Group Id as ‘com.sample.app’.

b.   Artifact Id as ‘hello-world-eclipse’

c.    Version as 1.

 

Click on Finish button.

 

Project structure looks like below.

 

 


Step 3: Update pom.xml by adding custom properties and build plugin.

 

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>hello-world-eclipse</artifactId>
	<version>1</version>

	<properties>
		<java.version>1.8</java.version>
		<maven.compiler.source>${java.version}</maven.compiler.source>
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.report.outputEncoding>UTF-8</project.report.outputEncoding>
	</properties>

	<build>
		<plugins>
			<plugin>
				<!-- Build an executable JAR -->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.0.2</version>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
							<mainClass>com.sample.app.HelloWorld</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

As you see above xml file, I set main class in the <manifest>.

 

Step 4: Create a package ‘com.sample.app’ and define HelloWorld class..

 

HelloWorld.java

package com.sample.app;

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

}

Step 5: Generate the artefact.

 

Navigate to the directory, where pom.xml is located and execute the command ‘mvn clean package’.

$mvn clean package
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< com.sample.app:hello-world-eclipse >-----------------
[INFO] Building hello-world-eclipse 1
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.0.2/maven-jar-plugin-3.0.2.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.0.2/maven-jar-plugin-3.0.2.jar (0 B at 0 B/s)
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world-eclipse ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world-eclipse ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world-eclipse ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/krishna/eclipse-workspace/hello-world-eclipse/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world-eclipse ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world-eclipse ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world-eclipse ---
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ hello-world-eclipse ---
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/3.0.1/maven-shared-utils-3.0.1.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/3.0.1/maven-shared-utils-3.0.1.jar (0 B at 0 B/s)
[INFO] Building jar: /Users/krishna/eclipse-workspace/hello-world-eclipse/target/hello-world-eclipse-1.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.247 s
[INFO] Finished at: 2022-03-13T19:52:26+05:30
[INFO] ------------------------------------------------------------------------
$

‘hello-world-eclipse-1.jar’ file is generated and placed at target folder.

 

Execute the command ‘java -jar ./target/hello-world-eclipse-1.jar’ to run the jar file.

$java -jar ./target/hello-world-eclipse-1.jar 
Hello World!!!!!!!

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/maven-projects/hello-world-eclipse






Previous                                                 Next                                                 Home

No comments:

Post a Comment