How to determine where a dependency is used? (Java, Maven) - java

When using Maven with Java, is it possible to see where a dependency is used? Specifically to know which classes in your project import a class from a given dependency?
This is especially difficult when the package naming of the class and the dependency declaration's tags do not line up.
For example, given a POM which contains a dependency:
<dependency>
<groupId>org.company.project</groupId>
<artifactId>someartifact</artifactId>
<version>1.0</version>
</dependency>
find all classes that uses it (such as):
import org.company.similarprojectnamebutnotquitethesame.packagecontinued.SomeClass
TLDR: Is there an efficient way to locate all files in my project that use a given dependency?

Use a JAR search engine that matches the class name with a package, such as findjar.com or help4j.com

Related

Replace provided Maven dependency with local class

If I check my effective pom I will find the following entry:
<dependency>
<groupId>com.package.of.other.department</groupId>
<artifactId>someArtifact</artifactId>
<version>2.4.2</version>
<scope>provided</scope>
</dependency>
This comes from the parent pom that we have to use to let our software (bpmn processes) run on a company wide platform.
Now comes a hacky part. There will be a bigger change and we cannot use someArtifact anymore. Unfortunately that artifact gets called directly by all our processes (you design the process and configure the full qualified class name for an item) and can't just configure a different artifact, as that would most likely break a lot of the running processes.
The simple plan was to create a class with the same package name and with the same class name, remove every dependency to the original package and everything should work fine. During the tests I noticed that it doesn't use my new class but still the original one, most likely because it gets provided as dependency via the mandatory parent pom and for some reason prefers that over my local one.
Excluding a provided dependency from the parent pom doesn't seem to work that easily?! Any idea how I could solve my issue?
If the application is regular java, the class that will be load is the first class met in the classpath order.
If you use other runtime package dependency management, the strategy is different. As example you can adjust your import-package in OSGi to ensure the use a class contains in private-package.

How to use the SCIM API war file

I'm trying to extend some features to the scim api that are not supported.
I've added the following maven dependencies from WSO2 Nexus repository:
<dependency>
<groupId>org.wso2.carbon.identity.inbound.provisioning.scim</groupId>
<artifactId>identity-inbound-provisioning-scim</artifactId>
<version>5.1.4-SNAPSHOT</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.inbound.provisioning.scim</groupId>
<artifactId>org.wso2.carbon.identity.scim.provide</artifactId>
<version>5.1.4-SNAPSHOT</version>
<type>war</type>
<classifier>classes</classifier>
</dependency>
I've identified that I need to change the org.wso2.carbon.identity.scim.provider.resources.SCIMUserManager (and its UserStoreManager), and also add a new endpoint in the org.wso2.carbon.identity.scim.provider.resources.UserFeature.
However, these are located within org.wso2.carbon.identity.scim.provider but it seems that the war dependency hasn't any classes attached (and the maven 'classifier' tag is in vain), therefore I can't import or inherit those classes.
So, how can I extend the SCIM Api by using the org.wso2.carbon.identity.scim.provider library but managed by Maven?
I may not understand your question well. Anyway, You can change scim endpoint (wso2.war) file and put into repository/deployment/server/webapp directory. Also, you can find the class inside WEB-INF directory.

Maven dependency doesn't exist after pom.xml dependency addition in Java?

After I added this dependency to my pom.xml file:
<dependency>
<groupId>com.miglayout</groupId>
<artifactId>miglayout-swing</artifactId>
<version>5.0</version>
</dependency>
I tried to import com.miglayout.*; but I got the error:
package com.miglayout does not exist
How come nothing is wrong with other libraries I have imported using Maven in the same project, but I get issues with com.miglayout?
I believe the correct package is net.miginfocom.*
The maven groupId does not always correlate with the package name.
See MigLayout Javadocs
The classes inside the MiG layout library are under the packages:
net.miginfocom.swing for the miglayout-swing artefact
net.miginfocom.layout for the miglayout-core artefact (transitive dependency of miglayout-swing).
If you are using an IDE, you should not write the imports yourself and let the IDE handle it. This way, you will avoid mistakes relating to wrong package imports. Also, you should not use import on-demand and prefer single type import.

Find which dependency jar has required class

How can you find which dependency to import if you just know the class name.
Imagine i am getting error
hbase/mapreduce/HbaseDBMapper.java:[9,53] package org.apache.hadoop.hbase.mapreduce.replication does not exist
This is due to fact that i dont have dependency which can give me this package.
How can i find which dependency to import from maven repository or any other website
In general, http://www.findjar.com/ can be used to find the jar files and consequent maven dependencies, given a class.
In this specific case, you may want
<artifactId>hbase-server</artifactId>
<groupId>org.apache.hbase</groupId>
as the class is defined in https://github.com/apache/hbase/tree/master/hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce

Maven: Resolving Duplicate Dependencies

I'm developing an application that will be used internally at our company. In order for it to interop with our other internal systems I have to use some maven dependencies that we use internally, but this is causing some issues with using some external 3rd party dependencies that I also need.
So essentially my pom looks like this:
<dependencies>
<dependency>
internal-framework-artifact
</dependency>
<dependency>
necessary-third-party-artifact
</dependency>
</dependencies>
I've come to find that both of these dependencies have the apache's commons-collections as one of their own dependencies (among a large number of others, but we'll just keep it at one for this question's simplicity).
If I place exclusion rules on both of them for the commons-collections pom I can compile the project, but my resulting jar won't have access to either version of commons-collections and will just result in a java.lang.NoClassDefFoundError exception. Removing the exclusion rule on either of them just results in a mvn compiler error:
[WARNING] Rule 2: org.apache.maven.plugins.enforcer.BanDuplicateClasses failed with message:
Duplicate classes found:
I've been looking through various so q/a's and I can't really seem to find something that's 100% relevant to my situation. I'm really at a loss as to how to resolve this. Am I missing something really obvious?
I've never actually used the maven-shade-plugin for shading, but I think this is the exact use case it was designed for.
Create a new project that uses the maven-shade-plugin (see: http://maven.apache.org/plugins/maven-shade-plugin/) to produce an uber-jar version of internal-framework-artifact which contains that classes in internal-framework-artifact and all its dependencies. Configure the plugin so that it relocates all the classes that are also dependencies of necessary-third-party-artifact to some non-conflicting package names. This new project should produce a .jar with a different name, something like internal-framework-artifact-with-dependencies.
Now modify your original pom so that it is dependent on internal-framework-artifact-with-dependencies instead, and it should work.

Categories

Resources