Maven command line usage

Figure 35. 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="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <mirrors>  <!-- Favour Maven Central https over http  -->
    <mirror>
      <id>central-secure</id>
      <mirrorOf>central</mirrorOf>
      <name>Favour Maven Central https over http for security</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </mirror>
  </mirrors>

  <profiles>
    <profile>
      <id>dev</id>
        <repositories>
            <repository>
              <id>archetype</id>
              <name>Supplementary MI archetypes and artifacts</name>
              <url>https://maven.mi.hdm-stuttgart.de/nexus/repository/mi-maven</url>
              <releases><enabled>true</enabled></releases>
              <snapshots><enabled>true</enabled></snapshots>
            </repository>
        </repositories>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>dev</activeProfile>
  </activeProfiles>
</settings>

https://maven.mi.hdm-stuttgart.de/nexus/repository/mi-maven refers to a Sonatype Nexus repository manager instance hosting both supplementary Maven archetypes and artifacts.


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

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

[INFO] Scanning for projects...
      ...
[INFO] BUILD SUCCESS ...

Figure 37. CLI archetype details Slide presentation Create comment in forum
mvn --batch-mode  -e archetype:generate  \
                                              \
 -DarchetypeGroupId=de.hdm_stuttgart.mi \ 
 -DarchetypeArtifactId=mi-maven-archetype-quickstart \
 -DarchetypeVersion=1.2 \
                        \
 -DgroupId=de.hdm_stuttgart.mi.sd1  \
 -DartifactId=first  \
 -Dversion=0.9

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-quickstart 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 38. Generated project layout Slide presentation Create comment in forum
> cd first        # Enter project directory
> find . -type f  # Search recursively for files
./.gitignore 
./src/test/java/de/hdm_stuttgart/mi/sd1/AppTest.java 
./src/main/java/de/hdm_stuttgart/mi/sd1/App.java 
./src/main/resources/log4j2.xml 
./pom.xml 

Exclude specific files from version git based control.

Java source file defining automated Junit tests.

Java source file containing an executable main() class.

Configuring Apache Log4j logging framework.

The Project Object Model file defining build rules.


Figure 39. 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] --------------------------------------------------------
[INFO] BUILD SUCCESS

Figure 40. Compilation file view Slide presentation Create comment in forum
> find . -type f
...
./target/classes/log4j2.xml
./target/classes/de/hdm_stuttgart/mi/sd1/App.class

exercise No. 10

What about execution? Create comment in forum

Q:

Follow the previous steps creating your own mi-maven-archetype-quickstart based project and execute mvn compile. Then try to execute the generated bytecode file:

cd target/classes
java de.hdm_stuttgart.mi.sd1.App

What happens? Explain this result.

A:

Execution fails:

> java de.hdm_stuttgart.mi.sd1.App
Exception in thread "main" java.lang.NoClassDefFoundError: 
  org/apache/logging/log4j/LogManager
	at de.hdm_stuttgart.mi.sd1.App.<clinit>(App.java:14)
Caused by:
 java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
...

As we shall see additional libraries being defined in our project's pom.xml file are missing:

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
  <modelVersion>4.0.0</modelVersion>
  ...
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.11.0</version>
    </dependency>
  ...
</project>
Figure 41. Maven test Slide presentation Create comment in forum
> mvn test
   ...
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /ma/goik/first/target/test-classes
...
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running de.hdm_stuttgart.mi.sd1.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.567 sec
...

Figure 42. Test file view Slide presentation Create comment in forum
> find . -type f
   ...
./target/test-classes/de/hdm_stuttgart/mi/sd1/AppTest.class
  ...
./target/surefire-reports/de.hdm_stuttgart.mi.sd1.AppTest.txt
./target/surefire-reports/TEST-de.hdm_stuttgart.mi.sd1.AppTest.xml
./target/classes/de/hdm_stuttgart/mi/sd1/App.class

Figure 43. 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 
         /ma/goik/first/target/first-0.9-shaded.jar
...
[INFO] BUILD SUCCESS

Figure 44. Executing first-0.9.jar Slide presentation Create comment in forum
java -jar target/first-0.9.jar
Hello World!
2018-10-03 17:49:00,700 DEBUG [main] sd1.App (App.java:24) - 
   You may configure 'src/main/resources/log4j2.xml' 
2018-10-03 17:49:00,704 DEBUG [main] sd1.App (App.java:25) -
   for adapting both console and 'A1.log' file output

exercise No. 11

Details on execution Create comment in forum

Q:

In Figure 44, “Executing 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?

Tip

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.

A:

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

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
  <modelVersion>4.0.0</modelVersion>
  ...
    <manifestEntries>
       <Main-Class>de.hdm_stuttgart.mi.sd1.App</Main-Class>
    </manifestEntries>
  ...
</project>

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

> cat 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 45. 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 46. Maven clean Slide presentation Create comment in forum
> mvn clean
...
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ first ---
[INFO] Deleting /ma/goik/first/target
[INFO] ------------------------------------------------------------------------
[