Maven 2 Example Creating a new java project

After the successful installation of the Maven.

Let us create a new project template using maven.

mvn archetype:create -DgroupId=com.example -DartifactId=SimpleExample -Dpackagename=com.example

The result will be


To work on the project you can import the project into eclipse. to create the files required by eclipse execute the below command in the project directory.

mvn eclipse:eclipse


The directory structure that will be cretaed will look like



Now just write your source code in the src folder.



To compile the project

mvn compile

To run test cases


mvn test


To create the jar for the application, which will add the jar of the application in maven reposiroty
mvn install


The result will be
\M2_Repository\com\example\SimpleExample\1.0-SNAPSHOT\SimpleExample-1.0-SNAPSHOT.jar


You can even run multiple golas in one cmmand like


mvn compile test install


Now you can use this jar in any other application byshowing a dependency on this project as given below in pom.xml

<dependency>
<groupid>com.example</groupid>
<artifactid>SimpleExample</artifactid>
<version>1.0-SNAPSHOT</version>
</dependency>

If your project is having any dependency on any other then you need to show the deendency on it as below. example junit

<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

AddThis Feed Button
Please provide your feedback by sending an email to 1234java.blogspot@gmail.com. This would help me making the site more helpful. Thanks in advance.

DISCLAIMER:

The content provided in this article/site is not warranted or guaranteed by 1234java.blogspot.com. The content provided is intended for educational purposes in order to introduce to the reader key ideas, concepts. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials.