In this article we will learn how to create a simple java project with Maven. We also review project structure and the commands for building the project.
1. Installation
First, download and install maven. After that, type the following at the command prompt:
1 |
mvn --version |
This should print the current version of Maven.
1 2 3 4 5 6 |
Apache Maven 3.0.5 Maven home: /usr/share/maven Java version: 1.8.0_111, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-openjdk-i386/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.13.0-102-generic", arch: "i386", family: "unix" |
2. Creating a project
Now you need to create a project. Create a directory and run the following command in the console:
1 |
mvn archetype:generate -DgroupId=com.thecodeexamples.maven -DartifactId=maven-project -DarchetypeArtifactdId=maven-archetype-quickstart -DinteractiveMode=false |
You will see that a directory with the specified artifactId was created.
Inside the directory is the standard structure of the project maven.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
maven-project |-- pom.xml `-- src |-- main | `-- java | `-- com | `-- thecodeexamples | `-- maven | `-- App.java `-- test `-- java `-- com `-- thecodeexamples `-- maven `-- AppTest.java |
The src/main/java directory contains the source code for the project, src/test/java project tests, and the pom.xml file is the Project Object Model of the project.
By the way, in addition to maven-archetype-quickstart there are other Maven Archetypes. For example, maven-archetype-j2ee-simple will create a simplifed sample J2EE application, and maven-archetype-webapp a sample Maven Webapp project.
3. Pom.xml
Pom.xml is a key project file that contains all the information you need to build a project.
Here is an example from our created project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<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.thecodeexamples.maven</groupId> <artifactId>maven-project</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>maven-project</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> |
4. Build the project
To build the project, run:
1 |
mvn package |
The console displays a lot of information, and ends with the following lines:
1 2 3 4 5 6 7 8 9 |
[INFO] [INFO] --- maven-jar-plugin:2.2:jar (default-jar) @ maven-project --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.316s [INFO] Finished at: Wed May 31 20:47:51 CEST 2017 [INFO] Final Memory: 7M/110M [INFO] ------------------------------------------------------------------------ |
We can run the project with a command
1 |
java -cp target/maven-project-1.0-SNAPSHOT.jar com.thecodeexamples.maven.App |
Which will print the famous:
1 |
Hello World! |
3 Comments on “How to create a simple Java project with Maven”