Getting started with Maven: First Maven Project Part 2

Eclipse IDE Support

To import this project into Eclipse, you need to generate some Eclipse project configuration files :

mvn eclipse:eclipse

IntelliJ IDEA  Support

mvn idea:idea

Importing Maven Project into Eclipse

username:samplemavenproject$ mvn eclipse:eclipse[INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building samplemavenproject 1.0[INFO] ------------------------------------------------------------------------[INFO] [INFO] >>> maven-eclipse-plugin:2.10:eclipse (default-cli) > generate-resources @ samplemavenproject >>>[INFO] [INFO] <<< maven-eclipse-plugin:2.10:eclipse (default-cli) < generate-resources @ samplemavenproject <<<[INFO] [INFO] [INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ samplemavenproject ---[INFO] Using Eclipse Workspace: null[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER[INFO] Not writing settings - defaults suffice[INFO] Wrote Eclipse project for "samplemavenproject" to /Users/admin/Desktop/samplemavenproject.[INFO] [INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 2.632 s[INFO] Finished at: 2018-04-03T00:57:05+05:30[INFO] Final Memory: 12M/41M[INFO] ------------------------------------------------------------------------username:samplemavenproject$

1.Open Eclipse.

2. Select File > ImportMaven.

3. Select  Existing Maven Projects Option. Click on Next Button.

 4. Select Project location, where a project was created using Maven. 

import maven project into eclipse

5. Click Finish


Project Structure in Eclipse:

Java Code:

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

Building the Project

username:samplemavenproject$ mvn clean package[INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building samplemavenproject 1.0[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ samplemavenproject ---[INFO] Deleting /Users/admin/Desktop/samplemavenproject/target[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ samplemavenproject ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory /Users/admin/Desktop/samplemavenproject/src/main/resources[INFO] [INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ samplemavenproject ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to /Users/admin/Desktop/samplemavenproject/target/classes[INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ samplemavenproject ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory /Users/admin/Desktop/samplemavenproject/src/test/resources[INFO] [INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ samplemavenproject ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to /Users/admin/Desktop/samplemavenproject/target/test-classes[INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ samplemavenproject ---[INFO] Surefire report directory: /Users/admin/Desktop/samplemavenproject/target/surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running com.interviewbubble.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.026 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ samplemavenproject ---[INFO] Building jar: /Users/admin/Desktop/samplemavenproject/target/samplemavenproject-1.0.jar[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 3.900 s[INFO] Finished at: 2018-04-03T01:44:08+05:30[INFO] Final Memory: 14M/48M[INFO] ------------------------------------------------------------------------username:samplemavenproject$

Getting Jar file:

Maven creates a target subdirectory inside the project root directory. Inside the target directory, you will find the finished JAR file(samplemavenproject-1.0.jar), as well as lots of temporary files (e.g. a classes directory containing all the compiled classes).


Running App on Eclipse:

Now, right click on App.java. Select Run As option. Then select Java Application.

OUTPUT on the console:

Hello World!

Final Code of First Maven Project:

Java Code:

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

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>  <groupId>com.interviewbubble</groupId>  <artifactId>samplemavenproject</artifactId>  <version>1.0</version>  <packaging>jar</packaging>  <name>samplemavenproject</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>    <plugins>        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-compiler-plugin</artifactId>            <version>3.5.1</version>            <configuration>                <source>1.8</source>                <target>1.8</target>            </configuration>        </plugin>    </plugins></build></project>