Choose which dependency to use in class - java

I want to use a class and in my pom there are two dependencies that support it: dependency1 and dependency2.
Using the class with dependency1 crashed my program, so I deleted it completely from pom and left dependency2 as it was and the code was working.
How do I tell maven to build my class with dependency2 and not dependency1, without deleting dependency1 (in case dependency1 contains something that I want to use in my code later on)?

You cannot sensibly use two libraries that contain classes with the same qualified class names.
So
either restrict yourself to one of them.
or use the Maven shade plugin to relocate the packages of one of the dependencies.

when you are importing the dependency in your respective class just check for the entire address of the dependency (the whole package structure) and make sure that you are using the dependency from the dependency2.
Also, if you have removed dependency1 from the pom.xml, maven will not put the dependency1 in the target folder which will be generated while building the project.

It's very unlikely to have the same class with same package name on two different dependencies.Because artifactIds are unique for each dependency even it belongs to the same groupdId. So if you solved your issue by using the dependency2's class then that's the class you need. And as you asked if you need dependency1 for any other task keeping the dependency1 on your pom.xml won't be a problem.Only thing that you need to take care is importing the class that you need exactly from dependency2.So please check your import statements in the class and see if it's importing the class from dependency2.

Related

Maven: Identify the correct dependency from given code

What is the best way to find the right dependency for a used class that are part of the maven-online-repository?
As far I see it is this approach:
lookup the import (e.g. org.whatever.X;) from your code at the maven-repository online (search.maven.org).
Pick one of the result list and include it in the dependency section of the POM.
Hope the chosen version and artifact of the dependency matches your requirements (compiling, runtime). If not try another artifact or version.
I'd like to share my way of doing it. What do you mean by "finding the ... for a used class that are part of the ..."? Do you mean that the dependancy is already used in somewhere else, or that you only know the package name that you may need?
I would first check which version I need for the current project.
If I'm working on a team project and someone has used the dependency in somewhere else, I would check their pom (to ensure we are using the same dependency).
Then I would look up the dependency in Maven repo and include it in my pom.
Hope this helps.
Essentially, yes this is what you have to do to obtain libraries/modules for your project.
Something that's helped me out though with this specific problem: versioning. You can set the versions you need for each of your dependencies with <properties> -> <gson.version>2.8.1</gson.version> (for example). That way, you can guarantee that your build matches with the reqs of the class or type of code you're trying to implement.
Maven doc ref: https://maven.apache.org/pom.html#Properties

Maven class creation missing some class

I got some weird issue regarding class generation in Maven.
Somehow some of my class is not generated by Maven.
I got an abstract class "A" -> Generated
I got another abstract class "B" extends "A" -> Not generated
I got another class extends "B" -> Not generated
Is it possible because there is no usage then maven not creating the class?
How to make maven create the class in build?
Anyone know the reason of this? I already tried using mvn package and mvn install.
Note : There is no exclusion for the class in my pom
Class files are generated from all Java files in src/main/java. If some of the Java Files are not compiled,
the Java files are not in src/main/java.
or the source directory was changed.
or there is some weird configuration in the pom.
Okay found the issue... caused by my stupid mistake of not putting the packages name correctly in includes section of my pom.
I keep checking excludes section instead of includes section. But this is good info for me since I just know if I have includes section and I didn't put it in there then mvn won't build it, but mvn will build it if I used the class somewhere.
Thank you for the help that make me keep checking the pom.

Maven same Classname in different dependencies

i have a maven project that has two dependencies: DependencyA and DependencyB. In each of the dependencies there is a class, lets call it FooClass.
Now there is a problem, because one class of my project needs the FooClass of DependencyA and another class needs the FooClass of DependencyB. But both classes load the FooClass of DependencyA.
Can i solve this problem with maven? Or do i have to think about the design of my project structure?
You should avoid getting two classes with same name in same package. This would lead to the idea that you have two modules of different versions of one code...
It would not be an issue if you had for instance com.yourcompany.yourproyectA.FooClass and com.yourcompany.yourproyectB.FooClass.
This scenario happens often and I have never had an issue with maven.
Regards
If two libraries need two different versions of the same library, I recommend to use a module system such as OSGi.

The hierarchy of the type Classe is inconsistent

I have a class that implements an abstract class. The abstract class is in another package of my project. Added the package where the class is abstract by Configure build path/Project. In class that implements is accusing the following error: The hierarchy of the type class is inconsistent
Have I to add this package somewhere else?
Thank you!
These errors happened because some interface/class in the hierarchy cannot be resolved.
For example: the error is in your class - class X, X inherits Y, and in turn, Y inherits Z. However, the compiler cannot resolve z (in above error), because z is belong to a library that is not included.
Therefore, you have to add package containing z to the classpath/ or project's Java Build Path (if you are using eclipse).
hope it may help.
Go to the Project Explorer.
Right Click on your Project
Build Path
Configure Build Path
Remove JRE System Library
Click on Add Library
Add JRE System Library
Click on Next and then Finish
The errors will be resolved.
This means you have made an incompatible change in a super class but haven't recompiled it.
I suggest you use a build system like Maven or Ant and/or use an IDE to build all your code.
I was facing this issue in one of My RCP application.
Cause: I was not added core plugin org.eclipse.core.runtime in dependancy section of Manifest file.
When I have added this dependency jar issue has been resolved.
Thanks,
Sid
That means class which implemented by you has reference to the interface or class which again references some other classes or interfaces in other libraries and those are not available.
springframework.aop jar is depends on aopalliance.jar add it to your class path it will resolve ur problm hope it works cos i was facing same prob I added dependent jar so it resolved
right click your project ,Properties- java build path- source- include all the cs file source(it may inherit the files) and sync.
this worked for me and correctly.
I also had this problem when tried to use some class from a plugin project in another one.
I had something like myClass extends pp1Class - here i had the error, pp1Class plugin was added as a dependency. pp1Class extends pp2Class - which was a dependency in pp1 (plugin proj1) but not in my plugin. What i did was go to pp1 and where you have defined the dependency to pp2, click properties and check the "Reexport this dependency" (this is in the MANIFEST.MF).
This should solve the problem, it solved mine.
Another reason for this error is, one of your base classes implements an interface which is in an external library, and your .classpath file is kept on a source control system (therefore readonly).
For instance, your ClassB extends ClassA and ClassA implements InterfaceA which is in LibraryA.jar. ClassA is in ProjectA, ClassB is in ProjectB. ProjectA .classpath file is readonly.
Here you have to export the LibraryA.jar from your ProjectA. But I guess due to an Eclipse bug, when a new team mate connects these projects (or occasionally when you prepare another workspace), he gets this type hierarchy error. Only way to solve this problem is to check-out .classpath file in ProjectA, remove and re-add a library (does not have to be LibraryA.jar). This operation somehow resolves the error.
In the Eclipse, OSGi environment, the required package can be added to the MANIFEST.MF-> Dependencies tab -> Imported packages. This will solve the issue. Or the plugin which contains the class can be added to the Required Plugin-ins
I also face this issue in my maven project using with Eclipse oxygen.1a IDE,
The hierarchy of the type MyClassName is inconsistent
Error showing on class name level,
thereafter I took complete svn update from repository and later Maven -> Update Project and later Project Refresh.
The error was gone...
As per my understanding, this were happening due to unmanaged project version.
I actually added all the related jars and interfaces in build path but still I was facing the error so later someone suggested me to add the j2ee.jar in build path and my error just went away.
The hierarchy of the type A is inconsistent
The above error is mainly because of Some jars missing in the classpath
eg: I was trying to implement an interface "MethodBeforeAdvice"
Here MethodBeforeAdvice implements BeforeAdvice and these two interfaces were present in one jar file called "Spring-aop-4.2.5.Release.jar"
But "BeforeAdvice" interface implements an interface "Advice" which was present in some other jar "aop-alliance-1.0.0.jar" which was not present in my class-path

maven unable to differentiate between two similar classes in different jars

i am using Maven 3 and trying to build a jar.Now there is a class which is present in two different dependent jars. Though the methods in the interface is different but maven is trying to get the method defined in the class in different jar . So how to ensure that the class is picked from another jar?
E.g the required class is xyz.class and is present in two differnet jars as:
jar1: com/mycompany/xyz.class
and jar2: com/mycompany/xyz.class
The xyz.class in jar1 is what maven is picking up i suppose but i want the one in jar2
Thanks
This is more of a Java compilation problem than Maven. Check out the answers for:
how to load same class from different jars
You'll need to define your custom class loader for this kind of situation.

Categories

Resources