Last updated: August 20, 2017

  1. A little bit of history
  2. What for?
  3. Installation
  4. Generating project

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting, and documentation from a central piece of information.

Primarily used for Java-based projects but that can also be used to merge projects in other programming languages like C# and Ruby. Many integrated development environments provide plugins for Maven. Typical tasks if a build tool is a compilation of source code, running test and packaging results into jar files. Maven can also perform related activities, for example, creating websites, uploads build results or generate reports.

Automating process of creating the initial project structure for Java application is very important, especially when doing it manually may take much more time and also there is a possibility for making typos during using some commands in the console, the last one is very frequent.

A little bit of history:

Originally Maven was designed to simplify building process in Jakarta Turbine project.

What for?

Maven can be used for:

  • handle dependencies and intermediate dependencies
  • easy selection of tasks from the command line
  • versioning and tagging code
  • a large number of plugins to simple and complex tasks
  • Integration with all IDE such as Eclipse, NetBeans or IntelliJ
  • ability to manage a number of the modules of the project at a time
  • support for:
    • unit tests
    • integration tests
    • load tests
    • performance tests
  • application builds
  • generating documentation
  • releases
  • mailing list management

Maven is working according to the Convention Over Configuration pattern, which means that the configuration requires only those components that are nonstandard or user will want to adopt them for their needs, and the same configuration change can also be found under only one file – pom.xml. Developers can build Maven project without the need to understand how the individual plugin works.

Installation:

I use several systems start from Linux, Windows and Mac OS X machines. For this post, I will use Mac OSX. Open terminal and type: brew install maven

If we have already maven installed, in a terminal should appear:

~ brew install maven
Warning: maven-3.3.9 already installed

After installing Maven should check if everything is working properly, this purpose should be in the command line to run Maven and check the versions of what was installed:

~ mvn --version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_73, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home/jre
Default locale: pl_PL, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.3", arch: "x86_64", family: "mac"

As you can see installation is not difficult in inches. Some different looks to install Maven on Linux and Windows.

Before we turn to the creation of the first project we need to discuss a matter of a few, namely what they are:

  • artefacts
  • group
  • version

The artifact is a name that identifies the project in the group. The group allows you to organize the namespace in which it is the artifact.

Both the group and artifact and version uniquely identifies the library of which we want to use. There is also something like what is called a plugin and archetype. The plugin is a special Java language, class in which performs the appropriate actions on the basis of asking configuration, for example, to pack compiled class to a jar file, generate documentation. An artifact is a specific plugin using which created them st project. It contains in its structure mapping of tree directories a new project, or file pom.xml contains basic project data.

Generating project:

To generate maven project we have two options. First from a console and second from IDE. Creating maven project looks as follows:

JavaMaven mvn archetype:generate -DgroupId=com.andrzejdubaj.build.maven.java -DartifactId=com.andrzejdubaj.build.maven.java -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

With this command Maven generates a Java project:

[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 0.8 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 6.7 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: /Users/andrzejdubaj/Programming/Java/JavaMaven
[INFO] Parameter: package, Value: com.andrzejdubaj.build.maven.java
[INFO] Parameter: groupId, Value: com.andrzejdubaj.build.maven.java
[INFO] Parameter: artifactId, Value: com.andrzejdubaj.build.maven.java
[INFO] Parameter: packageName, Value: com.andrzejdubaj.build.maven.java
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/andrzejdubaj/Programming/Java/JavaMaven/com.andrzejdubaj.build.maven.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.883 s
[INFO] Finished at: 2017-03-07T16:43:43+01:00
[INFO] Final Memory: 15M/222M
[INFO] ------------------------------------------------------------------------

This last step can take some time depends on how much RAM memory do you have (These days it’s 16 GB). Structure of the generated project looks like follows:

└── com.andrzejdubaj.build.maven.java
    ├── pom.xml
    └── src
        ├── main
           └── java
               └── com
                   └── andrzejdubaj
                       └── build
                           └── maven
                               └── java
                                   └── App.java
        └── test
            └── java
                └── com
                    └── andrzejdubaj
                        └── build
                            └── maven
                                └── java
                                    └── AppTest.java

We have generated whole Maven project structure Java source code. Maven created App.java class which is just simple “Hello World” program. We have also can see AppTest.java that is the simple test class. In root catalog, we can find our 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.andrzejdubaj.build.maven.java</groupId>
  <artifactId>com.andrzejdubaj.build.maven.java</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>com.andrzejdubaj.build.maven.java</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>

To compile Java source code we need to trigger: mvn compile in root project directory. This command runs through all life cycle phase, which is needed to compile a project.

  com.andrzejdubaj.build.maven.java mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building com.andrzejdubaj.build.maven.java 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ com.andrzejdubaj.build.maven.java ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/andrzejdubaj/Programming/Java/JavaMaven/com.andrzejdubaj.build.maven.java/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ com.andrzejdubaj.build.maven.java ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/andrzejdubaj/Programming/Java/JavaMaven/com.andrzejdubaj.build.maven.java/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.020 s
[INFO] Finished at: 2017-03-07T18:11:16+01:00
[INFO] Final Memory: 14M/309M
[INFO] ------------------------------------------------------------------------

To run test phases instead to run a full build we need just to trigger: mvn test

  com.andrzejdubaj.build.maven.java mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building com.andrzejdubaj.build.maven.java 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ com.andrzejdubaj.build.maven.java ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/andrzejdubaj/Programming/Java/JavaMaven/com.andrzejdubaj.build.maven.java/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ com.andrzejdubaj.build.maven.java ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ com.andrzejdubaj.build.maven.java ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/andrzejdubaj/Programming/Java/JavaMaven/com.andrzejdubaj.build.maven.java/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ com.andrzejdubaj.build.maven.java ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/andrzejdubaj/Programming/Java/JavaMaven/com.andrzejdubaj.build.maven.java/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ com.andrzejdubaj.build.maven.java ---
[INFO] Surefire report directory: /Users/andrzejdubaj/Programming/Java/JavaMaven/com.andrzejdubaj.build.maven.java/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.andrzejdubaj.build.maven.java.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.576 s
[INFO] Finished at: 2017-03-07T18:16:43+01:00
[INFO] Final Memory: 14M/208M
[INFO] ------------------------------------------------------------------------

To clean project and remove all generated files from ./target directory we can use: mvn clean

  com.andrzejdubaj.build.maven.java mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building com.andrzejdubaj.build.maven.java 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ com.andrzejdubaj.build.maven.java ---
[INFO] Deleting /Users/andrzejdubaj/Programming/Java/JavaMaven/com.andrzejdubaj.build.maven.java/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.221 s
[INFO] Finished at: 2017-03-07T18:19:09+01:00
[INFO] Final Memory: 8M/309M
[INFO] ------------------------------------------------------------------------

Reference:

  1. Apache Maven Project
  2. MVN Repository
  3. IntelliJ IDEA Maven
  4. Supported tags and respective Dockerfile links
  5. Jakarta Project
  6. Five things you didn’t know about Maven by IBM
  7. Introduction to Apache Maven 2
  8. Deployment of artifacts with FPT
  9. Maven Wrapper GitHub repo