Maven command line usage

Figure 43. Supplementary MI Maven archetypes Slide presentation Create comment in forum

Create an empty .m2 Maven related directory below your home directory ~. The -p option avoids an error message in case ~/.m2 already exists.

Use a text editor creating (or adding) the following content in ~/.m2/settings.xml. Any text editor like (vim, nano, emacs, IntelliJ,...) will do:

<settings xmlns=""

  <mirrors>  <!-- Favour Maven Central https over http  -->
      <name>Favour Maven Central https over http for security</name>

              <name>Supplementary MI archetypes and artifacts</name>

</settings> refers to a Sonatype Nexus repository manager instance hosting supplementary Maven archetypes and artifacts.

Now test your configuration by creating a dummy project (copy and paste to a terminal window):

Figure 44. CLI archetype test Slide presentation Create comment in forum
mvn --batch-mode -e archetype:generate -DgroupId=de.hdm_stuttgart.mi.sd1 \
-DartifactId=second -Dversion=0.9 -DarchetypeGroupId=de.hdm_stuttgart.mi \
-DarchetypeArtifactId=mi-maven-archetype-helloworld -DarchetypeVersion=1.4

[INFO] Scanning for projects...

Figure 45. CLI archetype details Slide presentation Create comment in forum

Defining archetype and project details:

mvn --batch-mode  -e archetype:generate  \
 -DarchetypeGroupId=de.hdm_stuttgart.mi \ 
 -DarchetypeArtifactId=mi-maven-archetype-helloworld \
 -DarchetypeVersion=1.4 \
 -DgroupId=de.hdm_stuttgart.mi.sd1  \
 -DartifactId=first  \

During project generation Maven shall work in batch mode not asking for user input.

Create a Maven project using an archetype being specified by .

archetypeGroupId, archetypeArtifactId and archetypeVersion uniquely identify an archetype worldwide.

mi-maven-archetype-helloworld denotes one of several available archetypes.

Likewise groupId, artifactId and version are intended to uniquely define a project being generated. Moreover the groupId property defines the project's default base package to be explained in Objects and Classes.

Figure 46. Generated project layout Slide presentation Create comment in forum
> cd first        # Enter project directory
> find . -type f  # Search recursively for files

Exclude specific files from version git based control.

Java source file containing an executable main() class.

The Project Object Model file defining build rules.

Figure 47. Maven compile Slide presentation Create comment in forum
> mvn compile
[INFO] Scanning for projects...
[INFO] Building first 0.9
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /ma/goik/first/target/classes
[INFO] --------------------------------------------------------

Figure 48. Compilation file view Slide presentation Create comment in forum
> find . -type f

Figure 49. Execution Slide presentation Create comment in forum
> cd target/classes 
> java de.hdm_stuttgart.mi.sd1.App 
Hi there, let's have
fun learning Java! 

Change to base directory containing compiled Java classes.

Application execution. Note:

Our App class is being prefixed by the package name de.hdm_stuttgart.mi.sd1 defined by the groupId parameter in Figure 45, “CLI archetype details ”.

The expected output result.


Executing this particular class requires a configuration in our project's pom.xml file:

</archive> ...

Figure 50. Maven package Slide presentation Create comment in forum
> mvn package
 T E S T S
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] Building jar: /ma/goik/first/target/first-0.9.jar
[INFO] Replacing /ma/goik/first/target/first-0.9.jar with 

Figure 51. Executing Java archive first-0.9.jar Slide presentation Create comment in forum
java -jar target/first-0.9.jar
Hi there, let's have
fun learning Java!

Remark: This will execute HelloWorld.class being contained in first-0.9.jar.

exercise No. 10

Details on execution Create comment in forum


In Figure 51, “Executing Java™ archive first-0.9.jar we saw successful execution of:

java -jar target/first-0.9.jar

How does this actually work? There might be multiple executable classes containing main() methods within a given project? How is de/hdm_stuttgart/mi/sd1/App.class being selected for execution?


Have a closer look on your project's pom.xml. Also unzip your generated first-0.9.jar file to examine the contained file META-INF/MANIFEST.MF.


Our pom.xml defines a single class which must contain a public static void main(String[] args) method:

<project xmlns="" ...>

The Maven package processing step wraps this class name into the generated first-0.9.jar archive's manifest file META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.5.2
Built-By: goik
Build-Jdk: 10.0.2
Main-Class: de.hdm_stuttgart.mi.sd1.App

This allows the Java runtime to choose the class to be executed.

Figure 52. Maven javadoc:javadoc Slide presentation Create comment in forum
> mvn javadoc:javadoc
[INFO] Scanning for projects...
Generating /ma/goik/First/target/site/apidocs/allclasses-noframe.html...
Generating /ma/goik/First/target/site/apidocs/index.html...
Generating /ma/goik/First/target/site/apidocs/overview-summary.html...
Generating /ma/goik/First/target/site/apidocs/help-doc.html...

See e.g. class String documentation.

Figure 53. Maven clean Slide presentation Create comment in forum
> mvn clean
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ first ---
[INFO] Deleting /ma/goik/first/target
[INFO] ------------------------------------------------------------------------