StringUtil defaultIfBlank disappearing after adding kube java client - java

I'm adding the following dependencies to my maven project:
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>10.0.0</version>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java-extended</artifactId>
<version>10.0.0</version>
</dependency>
However, after adding this, StringUtils.defaultsIfBlank disappears and my code fails to compile. I've tried adding the following exclusions to these dependencies:
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</exclusion>
</exclusions>
I found that the StringUtils class that goes into the classpath is coming from io.sundr:sundr-codegen:0.22.0 which is a dependency from Kube and is packed as an uber jar. this seems odd, how do I make the StringUtils in my direct dependency to take precendence?

You can see which version of commons-lang is being pulled in by using the dependency:tree goal. It sounds like perhaps one of these dependencies you list above is transitively bringing in a different version of commons-lang than you are implicitly requiring.

Related

Why do I get a NoSuchMethodError in my java unit tests but the IDE doesn't show classpath problems?

I have a unit test that uses some methods of the com.google.guava API, that is included as sub-dependencies in some other dependencies I declared in my pom.xml.
The IDE, in my case eclipse, didn't show any problems.
But when I run the test I get the error:
com.google.common.util.concurrent.ExecutionError: java.lang.NoSuchMethodError: com.google.common.base.Objects.firstNonNull(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
I have seen other users who fixed this problem by excluding guava like this:
<dependency>
<groupId>com.atlassian.activeobjects</groupId>
<artifactId>activeobjects-test</artifactId>
<version>${ao.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
but that didn't help. Instead it also caused classpath errors in my non-test code.
What can I do instead?
In my case, this error was fixed by using a newer version of activeobjects (2.0.0 --> 3.0.0)
<properties>
<ao.version>3.0.0</ao.version>
</properties>
I found out that if you explicitly add the dependency for guava in your pom, the error is gone!
So I just added this dependency:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>

Couldn't understand managed version under Maven dependency hierarchy

In my pom.xml, I have a SikuliX Jar which has a transitive dependency on jna-platform.
As seen in below image, version 4.5.2 has overrided version 5.4.0.
But i dont understand, how this version is overrided as i have not specified any dependency for jna-platform. I had also verified that no any there dependency is fetching this jar.
Please help me understand why this is happening. Any detailed document is well appreciated.
Related dependencies:-
<dependency>
<groupId>com.sikulix</groupId>
<artifactId>sikulixapi</artifactId>
<version>2.0.4</version>
<exclusions>
<exclusion>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
</exclusion>
</exclusions>
</dependency>
Thanks
Since you were using spring boot, as suggested here (there's also the reason of this behaviour):
java.lang.NoClassDefFoundError: com/sun/jna/platform/win32/SspiUtil$ManagedSecBufferDesc #882
you can change your order of dependencies, or specify the exact version, like this:
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.4.0</version>
</dependency>
or add this property:
<jna.version>5.4.0</jna.version>

elasticsearch lib can not add commons-beanutils and commons-collections Simultaneously

I made a native java plugin for elasticsearch, and I imported the net.sf.json jar in my plugin. So I added the following jar in path\elasticsearch-2.0.0\lib:
commons-beanutils-1.8.0
commons-collections-3.2.1
commons-lang-2.5
commons-logging-1.1.1
ezmorph-1.0.6
json-lib-2.4-jdk15
But when I run ./elasticsearch, it shows the following exception:
Exception in thread "main" java.lang.IllegalStateException: jar hell!
class: org.apache.commons.collections.ArrayStack
jar1: D:\Program Files\elasticsearch-2.0.0\lib\commons-beanutils-1.8.0.jar
jar2: D:\Program Files\elasticsearch-2.0.0\lib\commons-collections-3.2.1.jar
at org.elasticsearch.bootstrap.JarHell.checkClass(JarHell.java:267)
at org.elasticsearch.bootstrap.JarHell.checkJarHell(JarHell.java:185)
at org.elasticsearch.bootstrap.JarHell.checkJarHell(JarHell.java:86)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:156)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:270)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35)
Refer to the log for complete error details.
Referenced checkClass, I guess it because that commons-beanutils-1.8.0.jar and commons-collections-3.2.1.jar both have path org\apache\commons\collections.
How can I deal with it?
And is there other way to add extra jar for plugin in elasticsearch?
Since you're using Maven, you can use dependency exclusions in your pom.xml for the json-lib artifact:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
<exclusions>
<exclusion>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</exclusion>
<exclusion>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</exclusion>
</exclusions>
</dependency>
What will happen is that the compile-time dependencies for json-lib will not be used. Note that you run the risk of creating conflicts since your version of json-lib might depend on different versions of commons-beanutils and commons-collections, but that's worth a try.
UPDATE
Then you simply need another exclusion for your direct commons-beanutils dependency:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.0</version>
<exclusions>
<exclusion>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</exclusion>
</exclusions>
</dependency>
My temporarily way to solve this question is remove the folder org.apache.commons.collections in commons-beanutils jar.
because I find that the class files in this folder are also contained in commons-collections.
Also the way is very not smart, it works, and I just need remove and add to lib file once.
Or use other version of commons-beanutils that does not contain collections forder, like version 1.9.2
UPDATE
Thanking #Val's thinking, the better pom.xml:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
<exclusions>
<exclusion>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>

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>

What's wrong with my Apache CXF client?

This is part of my pom.xml:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
<scope>runtime</scope>
</dependency>
I'm trying to use Apache CXF as an implementation of JAX-WS. Everything works fine (Java code is generated from WSDL by means of org.apache.cxf:cxf-codegen-plugin:2.4.0), until execution:
java.lang.NoSuchMethodError:
javax.wsdl.xml.WSDLReader.readWSDL(Ljavax/wsdl/xml/WSDLLocator;Lorg/w3c/dom/Element;)Ljavax/wsdl/Definition;
at org.apache.cxf.wsdl11.WSDLManagerImpl.loadDefinition(WSDLManagerImpl.java:237)
at org.apache.cxf.wsdl11.WSDLManagerImpl.getDefinition(WSDLManagerImpl.java:186)
at org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:91)
at org.apache.cxf.jaxws.ServiceImpl.initializePorts(ServiceImpl.java:203)
at org.apache.cxf.jaxws.ServiceImpl.<init>(ServiceImpl.java:147)
at org.apache.cxf.jaxws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:90)
at javax.xml.ws.Service.<init>(Service.java:56)
....
What is it about? What dependency did I miss?
You likely have a 1.5 (or older) version of wsdl4j coming from someplace else. CXF requires the 1.6.x versions.
EDIT:
Also be on the lookout for the Axis version of this jar. You can exclude it like so:
<exclusions>
<exclusion>
<artifactId>axis-wsdl4j</artifactId>
<groupId>axis</groupId>
</exclusion>
</exclusions>
This dependency is wsdl4j-1.6.2.jar, it probably does not get transitively resolved with the current set of dependencies you have.

Categories

Resources