Add dependecy to Maven and set to $CATALINA_HOME/shared/lib - java

I'm using Matlab MCR in web project so I imported these dependecies to pom.xml
<!-- Matlab client tool library -->
<!-- <dependency>
<groupId>DataConcatenation</groupId>
<artifactId>DataConcatenation</artifactId>
<version>0.0.5-SNAPSHOT</version>
</dependency> -->
<!-- <dependency>
<groupId>DataConcatenator</groupId>
<artifactId>DataConcatenator</artifactId>
<version>0.0.5-SNAPSHOT</version>
</dependency> -->
<!-- <dependency>
<groupId>DataConversion</groupId>
<artifactId>DataConversion</artifactId>
<version>0.0.5-SNAPSHOT</version>
</dependency> -->
<dependency>
<groupId>DataConverter</groupId>
<artifactId>DataConverter</artifactId>
<version>0.0.5-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>DataConcatenation</artifactId>
<groupId>DataConcatenation</groupId>
</exclusion>
<exclusion>
<artifactId>DataConcatenator</artifactId>
<groupId>DataConcatenator</groupId>
</exclusion>
<exclusion>
<artifactId>DataConversion</artifactId>
<groupId>DataConversion</groupId>
</exclusion>
</exclusions>
</dependency>
The first problems is that I have to exclude the other tree dependencies even if I use only DataConverter, But I need only dataConverter and this library doesn't have other dependecies.
The second and most important problem is this error:
Threw exception in ZipAndMat::createZipAndMat: java.lang.UnsatisfiedLinkError: Native Library /usr/v81/bin/glnxa64/libnativedl.so already loaded in another classloader
I read a lot of guide and I understand that I have to put this jar into $CATALINA_HOME/shared/lib so all the class loader share the same jar.
But how can I add this dependecy to the above path? This is the first time that I have to implement this configuration. I use tomcat on my server and deploy project through war file.
Thanks

You can place the jar into $CATALINA_HOME/shared/lib, and then specify in your POM the dependency's <scope> as provided:
<dependency>
<groupId>DataConverter</groupId>
<artifactId>DataConverter</artifactId>
<version>0.0.5-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
...
</exclusions>
</dependency>
This method is often used for jars which contain JDBC drivers when running on Tomcat; the jdbc jar is placed in $CATALINA_HOME/shared/lib (so all WARs can find it), yet each project list’s the JDBC jar as a dependency with provided as the scope.
See this post for more information on provided.

Related

Exlude package from maven dependency

I have error java: module com.example.learningfx reads package jfxtras.labs.util.event from both vworkflows.fx and jfxtras.labs and I think I need to exclude package from jfxtras.labs or vworkflow.fx. Jow can I not include some packages from dependency? Do I need to make this in pom.xml or module-info.java? I use maven and IntelliJ. I think there is already some questions like this but I don't find it
You can only include/exclude whole dependencies, not packages from dependencies.
See https://maven.apache.org/pom.html#Exclusions
You do not provide complete groupId:artifactId:version coordinates, so the example cannot be exact:
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>vworkflows.fx</artifactId>
<version>...</version>
<exclusions>
<exclusion>
<groupId>org.jfxtras</groupId>
<artifactId>jfxtras.labs.util.event</artifactId>
</exclusion>
</exclusions>
</dependency>
...

SLF4J Logback LoggerContext Conflicts

I am working on deploying a spring boot executable jar for my application. Im using SLF4J logging, and when I build and run inside of IntelliJ I have no issues.
However, when I try to run the .jar, from the command line I get a LoggerFactory is not a Logback LoggerContext but Logback is on the classpath exception.
It complains about the slf4j-log4j12-1.7.12.jar in two places /opt/mapr/lib/ and /opt/mapr/hadoop/hadoop-2.7.0/share/hadoop/common/lib/.
If i remove the jar from both places and run my app:
java -cp $(mapr classpath):MapRProducerApp-0.0.1-SNAPSHOT.jar org.springframework.boot.loader.PropertiesLauncher
it will then fail startup due to SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
I dont understand why it would fail saying faild to load the slf4j class when my apps .jar is built with that dependency through
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
In IntelliJ, I am bringing in all external dependencies that exist inside the /opt/mapr/lib folder, which includes the slf4j-log4j12-1.7.10.jar, but IntelliJ does not give the Logback LoggerContext error.
To recap here:
Building and running the jar on its own works, but will fail because it needs dependencies that exist inside the mapr classpath.
Running the jar with the mapr classpath fails because of the slf4j jar inside /opt/mapr/lib.
Removing that jar leads to another failure about a slf4j jar in /opt/mapr/hadoop/hadoop-2.7.0/share/hadoop/common/lib/.
Removing that jar then fails the app because it now cannot find any slf4j binding.
Is there something im missing? Do I need to package my app a certain way so it does not include the SLF4J dependency?
I'm not clear about your <dependencies> hierarchy, but by adding <exclusion> you can simply resolve the conflict.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
If you are using Spring-boot actuator, only exclusion of spring-logging might not help.
You will have to provide logging for it. Spring Boot support log4j for logging configuration.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Refrence: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.logging

Spring-Boot: How can we remove some dependencies from Effective pom?

I am using spring-boot 2.0.3.RELEASE. When I am clicking on "show Effective POM" option by using IntelliJ IDEA, it loads Effective POM. And there I can see a few dependencies that my client don't want to have at there side.
Is there any way to tell Maven not to include these dependencies? How can we exclude dependencies from effective poms?
Maven provides a way to exclude dependencies with the exclude tag
Here is an example taken from the documentation website https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html
<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>
The idea is to locate parent dependencie from where you are getting deps you don't want and add an exclusion tag.
If they are needed in runtime you can specify the scope to provided
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
That will tell maven to use the deps to compile but not no include them in the target package, and they will be provided in the production environment by the JVM executing the code.
Hope this helps

Cross dependency issue with Google Collection and Google Guava on Shrinkwrap-resolver

I'm dealing with the development of a Java EE project that involves several tools such as jBPM, Hibernate, Resteasy, ect.
In order to manage dependencies, I'm using Maven: my pom.xml is available here.
Now, I'd like to use inside that project QueryDSL 3.4.3 that depends on Google Guava 14.0.1: unfortunately, something imports as dependency Google Collections 1.0 that generates a conflict with Google Guava 14.0.1.
Is it possible to understand where Google Collections is from?
Is there a way to resolve this issue safety? (Now, I'm just removing Google Collections's jar from the deployment folder)
Update
By using the command mvn dependency:tree, I found that Google Collections 1.0 comes from:
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-impl-maven</artifactId>
</dependency>
Now, I've just to understand if it will work well also by excluding google-collections.
See also: http://grepcode.com/.../shrinkwrap-resolver-impl-maven/2.1.1/
As said, Google Collection dependency comes from shrinkwrap-resolver-impl-maven.
I resolved that issue by editing the pom.xml as follows:
<!-- ShrinkWrap Maven Resolver for Arquillian Tests -->
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-impl-maven</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
</exclusion>
</exclusions>
</dependency>
Then:
<!-- Arquillian profiles -->
<profiles>
<!-- Arquillian test profile managed by JBoss AS 7 -->
<profile>
<id>arquillian-jbossas-managed</id>
<dependencies>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-arquillian-container-managed</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>google-collections</artifactId>
<groupId>com.google.collections</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>
</profiles>
Now, it works fine.

Maven dependency on zip artifact

I have a project that needs to depend on a ZIP file which is produced by another project. That 'other project' is not under my control. The ZIP file is required for correctly building my project. It is not required for execution of my project. I need Maven to download the ZIP file for me.
I currently create a dependency on the ZIP artifact like this:
<dependency>
<groupId>org.foo</groupId>
<artifactId>zeus</artifactId>
<version>1.1</version>
<type>zip</type>
<scope>test</scope>
</dependency>
My problem is the scope. If I use anything but test it brings with it a lot of transitive dependencies from 'other project' which screws up my own project. Using test as the scope actually does the job but it shows in my IDE as a Test-dependency. So I feel I'm doing something wrong. This is not a test-dependency!
I've looked through the available Maven scopes ('compile', 'provided', etc) and I just cannot seem to find one that matches my use case. Am I doing something wrong?
You can just exclude all transitive dependencies with wildcard:
<dependency>
<groupId>org.foo</groupId>
<artifactId>zeus</artifactId>
<version>1.1</version>
<type>zip</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
(source Exclude all transitive dependencies of a single dependency)
You have to declare all transitive dependencies as exclusions:
<dependency>
<groupId>org.foo</groupId>
<artifactId>zeus</artifactId>
<version>1.1</version>
<type>zip</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.foo</groupId>
<artifactId>transitive-dep-1</artifactId>
</exclusion>
<exclusion>
<groupId>org.foo</groupId>
<artifactId>transitive-dep-2</artifactId>
</exclusion>
<!-- add all transitive deps. -->
</exclusions>
</dependency>

Categories

Resources