Java: Get Started with Apache Maven

remko de knikker
NYC⚡️DEV
Published in
4 min readMar 2, 2018

One of the key tools in your toolbox as a Java Developer is Apache Maven, to clean, build, package and install among other, your Java project, while managing versions and dependencies in a consistent manner.

This tutorial gets you started with Apache Maven in 15 minutes.

  • Create a simple Java app ‘HelloWorld’ with a ‘com.remkohde.java/HelloWorld’ class and a manifest file,
$ rm -rf ./helloworld
$ mkdir ./helloworld
$ cd ./helloworld
$ mkdir ./src
$ mkdir ./src/main
$ mkdir ./src/main/java
$ mkdir ./src/main/java/com
$ mkdir ./src/main/java/com/remkohde
$ mkdir ./src/main/resources
$ mkdir ./src/test
$ mkdir ./target
  • Above you created the recommended directory structure for a Java application. Java source files are saved in the ‘./src/main/java’ folder, the folder ‘./src/main/resources’ is added to the class-path to include resources like properties files to your Java application, test files are saved in ‘./src/test’, compiled class files are saved to ‘./target/classes’, and jar archives are saved to the ‘./target’ folder.
  • To create our main class file ‘HelloWorld.java’ try using the vi editor. You can use other text editors as well, but it is a good-to-have skill to be familiar with vi command line editing, imho. Briefly, you open the vi editor with ‘$ vi path/to/filename’, which opens the ‘path/to/filename’ file in the vi editor and creates that file implicitly if it doesn’t exist. Inside vi, you can press the colon to execute vi commands, e.g. ‘:w’ to write file to disk or save the file, ‘:q’ to quit or exit, to force quit without saving ‘:q!’, to exit and save ‘:wq’. While you are in ‘execute vi commands’ mode you cannot insert or edit the file; while you are editing with the ‘i’ mode, you cannot execute vi commands, until you press the <ESC> character.
  • Now try using the vi editor to create the ‘HelloWorld.java’ class,
$ vi ./src/main/java/com/remkohde/HelloWorld.java
package com.remkohde;
public class HelloWorld {
public static void main(String[] args) {
HelloWorld myWorld = new HelloWorld();
String said = myWorld.sayHelloTo("World");
System.out.println(said);
}
public String sayHelloTo(String to) {
return "Hello "+to+"!";
}
}
:wq
  • I want to package our Java application in a Jar file. A Jar package needs a Manifest file to describe the files it contains. At the minimum, we need to set the main class or entrypoint in the Jar,
$ mkdir ./src/main/resources/META-INF
$ vi src/main/resources/META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.remkohde.HelloWorld
:wq
  • Make sure ‘java’ and ‘javac’ are on the PATH of your operating system. The PATH is a list of environment variables that your operating system searches to find where applications, which you are trying to run, are located. By setting the JAVA_HOME environment, and adding ‘$JAVA_HOME/bin’ to your PATH, your system looks in that directory to run the ‘java’ and ‘javac’ applications that are part of the Java environment. You run Java applications with the ‘java’ application, and compile your Java applications with ‘javac’. On Mac, your PATH is defined in the ‘~/.bash_profile’ file. Run ‘echo $PATH’ to see your current PATH definition. When you want to run Maven from the commandline with ‘mvn’, you need to make sure your operating system can find the Maven executable called ‘mvn’, so you need to add the location of the ‘mvn’ application also to your PATH.
  • Compile and Package your Java app
$ rm -rf target
$ mkdir target
$ javac -classpath src/main/resources -sourcepath src/main/java src/main/java/com/remkohde/HelloWorld.java -d target/classes
$ jar cfm target/MyWorld.jar ./src/main/resources/META-INF/MANIFEST.MF -C target/classes .
  • Run ‘HelloWorld’ with ‘java -jar’
$ java -jar target/MyWorld.jar
  • Install Apache Maven manually or use HomeBrew to install Apache Maven, I recommend to use HomeBrew or brew in short.
  • To install HomeBrew on Mac, paste the following installation command on the command-line,
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • With HomeBrew, use the brew formula as follows,
$ brew install maven
$ mvn -v
Apache Maven 3.5.2 (138e5d7d; 2017-10-18T03:58:13-04:00)
Maven home: /usr/local/Cellar/maven/3.5.2/libexec
Java version: 9.0.4, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.3", arch: "x86_64", family: "mac"
  • If mvn command is not found, make sure you add the path to your environment variables,
$ vi ~/.bash_profile
export MAVEN_HOME='/usr/local/Cellar/maven/3.5.2'
export PATH=$MAVEN_HOME/bin:$PATH
:wq
$ source ~/.bash_profile
$ mvn -v
  • Create a Maven configuration file named ‘pom.xml’
$ vi 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.remkohde</groupId>
<artifactId>helloworld-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HelloWorld Project</name>
<url>http://www.remkohde.com</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>src/main/resources/</classpathPrefix
<mainClass>com.remkohde.HelloWorld</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
:wq
  • Note that you have defined the application to depend on Java version 1.8, you have defined a dependency on an external Java project called JUnit version 4.8.2 (you are not yet actually using JUnit in our application, but you are all set up to get started with JUnit), and in the plugins section you have defined to use the ‘maven-jar-plugin’ to build and package your application,
  • Also, you can always search for dependencies and versions at https://mvnrepository.com,
  • Now, compile and package the application with Maven,
$ mvn package
  • Run the application
$ java -jar target/helloworld-app-1.0-SNAPSHOT.jar

Congratulations! You have replaced native Java command-line compiling and packaging using ‘javac’ and ‘java -jar’ by a Maven based process. As your project gets more complex, by adding dependencies on external packages and version, using Maven will make your life a lot easier. Your path into being a Java developer in a Maven world has started. Make sure to add ‘Apache Maven’ and ‘vi editor’ to your LinkedIN resume!

--

--

remko de knikker
NYC⚡️DEV

Cloud Native Developer Advocate @IBMDeveloper for Cloud Native, Containers, Kubernetes, Security and DevOps. Dutch NYer, dad, humanist with empathy for paradox.