Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Friday, 2 August 2013

Create a Java Project in Maven and import it in Eclipse IDE

In this particular blog we will discuss ‘How to create a Java Project in Maven’, ‘Import maven java project in eclipse ’. and ‘create a jar from java maven project’.

Before we start lets make sure you have a working Maven Package and Java installation in your machine. To make sure if you have Java installed in your system supply following command:

C:\Users\nagesh.chauhan>javac




To make sure you have a correctly configured Maven package supply following command.

C:\Users\nagesh.chauhan>mvn -version




Create Java project in Maven

To create a java project in maven open your command prompt and navigate to the folder where you want to create the project. Now supply following command.

C:\Users\nagesh.chauhan>cd maven-project
C:\Users\nagesh.chauhan\maven-project>mvn archetype:generate -DgroupId=com.beingjavaguys.java -DartifactId=SimpleJavaProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


You will see a screen creating Maven a specified Java Project for you.



Make Maven – Jave project to work with Eclipse IDE

Here we are done with creating a Java Project in Maven, now lets make it working with eclipse IDE. Now to convert this maven-java project to work with Eclipse IDE navigate to the parent directory of your project and supply following command.

C:\Users\nagesh.chauhan>mvn eclipse:eclipse




Now import the converted project in your Eclipse IDE, the project structure in Eclipse will look like the image shown below:



Create a jar file from maven-java project in Eclipse

Now lets see how to create a jar from java-maven project, update your pom.xml a little bit to make it package your java application.

SimpleJavaProject\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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.beingjavaguys.java</groupId>
 <artifactId>SimpleJavaProject</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>SimpleJavaProject</name>
 <url>http://maven.apache.org</url>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.11</version>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
    </configuration>
   </plugin>
  </plugins>
 </build>

</project>

Now navigate to your project parent directory and supply following command.

C:\Users\nagesh.chauhan\maven-project\SimpleJavaProject>mvn install -Dmaven.test.skip=true

We have skipped all junit classes from packaging because those are not configured yet. You will get following output screen showing packaging process.


Now go to your Eclipse IDE and expand target folder, you will get your generated jar file there as shown in following figure.


Run the java project from eclipse IDE and you will see the output like :



In this particular blog we came across ‘How to create a Java Project in Maven’, ‘Import maven java project in eclipse ’. and ‘create a jar from java maven project’. In upcoming blogs we will see more about 'Maven', 'Spring', 'Java' and other opensource technologies.






Thanks for reading !
Being Java Guys Team


1 comment:

  1. Thanks for the lesson.
    Didn't run from command line though the way I tried.
    cd target
    java -jar SimpleJavaProject-1.0-SNAPSHOT.jar
    no main manifest attribute, in SimpleJavaProject-1.0-SNAPSHOT.jar

    wha wha wha wha whaaaaaa!

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft