How to create a Java project with Maven

  • 07 August 2017
  • ADM

 

How to create a Java project with Maven - images/logos/maven.jpg

 

In this tutorial will present how to create a Java project with Maven.

The tools used in this tutorial are:

  • Maven 3.3
  • Eclipse Neon
  • Java 1.8

Note: Any versions will do the work.

If you need help to install any of the tools used in this tutorial please check How to prepare a Windows Java Development Machine for Java and Eclipse installation or the Maven installation tutorials (Windows, MacOS).

Create a Project

To create the project structure and the minimum code for the application run the following command from the terminal(Linux or macOS) or command prompt (Windows):

# mvn archetype:generate -DgroupId=com.admfactory -DartifactId=javatest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

The maven-archetype-quickstart archetype will create all the project and files needed.

You can see an output that looks like

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (5 KB at 9.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 8.4 KB/sec)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: D:\Repositories\Bitbucket\admfactory.com
[INFO] Parameter: package, Value: com.admfactory
[INFO] Parameter: groupId, Value: com.admfactory
[INFO] Parameter: artifactId, Value: javatest
[INFO] Parameter: packageName, Value: com.admfactory
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\Repositories\Bitbucket\admfactory.com\javatest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.975 s
[INFO] Finished at: 2017-07-04T23:09:04+03:00
[INFO] Final Memory: 15M/193M
[INFO] ------------------------------------------------------------------------

Now depending if all necessary plugins are already installed or not, the output can be longer and the process might take longer.

The project folder structure looks like this:

│   pom.xml
│
└───src
    ├───main
    │   └───java
    │       └───com
    │           └───admfactory
    │                   App.java
    │
    └───test
        └───java
            └───com
                └───admfactory
                        AppTest.java

A minimum hello world application is created. The project includes a minimum pom.xml file

<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.admfactory</groupId>
  <artifactId>javatest</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>javatest</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>

The application is really simple and is only printing the "Hello World!" text.

package com.admfactory;

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

Import in Eclipse

To import the project in Eclipse, first you need to create the Eclipse project file by running the following command:

# mvn eclipse:eclipse

After this you can import from Eclipse: select "File -> Import… -> General->Existing Projects into Workspace"

Update Java version

To update the Java version the maven-compiler-plugin need to be added to the pom.xml. Also you could update the Unit test to the latest version (4.12 at the point when the article was written).

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

To update the project from the command line navigate to the project folder and run again the command:

# mvn eclipse:eclipse

Maven Packaging

To create the jar file the following command needed to be executed.

# mvn package

The jar file will be generated into /target folder.

Run the Application

To run the application from the command line, execute the following command:

# java -cp target\javatest-1.0-SNAPSHOT.jar com.admfactory.App
Hello World!

 

References