Bundle Dependencies together - java

I have created a library and exported it as a jar. The library has a couple of dependencies as AspectJ and springfox. Every time an application adds this as a dependency in pom.xml, it has to add dependencies as SpringFox and Aspectj as well into its pom.xml.
Is there an option in Maven through which we can avoid this step of adding these dependencies?

No need for any option. If the pom.xml of your library references AspectJ and springfox, then these are inherited by all projects that use your library.

Related

How version of lib defined in classpath?

i have hirarhy of modules.
I set up version of hibernate-validator as 6.1.5.Final
But when i build project version of library another
maven dependencies: tree ouput
org.hibernate:hibernate-validator:jar:5.3.4.Final:compile
I can not understand how it works.
I put all dependency tree here https://paste2.org/CwB2H4W2
Problems with dependencies should always bring you to the "Dependency Hierarchy" Tab of your POM.xml. There you will see your projects dependencies, and dependencies of that dependencies.
If I had to guess I'd say there you will find a module providing the dependency you think you don't use.
Further information to maybe change this:
First declared dependencies get used first. So if you define your dependency before the Module that brings the other dependency in, Maven should select yours.

maven is not downloading any dependency that I have add

I have install maven and JDK and tried to create maven project and add some dependencies, as you can see in the screenshot but the dependency is not downloading so I can not import. can you help.
That's because you are using dependencyManagement, where you only specify dependency meta-information, but not the actual dependencies.
More details about managing dependencies via Maven you can find here:
maven dependency mechanism
maven dependency management
Change the version of guice to 4.1.0
What it seems like there is no 4.1
https://mvnrepository.com/artifact/com.google.inject/guice

How to exclude a package from a dependency jar(using maven) present in a web project?

I have below dependency in my pom.xml.
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-server-client</artifactId>
<version>4.7.0-HBase-1.1</version>
</dependency>
The above is for phoenix query server interacaction
This dependency has org.apache.calcite:calcite-avatica:1.6.0 transitive dependency. The calcite-avatica dependency has jackson-databind(2.1.1) one of its transitive dependency. So jackson-databind(version: 2.1.1) dependency should be included in classpath of my project. But instead of that, all of the classes in the jackson-databind(version: 2.1.1) are included in calcite-avatica jar. I have declared jackson-databind( version: 2.7.2) as direct dependency in my pom.xml.
I can exclude the jackson-databind(2.1.1) through tag. . <
org.apache.phoenix
phoenix-server-client
4.7.0-HBase-1.1
com.fasterxml./*
jackson-databind
But it is not getting excluded since it has been bundled in calcite-avatica:1.6.0 jar. Is there any solution for that. I have tried maven shaded plugin. but in vain.
There are different approaches, none of them is perfect:
If you don't need calcite-avatica jar, exclude it (with the Maven exclude mechanism.
If you need some classes from calcite-avatica jar, but not the jackson-databind classes, alter the order in which you declare the dependencies. Because Java can load each class only once, either the classes of calcite-avatica jar hide those of jackson-databind or vice versa.
Create a "hacked" calcite-avatica jar which does not contain the doubled classes.
If you really, really need jackson-databind in two different versions, you need try to construct this with the shade plugin, but it is not easy.

Create common class, has dependency, how do I formally share this dependency?

I've extended an abstract class and implemented a method that I will use with Mule over and over. I want to add it to a library that I will repo on Maven central. It depends on a JAR that's provided in the Mule connector devkit (sdk for Mule connectors). How do I formally publish or tell others publicly that my common library will not work without the earlier dependency too? The dependencies org.json and fasterXML...Jackson..etc.
My code module is an HttpProcessMessage and the over-used method returns a String of formatted JSON. The message POJO gets loaded and then ultimately my method is like a toString() method but more sophisticated.
I would like for this class to be part of a common library that would become part of the community. I wish that my code here would be the foundation and have no dependencies. Now, I'm looking for an answer to address the dependencies and inform the public.
That's one of the main benefits of Maven and other dependency management tools, when your dependency is added to their Maven project, Maven will automatically fetch transitive dependencies. So there is no need to let people know what dependencies you rely on. It will automatically be handled and they can use Maven command if they wish or inspect the maven artefact to determine what transitive dependencies you rely on.
You will need to add the com.faster.xml dependencies and org.json dependencies to your Maven pom.xml and all this information is packaged alongside your Maven artefact and will be stored in a Maven repo.
More info on Maven dependencies here: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
If they are manually installing the jar outside of Maven/Gradle etc. then theres not much you can do except provide them a detailed README on installation instructions.

OSGi (Karaf) Resolution vs Maven dependencies

In my example below there seems to be a discrepancy / duplication in the required steps in OSGi resolution and Maven dependency support.
I have a jar which is dependent on a external 3rd party jar, in this case time4j.
<dependency>
<groupId>net.time4j</groupId>
<artifactId>time4j-core</artifactId>
<version>4.16</version>
</dependency>
I can then run my simple jar locally by importing the packages and running etc.
When importing into OSGi I have to ensure that this jar is imported first, often using the PAX Wrap Url.
This is ok for 1 jar dependency but what about when there are multiple dependencies. I could use a features.xml file to collect these jars but why? I've already specified them in my pom.xml.
It seems there should be a way for OSGi / Karaf to read the pom.xml dependencies and import these into the container using the PAX Wrap url when needed.
Have I missed something here?
Sorry but your expectations are not in sync with reality.
First of all Maven dependencies are build-time dependencies. That's why you declare dependencies you know to be available in the runtime as provided
<scope>provided</scope>
Neither OSGi nor Karaf can do anything about your build time dependencies.
BUT with OSGi you can make sure your build dependencies are also available in your runtime and don't interfere with other libraries that might be available.
That's why you need to declare your imports and exports etc.
Karaf does help you with some of the dependencies for example with feature files.
If you have a feature definition maven project, all of your compile scope dependencies can be included in one feature file.
BUT, the OSGi resolver only looks at the currently available bundles and nothing more, no connection what so ever to maven, if you want to have some sort of automagic resolving of external dependencies you need to make sure that you have
a) an OBR resolver enabled (this depends on the karaf version you are using, with 4.x it's already included) and
b) an OBR repository at hand, Karaf Cave would be the project to look for in that case, cause it can reside like a proxy on top of a maven repository.

Categories