Exclude slf4j from glassfish - java

I referred so many links. I finally got this And I don't prefer this way
I want to exclude slf4j from this jar. It is glassfish embedded all.
Why do I want to Exclude?
It is a different story. If I include this slf4j, then AKKA event logging is not working due to multiple slf4j. If I exclude embedded jar completely logging works. But I need embedded jar but not glassfish slf4j.
Any Solution?
Glassfish' Pom. It seems slf4j is a transitive dependancy

Have you try something like:
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>4.0-b72</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
UPDATE
The slf4j classes are embedded. If you still want to work with an uber jar, an option is to shade glassfish-embedded-all, i.e. to create a maven project or a module with:
<dependencies>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>4.0-b72</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/org/slf4j/**/*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
and then to use the generated jar in place of glassfish-embedded-all.

Related

Java Sql Driver Class Not Found Jar With Command Line Run

I've been trying to run my Maven Kotlin Ktor project with command line. The project contains the MySQL configuration too. Every time I tried to hit the following command, I got the following error.
Here is my command.
kotlin -cp mysql-connector-java-8.0.30.jar:dev_meet_dev_api.jar MainClassKt
The dev_meet_dev_api.jar file is my project jar and mysql-connector-java-8.0.30 is for MySQL connection with the database. My project is dependent on MySQL connector that's why I added the external dependency for MySQL connector.
As soon I hit the above command on terminal I've got the following error.
Caused by: java.lang.ClassNotFoundException: java.sql.Driver
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
Edit 1: Added the maven file.
<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>spartons.com.devMeetdevApi</groupId>
<artifactId>KtorTesting</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kotlin.version>1.7.10</kotlin.version>
<ktor.version>2.1.1</ktor.version>
<junit.version>4.12</junit.version>
<serialization.version>1.4.0</serialization.version>
<coroutines.version>1.6.4</coroutines.version>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<kotlin.compiler.jvmTarget>11</kotlin.compiler.jvmTarget>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
</properties>
<dependencies>
<!-- Kotlin language dependencies -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Kotlin's coroutines dependency -->
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>${coroutines.version}</version>
</dependency>
<!-- Ktor dependencies -->
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-core-jvm</artifactId>
<version>${ktor.version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-jetty-jvm</artifactId>
<version>${ktor.version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-content-negotiation-jvm</artifactId>
<version>${ktor.version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-serialization-kotlinx-json-jvm</artifactId>
<version>${ktor.version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-status-pages-jvm</artifactId>
<version>${ktor.version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-cors-jvm</artifactId>
<version>${ktor.version}</version>
</dependency>
<!-- Kotlin serialization version -->
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-json</artifactId>
<version>${serialization.version}</version>
</dependency>
<!-- MySQL connector dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<finalName>dev_meet_dev_api</finalName>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>kotlinx-serialization</plugin>
</compilerPlugins>
<args>
<arg>-opt-in=kotlin.RequiresOptIn</arg>
</args>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-serialization</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
</executions>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactSet>
<excludes>
<exclude>mysql:mysql-connector-java:jar:</exclude>
</excludes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</plugin>
</plugins>
</build>
</project>
Edit 2: Added the java-11 modules
P.S I'm using Java 11. Projects works fine if I try to run with the IntelliJ.
You can do this directly in Maven! In your pom.xml add the driver as a dependency.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
Then you shade the dependency into the jar
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>mysql:mysql-connector-java</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then directly execute the jar. If this does not help may I know which Java version you are using?
Kotlin applications need the Kotlin runtime to run (mainly kotlin-stdlib.jar). It can be included
in the application jar or not.
If the runtime is not included, you must add it to the classpath:
java -cp /path/to/kotlin-stdlib.jar:app.jar MainClassKt
You can also use the kotlin script, which automatically adds it:
kotlin -cp app.jar MainClassKt
In your case, the runtime is included thanks to the maven-shade-plugin, so you can run the app like this:
java -cp mysql-connector-java-8.0.30.jar:dev_meet_dev_api.jar MainClassKt
Possibility 1
It is possible that some dependency that you manually added has a different version of java.sql.Driver.
It is overriding the java.sql.Driver dependency version that your code flow needs.
To debug if this is actually the cause of your problem, you can use IntelliJ maven dependency plugin.
It can show if there is any conflict in dependency causing the needed class to not load.
https://www.jetbrains.com/help/idea/work-with-maven-dependencies.html#maven_dependency_diagram
Also, please note that in my experience, If multiple dependencies are loading different versions of same class, then you may get different results in different setup, since order of class loading may differ. This can result in your code working in IntellijIdea, but not when running from terminal.
Possibility 2
Check if the jdbc driver version is compatible with java and database versions that you are using. Try upgrading or downgrading the jdbc version to see if that resolves issue.
Debug approach 1: if above ways don't fix the issue
Since you say that your code works in Intellij, start your application in debug mode and put a debug point in java.sql.Driver.
See which the library which contains this class and the dependency which contains this library.
That can help you debug the cause of problem.
Debug approach 2: if above ways don't fix the issue
Use remote debugging.
Start your jar application in console with remote debug enabled.
Refer this on steps to do it.
https://www.jetbrains.com/help/idea/tutorial-remote-debug.html#ecd9fef1
Using this, you can find the code flow and the cause of problem for program that runs in console.
I hope that this helps you.

Maven build has missing package linkages

I am having problems using Maven. I have an Apache Flink project and wanted to run it on my server. Locally it runs fine but on the server it aborts with the error:
java.lang.NoClassDefFoundError: org/apache/flink/examples/java/ml/util/LinearRegressionData
In my Java project I imported the class with
import org.apache.flink.examples.java.ml.util.LinearRegressionData;
And I used the correct class at the import:
After the build I had a look into the Jar file. The following classes were included:
The /util/ folder is completely missing. My dependency-section in the pom file looks like the following:
<dependencies>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-core</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-runtime</artifactId>
<version>0.9.0</version>
</dependency>
</dependencies>
When I have seen the package organization in the repository located at https://github.com/apache/flink/tree/release-0.9, I thought it would be possible to add the following lines to flink:
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-examples</artifactId>
<version>0.9.0</version>
</dependency>
But these dependencies cannot be resolved. Since Maven does not throw an error when performing a clean install, I think this is a dependency issue. I thought Maven would include all used imports automatically. How can I make this runnable on my server?
You should include ML example as follows:
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java-examples</artifactId>
<version>0.9.0</version>
</dependency>
flink-examples is a parent pom module; not a jar module. How do you build your jar file? Using maven-jar-plugin? A regular mvn package or mvn install does not packages dependencies.
Using maven-jar-plugin requires to specify what you need to include using <include>. See here: https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html
In your case it should be something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Main-Class>org.apache.flink.examples.java.ml.LinearRegression</Main-Class>
</manifestEntries>
</archive>
<includes>
<include>org/apache/flink/examples/java/ml/*.class</include>
<include>org/apache/flink/examples/java/ml/util/*.class</include>
</includes>
</configuration>
</plugin>
You can also compare with https://github.com/apache/flink/blob/master/flink-examples/flink-java-examples/pom.xml
You also need to "pull" and unpack your dependencies into your project such that they can be re-packages using maven-dependency-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version><!--$NO-MVN-MAN-VER$-->
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java-examples</artifactId>
<version>0.9.0</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

Exclude resources from a dependent jar with Maven

I have a third party jar file that I have to use, but unfortunately it contains an embedded log4j configuration file as a resource. When I include the third party jar as a Maven dependency, I also pick up their log4j configuration, which overrides my own.
Is there a way to tell Maven to include a jar dependency, while excluding a specific resource within that jar?
The short answer is NO
But, if you wish to use your own log4j configuration set the System Property named log4j.configuration using -Dlog4j.configuration=somefile.properties or similar.
See this SO-answer for more info.
Yes, you can use maven shade plugin for that. You can use filters to exclude the resource.
A working example of how to achieve this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade.plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>logback.xml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
Use
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
</exclusion>
</exclusions>
</dependency>

Dependency on dependency leading to SNAPSHOT dependency in Maven

I am trying to use Maven for a Java Application I am writing and am trying to use Java2WSDL with
<plugin>
<groupId>org.apache.axis2.maven2</groupId>
<artifactId>axis2-java2wsdl-maven-plugin</artifactId>
<version>${java2wsdl.version}</version>
<configuration>
<className>com.barclays.hypercube.marketdata.Model.PointSeriesClient</className>
</configuration>
<executions>
<execution>
<goals>
<goal>java2wsdl</goal>
</goals>
</execution>
</executions>
</plugin>
It seems that there is a dependency on
org.apache.ws.commons.axiom:axiom-api:jar:SNAPSHOT
Unsurprisingly this is not available from my Maven repository.
Is there any way to override this? Perhapos with a value in my settings.xml file.
You can exclude that unwanted jar from this maven. But first make sure you do not need this jar. Like here
<project>
...
<dependencies>
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

Maven jaxws plugin - skip execution

I'm using the JAX-WS maven plugin (org.jvnet.jax-ws-commons:jaxws-maven-plugin version 2.2) to generate classes from a bunch of WSDL files in my project, and as the WSDLs never really change I would like to disable this code generation by default, and only enable it for a particular maven profile I've created. The element of this plugin supports a element, but setting this to true seems to do nothing. Am I doing something wrong here? Or is this a known bug, and is there something else I could do to avoid this code generation?
My plugin configuration looks like this:
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>import-wsdld</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlFiles>
<wsdlFile>MyWSDL.wsdl</wsdlFile>
</wsdlFiles>
</configuration>
</execution>
</executions>
<configuration>
<skip>true</skip>
<packageName>com.my.package</packageName>
<wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
<keep>true</keep>
<xnocompile>true</xnocompile>
<sourceDestDir>src/main/java</sourceDestDir>
<verbose>false</verbose>
</configuration>
<!-- Necessary to revert back to 2.1.7 -->
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-tools</artifactId>
<version>2.1.7</version>
<exclusions>
<exclusion>
<groupId>org.jvnet.staxex</groupId>
<artifactId>stax-ex</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jvnet.staxex</groupId>
<artifactId>stax-ex</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</plugin>
Many thanks,
Joseph.
Well, just do it (I mean plugin declaration with all its stuff) in <profile> block. I wouldn't rely on some magic plugin-specific solutions. Just use what Maven offers out-of-the-box and create <profile> with your <plugin> stuff.
Based on the documentation of the plugin it has not "skip" parameter which of course means it will not support. The best solution is to put that into a profile as mentioned before.

Categories