FrenchCornichon
FrenchCornichon

Reputation: 13

How to add Slf4j to the compiled jar after creating it

I have an issue here and I cannot wrap my head around it.

I need to add Slf4j dependency to my compiled jar after doing "mvn clean install".

This is because, if i try to run my jar app, I get this exception:

Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
        ... 1 more

From what I understood, it's because the slf4j is not in the jar after doing the compilation

I tried to do a workaround it with something like this :

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>${route.of.my.package}.lib/</classpathPrefix>
                                <mainClass>${route.of.my.package.MainClass}</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>

But still , nothing changed.

These are the dependencies added for the slf4j :

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-reload4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-reload4j</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>

I know I can use this method and do mvn compile assembly:assembly , to put everything into one jar with dependencies, and this totally works localy

                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>

But for distribution purposes, i'm forced to make it enter the jar after doing "mvn clean install" .

Thank you for your time and help.


So I tried to make a pom using shade. My goal here was to be able to do "mvn clean install" , find my Application.jar in my target directory, with all the dependencies inside. This is what my in the pom.xml actually looks like. I still can't acheive my goal

<build>
    <finalName>MainApp-${project.version}</finalName>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.4.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>path.to.main.app.MainApp</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    </build>

Upvotes: 0

Views: 410

Answers (2)

FrenchCornichon
FrenchCornichon

Reputation: 13

So thank you for the ideas you gave me, I managed to find the solution here : https://github.com/limegurutech/create-uber-jar/blob/master/pom.xml It works perfectly for me.

Have a good day !

Upvotes: 0

g00se
g00se

Reputation: 4296

This is mine. Note the making of the main class into a property. This makes it a lot more flexible though of course you must set that property somehow

        <plugin>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>${main.class}</Main-Class>
                                    <Build-Number>1</Build-Number>
                                </manifestEntries>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Of course, you should use your own Shade plugin version number

Upvotes: 0

Related Questions