rss feed Twitter Page Facebook Page Github Page Stack Over Flow Page

Create a new Java project using maven

Posted on Tuesday May 22, 2018 by Eric Potvin

Creating new Java project happens somehow often, but remember to add the pom.xml file is always something I forget. Of course, after creating this I will remember but in case you don't remember how or you do not know how here's two simple ways on how to create a Java project using maven:

The UI method

First, let's start by creating a new Project:

Name your project and location:

This creates the basic structure:

Adding Maven / pom.xml

Now, let's make this project a Maven project. Right click on the Project name in the "breadcrumb" section, then select "Add Framework Support..."

Select, Maven and click OK

Now we have a pom file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<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>groupId<groupId>
    <artifactId>MyProject<artifactId>
    <version>1.0-SNAPSHOT<version>
</project>

This pom file needs some love!

Simply change the "groupId", "artifactId" and "version" tags, example:

<groupId>com.bookofzeus.app</groupId>
<artifactId>BookOfZeusApp</artifactId>
<version>0.0.1</version>

At this point now you can create new files in the src/main folder and create some test that goes with it.

The Command line method

Another way to (very) easily build a maven project is using the command line tool.

mvn archetype:generate \
    -DgroupId=com.bookofzeus.app \
    -DartifactId=BookOfZeusApp \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DinteractiveMode=false

The "archetypeArtifactId" means what archetype to use to create the initial structure of the project

The interactiveMode means:
true if Maven should attempt to interact with the user for input, false if not. Defaults to true

This will create everything you need, including a sample script!

./src
./src/main
./src/main/java
./src/main/java/com
./src/main/java/com/bookofzeus
./src/main/java/com/bookofzeus/app
./src/main/java/com/bookofzeus/app/App.java
./src/test
./src/test/java
./src/test/java/com
./src/test/java/com/bookofzeus
./src/test/java/com/bookofzeus/app
./src/test/java/com/bookofzeus/app/AppTest.java
./pom.xml

Notes: You might want to change the jUnit version from the pom.xml file.