e42.uk Circle Device

 

Quick Reference

Using Maven for Java Development

Using Maven for Java Development

Gone are the days when I would compile stuff with javac it all seems to be with some build system these days, and with TDD it is probably for the best.

Packages Used

Beginning

First install Maven (Linux):

wget https://dlcdn.apache.org/maven/maven-3/3.9.1/binaries/apache-maven-3.9.1-bin.tar.gz
tar -xf apache-maven-3.9.1-bin.tar.gz
rm apache-maven-3.9.1-bin.tar.gz
mv apache-maven-3.9.1 /opt/
export PATH=/opt/apache-maven-3.9.1:${PATH}

Windows:

Download the zip version (https://dlcdn.apache.org/maven/maven-3/3.9.1/binaries/apache-maven-3.9.1-bin.zip)[https://dlcdn.apache.org/maven/maven-3/3.9.1/binaries/apache-maven-3.9.1-bin.zip] then extract it somewhere.

set PATH=C:\jdk-17\bin;C:\apache-maven-3.9.1\bin;%PATH%

Create a Project

Building

mvn compile

Execute from Command Line

Executing a basic program from the command line with java:

java -cp target\classes com.github.drcrane.mavenexample.cmdline.WithoutDependency arg0 arg1

Try the same with a class that has dependencies:

java -cp target\classes com.github.drcrane.mavenexample.cmdline.WithDependency arg0 arg1

Because the project requires libraries that were downloaded from central the libraries must be included with the project when distributed and when executing the project they must be in the classpath. These libraries are more formally known as dependencies and thus help is at hand via the maven-dependency-plugin (documentation here).

dependency:build-classpath

This tool allows us to create a class path which may then be added to the java command line:

mvn dependency:build-classpath -DincludeScope=runtime -Dmdep.outputFile=classpath.txt

That is fine until you realise that in windows the command line length is limited by default and so the resulting classpath becomes too long. There are several ways to resolve that problem, including making the command processor accept longer commands and environment variables. See how to set a long java classpath in windows and the command line is too long for more details.

dependency:copy-dependencies

It is also possible to copy all those dependency jars to a single directory which will allow a much shorter java command line to execute the project.

mvn dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory=dependency

To execute the programme on Windows:

java -cp dependency\*;target\classes com.github.drcrane.mavenexample.cmdline.WithDependency

To execute on Linux:

java -cp dependency/*:target/classes com.github.drcrane.mavenexample.cmdline.WithDependency

exec:java

To execute the application using the exec mojo, on Windows:

mvn exec:java -Dexec.mainClass=com.github.drcrane.mavenexample.cmdline.WithDependency -Dexec.args="first_argument ""second argument"""

On Linux:

mvn exec:java -Dexec.mainClass=com.github.drcrane.mavenexample.cmdline.WithDependency -Dexec.args="first_argument \"second argument\""

Quick Links: Techie Stuff | General | Personal | Quick Reference