Leaderboard1

Leaderboard2

Wednesday, May 21, 2014

Create a Java Project with Maven

Tools used:

  1. Maven 3.0.5
  2. Eclipse 4.2
  3. JDK 6

Steps:

1. Create a Project from Maven Template

Go to a folder where you want to create Maven project, and run this command on command prompt.

mvn archetype:generate -DgroupId=com.akash -DartifactId=MavenTest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This will download artifacts, dependencies and create a project structure.


Project Structure:


MavenTest

   |-src

   |---main

   |-----java

   |-------com

   |---------akash

   |-----------App.java

   |---test

   |-----java

   |-------com

   |---------akash

   |-----------AppTest.java

   |-pom.xml

Note: Keep source code in folder /src/main/java/project-package, all unit test code puts in /src/test/java/project-package.

Now a POM.xml file has been generated.

What is POM?

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which issrc/main/java; the test source directory, which is src/main/test; and so on.

Update POM.xml



















3. Make Maven project compatible with Eclipse

Run this commandmvn eclipse:eclipse

4. Update AppTest.java


package com.akash;

import org.junit.Assert;
import org.junit.Test;

public class AppTest {

@Test
public void testLengthOfTheUniqueKey() {

App obj = new App();

}

}



5. Compile Project and create jar

run mvn package on cmd

It will compile Maven project and create a jar.

No comments:

Post a Comment