maven java project example

Maven java project:

The archetype:generate command is used to create the simple maven java project. First we have to navigate the terminal (*uix or Mac) or command prompt (Windows) to the folder where we want to create the Java project.

Syntax:

mvn archetype:generate -DgroupId=groupid -DartifactId=project-name   
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=booleanValue

Example:

mvn archetype:generate -DgroupId=com.tutorialspointexamples -DartifactId= MavenHelloWorld  
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Generated directory structure:

MavenHelloWorld 
   |-src
   |---main
   |-----java
   |-------com
   |---------w3schools
   |-----------App.java
   |---test
   |-----java
   |-------com
   |---------w3schools
   |-----------AppTest.java
   |-pom.xml

Auto created App.java:

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

Auto created AppTest.java:

package com.w3schools;
 
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
 
/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }
 
    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }
 
    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

Auto created 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.w3schools</groupId>
  <artifactId>MavenHelloWorld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>MavenHelloWorld</name>
  <url>http://maven.apache.org</url>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>