Maven 3

Apache Maven is sotware project management tool, it uses the Project Object Model (POM) to manage builds (lifecycle), dependencies, reporting and documentation of software projects. It is primarily used for Java projects but can be used for many other programming langages as well. A XML file (POM file) is used to describe the software project on how it should be built, the build order, what dependencies are required, how to retrieve other software that is required, what plugins are required and to be used (there are many plugins available), etc. Maven supports all major IDE's and is used by many large Open Source Projects like Spring Framework, Spring Boot.

As of writing this Apache Maven is at version 3.6.3 in June 2020, and the Apache Maven website will have the latest downloads and documentation.

Maven has also established some standards which help developers coexist and be able to work on the same projects with out too much disruption

Getting Started

I am not going to explain how to install Maven as there is already many web pages on the internet how how to do this, and of course you have the Apache Maven website as well. Once you have installed Maven and Java you should be able to check both from the commandline, notice that the command for maven is mvn


Maven version
mvn -version
Maven help
mvn -help

Compiling Java (commandline)

In Java you create a Java source file (.java) and then compile it into a class file (.class) this is byte code that the Java Virtual Machine (JVM) understands and is able to execute. You then can package up a number of .class files into a Java Archive called a jar file (.jar) this generally is a complete application, there are also other Java packaging that can be used.

Remember that once we get into Maven all this will be done for us under the covers, this is so that you understand what Maven will be doing for us.

There are a common set of tools that you can use when developing in Java from running, compiling, packaging (Jar file), documentation, etc I provide a table below of the most commonly used:

Running
java [--class-path, --classpath, -cp]
java [-verbose:class]
java [-verbose:gc]

java Test 					# Run a java program without a package configured
java Test arg1 arg2 arg3			# Run a java program without a package configured and pass 3 arguments

java uk.co.datadisk.Test 			# Run a java program with a package of uk.co.datadisk, remember you are running from the root directory uk/co/datadisk, dont from from inside uk/co/datadisk the same directory as the class
java uk.co.datadisk.Test arg1 arg2 arg3		# Run a java program with a package and pass 3 arguments

java -classpath myjar.jar HelloWorld            # execute and run a jar file
Compiling
javac -d				# set the destination output directory of the class file
javac -gc				# generates all debugging info
javac -g:[lines, vars, source]		# generates specific debugging info
javac -verbose				# outputs compiler info

javac HelloWorld.java                   # creates a .class file (byte code file)
Packaging
jar [-c, --create]			# create a JAR archive file
jar -C					# changes the specified directory and includes the files at the end of the commandline
jar [-f, --file]			# specifies the name of the JAR file
jar [-m, --manifest]			# includes the manifest file information

jar cf myjar.jar Helloworld.class       # this will create a jar file
Other useful tools
javap -c				# prints disassembled code
javadoc					# generate API documentation in HTML format
jlink					# assemble and optimize a set of modules and their dependencies into a custom runtime image
jmod					# create JMOD files and list the content of an existing JMOD file
jdeps					# Java class dependency analyzer
jdeprscan				# static analysis tool that scans a jar file

Getting Started with Maven (commandline)

This section we take a very simple look at Maven in other sections I will cover more advanced features.

Very Basic POM XML file
<?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>uk.co.datadisk</groupId>
    <artifactId>hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
	
</project>
POM Breakdown
xml
## This defines the XML version and any encoding, the XML declaration describes some of the most general properties of 
## the document, telling the XML processor that it needs an XML parser to interpret this document <?xml version="1.0" encoding="UTF-8"?>
project, schema and namespacing
## This is the top level element and defines the schema used and any namespaces, also defines all the rules and elements that can be used.

<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">
model version
## This element indicates what version of the object model this POM is using.

<modelVersion>4.0.0</modelVersion>
group ID
## indicates the unique identifier of the organization or group that created the project. The groupId is one of the key 
## identifiers of a project and is typically based on the fully qualified domain name of your organization <groupId>uk.co.datadisk</groupId>
artifact ID
## This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact 
## for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their
## final name <artifactId>hello-world</artifactId>
version
## This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version 
## management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state
## of development <version>0.0.1-SNAPSHOT</version>
properties
## Maven properties are value placeholder, their values are accessible anywhere within a POM by using the notation ${X}, 
## where X is the property, or they can be used by plugins as default values <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>11</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> </properties>

Once we have a POM file we can run a number of Maven commands from the commandline, if you use the package command a target directory will be created, a JAR file will be created that contains a Manifest file and Maven pom.xml, pom.properites file. Some of the maven (mvn) commands you need a pom.xml file otherwise it will complain.

If you have a java file in src/main/java this would then get compiled and the class file will end up in target/classes directory.


Maven clean
mvn clean
Maven package
mvn package

You can add dependencies in the pom.xml file, this will reach out to the maven repository and download the neccessary JAR file.

dependencies
## Any dependencies required will be automatically downloaded from the Maven Repository

<dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
    <dependency>
</dependencies>