Contents

Maven project basic

This is a note for starting maven project from creating project to running application with configuration for external library dependencies.

Environment

  • Mac OS Monterey 12.0
  • Java 17.0.1 2021-10-19 LTS
  • Apache Maven 3.8.3

1. Generate project structure

1
mvn archetype:generate -DgroupId=io.hommalab.app -DartifactId=multiplication-client -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

This generates project structures as below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
log4j-sample
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── io
    │           └── hommalab
    │               └── app
    │                   └── App.java
    └── test
        └── java
            └── io
                └── hommalab
                    └── app
                        └── AppTest.java

2. Add package dependency

As an example, adding log4j libraries.

pom.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  <dependencies>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.14.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.14.1</version>
    </dependency>
  </dependencies>

Refs.


3. Update source code

Update main class using log4j like:

App.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package io.hommalab.app;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class App 
{
    public static void main( String[] args )
    {
        Logger logger = LogManager.getLogger("Logger");
        int ans =  4 * 5;

        System.out.println( "Hello World!:" +  ans );

        logger.info(ans);
        logger.warn("WARNING");
        logger.error("ERROR");
    }
}

4. Add definition for manifest file to pom.xml

Update pom.xml for adding classpath to external libraries.

  • mainclass: Adding path to main class to execute by java -jar xxx.jar command.
  • addClasspath: true for add classpath
  • classpathPrefix: add lib as a prefix directory in target ( libraries will be add in target/lib)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
    <archive>
        <manifest>
        <mainClass>io.hommalab.app.App</mainClass>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib</classpathPrefix>
        </manifest>
    </archive>
    </configuration>
</plugin>

5. Build package

5.1 mvn install package

Install and create package to target directory.

1
2
3
4
5
6
7
8
9
$ mvn clean install package

...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.283 s
[INFO] Finished at: 2021-11-16T13:57:06+09:00
[INFO] ------------------------------------------------------------------------

This generates:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
arget
├── classes
│   └── io
│       └── hommalab
│           └── app
│               └── App.class
├── generated-sources
│   └── annotations
├── generated-test-sources
│   └── test-annotations
├── log4j-sample-1.0-SNAPSHOT.jar
├── maven-archiver
│   └── pom.properties
├── maven-status
│   └── maven-compiler-plugin
│       ├── compile
│       │   └── default-compile
│       │       ├── createdFiles.lst
│       │       └── inputFiles.lst
│       └── testCompile
│           └── default-testCompile
│               ├── createdFiles.lst
│               └── inputFiles.lst
├── surefire-reports
│   ├── TEST-io.hommalab.app.AppTest.xml
│   └── io.hommalab.app.AppTest.txt
└── test-classes
    └── io
        └── hommalab
            └── app
                └── AppTest.class

5.2. deploy external libraries to target

Retrieve META-INF/MANIFEST.MF to target.

1
jar xf target/log4j-sample-1.0-SNAPSHOT.jar META-INF/MANIFEST.MF

This generates META-INF/MANIFEST.MF

1
2
3
4
5
6
Manifest-Version: 1.0
Created-By: Apache Maven 3.8.3
Built-By: tato
Build-Jdk: 17.0.1
Class-Path: lib/log4j-api-2.14.1.jar lib/log4j-core-2.14.1.jar
Main-Class: io.hommalab.app.App

Copy dependencies to target/lib

1
mvn dependency:copy-dependencies -DoutputDirectory=target/lib -DincludeScope=compile

This generates:

1
2
3
target/lib
├── log4j-api-2.14.1.jar
└── log4j-core-2.14.1.jar

6. Run application

Run application:

1
2
3
java -jar target/log4j-sample-1.0-SNAPSHOT.jar
Hello World!:20
14:05:59.775 [main] ERROR Logger - ERROR

If you do not specify mainclass in META-INF/MANIFEST.MF in jar, running application by

java -cp target/log4j-sample-1.0-SNAPSHOT.jar io.hommalab.app.App

Warning
Cannot use log4j2.xml configuration in this project as of 16 November. Need to check how to add classpath for external configuration file.

Sources

github


References