Multi modules Maven project – Start

This part follows the tutorial on Jdk 8 and Jdk 11 LTS docker image.

We will actually use the later image to build our project which is prepared here.

The article is focused on generating a project using the IntelliJ IDE.

The project features the following parts :

  • A main parent module
  • 2 children modules

The main parent module and the technical module are introduced on this guide. The next article of the series will talk about the integration of Spring Batch.

  • 1 module for logs management (Technical module – logback)
  • 1 module featuring Spring batch (a simple example)

Pre-requesites / IDE

Rather than using a built-in maven package, we will use our own docker image which embeds both Jdk 8 and Jdk 11 Lts.

By default the project is set to run on a Jdk 8 environment.

I won’t details the process of using the IDE, I’m using IntelliJ IDEA for Java development.

With IntelliJ you can target a specific JDK on a per module basis. You may use the IDE of your choice (Eclipse, Visual Studio…).

Initializing the project

Maven Parent module

Simply create an empty Maven project. In case default files are being added, you can just delete them so it only remains the following pom 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>net.linkstraffic</groupId>
    <artifactId>SimpleMulti</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>
</project>
Maven Parent module

The default Jdk version used by our project is also set within the pom. This makes sure we are not using the Jdk 5.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>net.linkstraffic.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

Now head towards the root path of your project and issue the following command :

$ docker run -it –rm  -v $(pwd):/usr/src/mymaven -w /usr/src/mymaven jdk8/jdk11 mvn clean install

Maven process - Using docker
mvn clean install using the Docker image

Technical module for logs

We will dedicate an entire module just for logging dependencies, all other modules will depend on this one.

Additional common technical libraries could be added in this module.

For this tutorial, no real application will be implemented in the module but we could imagine shipping various tools used in other modules.

The use of such module allows us to centralize the dependencies and their version.

Simply add a new module using your IDE, no archetype needed.

 

Maven technical module

As you can see, a new folder is created with a new pom.xml file, the root pom is also altered to include this :

<modules>
    <module>SimpleTechnical</module>
</modules>

We chose Logback to manage our logs. Let’s fill in the dependency within our new pom :

<?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">
    <parent>
        <artifactId>SimpleMulti</artifactId>
        <groupId>net.linkstraffic</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SimpleTechnical</artifactId>

    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.3.0-alpha5</version>
        </dependency>
    </dependencies>

</project>

As specified by the architecture documentation of Logback, we only need to add the classic dependency which itself provides both sl4j API and Logback Core libraries.

A main program just for fun

No real application but we will make sure (without any test, sorry !) that the logs are correctly displaying.

Following the architecture detailed here, we use all level types :

package net.linkstraffic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {

        logger.trace("trace log");
        logger.debug("debug log");
        logger.info("info log");
        logger.warn("warn log");
        logger.error("error log");
    }
}

And as we like when things are well set, we add a logback.xml file to play with the log level within our classes :

<configuration debug="true">

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are  by default assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="ERROR">
        <appender-ref ref="STDOUT" />
    </root>

    <logger name="net.linkstraffic.Main" level="TRACE" />
</configuration>

We’ve set the log level of our class to print out Trace logs – that will cover all types of levels. I’ll let you play around with the levels if you want 🙂

Run Java program with logback

Additionally, the assembly plugin provided by Maven bundles the dependencies within our Jar and we make it executable.

Using the following configuration for our Technical module, we will be able to use the Jar to execute our Main program.

<?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">
    <parent>
        <artifactId>SimpleMulti</artifactId>
        <groupId>net.linkstraffic</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SimpleTechnical</artifactId>

    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.3.0-alpha5</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>
                                        net.linkstraffic.Main
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Compile/Run using the docker image

To make sure everything is working as expected, let’s build our project using the image we have set up :

docker run -it --rm  -v $(pwd):/usr/src/mymaven -w /usr/src/mymaven jdk8/jdk11 mvn clean install
Docker build of multi module maven

The only module which features a Jar at the moment is the Technical one, so you should end up having the following file within your target folder :

SimpleTechnical-1.0-SNAPSHOT-jar-with-dependencies.jar

Now launch the program from the command line :

java -jar SimpleTechnical/target/SimpleTechnical-1.0-SNAPSHOT-jar-with-dependencies.jar

If everything went as expected, you should be getting the following result :

maven technical module run

That’s it for today ! I hope you enjoyed it, in the next tutorial we will complete the project by adding another module featuring Spring Batch and it will use Jdk11 LTS.

See you around !

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Want more information?

Related links will be displayed within articles for you to pick up another good spot to get more details about software development, deployment & monitoring.

Stay tuned by following us on Youtube.

%d bloggers like this: