I have a Maven artifact with a couple of dependencies. I want to publish the module built into a jar as normal, but I also want to publish a jar with its dependencies inside it using the maven shade plugin. This uber jar has a different classifier.
This all works fine, except if someone pulls in the uber jar using its classifier, they still get all the dependencies of the original, which they don't need.
How can I exclude certain dependencies based on the classifier? I have tried using profiles but I can't work out how to activate a profile based on the classifier of the artifact.
I know I could have the uber jar as a whole new artifact doing the exclusion itself, but that's a bit messy, I was hoping there was a better solution?
Try optional dependency. You have to declare all of your dependencies in the uber jar artifact's optional value to true.
Also, instead of using classifier, you might want to create another artifact. You can adapt this from mockito. See mockito-core vs mockito-all.
Related
I am trying to convert my existing web app project into maven. I have a bunch of jars in my project manually downloaded and kept in a directory. While converting to maven, I need the name of group id and artifact id of these jars so that I can mention it in pom.xml.
Is there any other way than manually searching for each jar name in mvnrepository.com?
I think you're slightly out of luck. There might be a chance that some of the jars contain the information you're looking for under
META-INF/maven/<groupId>/<artifactId>/pom.properties
But other than that, #JFabianMeier is right, it's manual labor. And please be aware that Maven uses transitive dependencies that may affect your classpath differently than when you specify the jars from a directory.
You might have to add exclusions to your POM's transitive dependencies to get the classpath as you want it.
I am currently working in a Java project, and we use maven to build the final jar. There are two ways to build the jars as we all know, i.e. one single jar with-dependencies, and a jar without dependencies. In the latter case, we need to add dependent jars to the classpath as well.
In principle both can work, personally I prefer one jar with dependencies, but in the project team members decided to use separate jar without dependencies. So hereby I would like to know which choice is better?
This question has no answer, since it depends on what you need to do.
If you're writing an utility package or component, that could be a dependency of another project, then there's no point in having all the dependencies inside it - it's better to leave dependency resolution to a dependency manager, like Maven.
If you, instead, are writing a full application packaged as a jar, I mean something with a MainClass that can be executed with java -jar myjar, then having the dependencies together will make distribution easier.
Consider that, for instance, if you're writing a webapp, that'll be packaged as a WAR, it normally comes with dependencies.
We are relatively new to Maven and now face a problem.
We have a Maven project (projectA) whose JAR is the dependency of several other projects. Some of the other projects are some custom web container while others are not, so some of projectA's dependency jars are provided in the case of the custom web container, but should be runtime scope in the case of other projects. We currently use exclusion list to filter out the provided jars in the case of the custom web container.
We are wondering if it would be better to use maven profiles. We know how to create the profiles with different dependencies (actually same dependencies different scope), and in both profiles, the built projectA jar is identical bit-wise. But what we don't know is, when we deploy/release the projectA jar artifact to a maven repository, what should the pom.xml look like? For these web container projects, the pom.xml should not include the provided jars, but for other projects, the pom.xml should include these jars.
We can use a different name for the jar in each profile and deploy each with a different pom.xml, but since these jars are identical bit-wise, it doesn't seem like a perfect solution. So we thought there's gotta be a better solution to this problem, only that we don't know since we are relatively new to Maven. Thanks.
The POM is the POM. You seem to be talking about managing transitive dependencies in other projects that reference "A". Your options in Maven are fairly limited:
You can use exclusions to remove transitive dependencies that you don't want.
You can declare dependencies in "A" as "provided", but this is only really correct if that jar actually is provided in A's target environment. It's primarily intended for Java EE api dependencies, like servlet-api, which are provided by containers and prohibited from being included in WAR files.
You can declare dependencies as optional, which is what people usually mean when they say "provided", and manually include those dependencies in the places where they're needed.
I'd personally choose the "optional" route because it's the job of each project to pull in the dependencies it needs, and if something is optional when using "A", it just means things that use "A" have to explicitly choose whether they'll use that optional part of it. This tends to be the best fit when building an artifact that has multiple, differing use cases.
For additional help in this area, you can also use the maven enforcer plugin to ban certain dependencies from builds so that you don't accidentally get jars that you don't want.
I am writing pom.xml for our project. I need to copy two different versions of same jar. But I searched maven docs and found that Maven has no support for this. Is there any other way to do that?
Note: Both jars have same groupid and artifact id.Only their versions are different.
Thanks in advance!
As you already know, Maven was designed to make sure that you will never have two JARs with the same coordinate (group + artifact id) but different versions on the classpath.
There is no way to achieve what you want without modifying the POMs of the JARs
So you need a workaround. Here are a couple of solutions:
Give the JARs different classifiers. Typical classifiers are "tests" and "sources" but they can be anything.
Move the version number to the artifact id and give the two JARs a new version.
For all approaches, you will need to download the JARs (and probably their POMs as well) and install them again using mvn file:install (after changing the POMs) or deploy them with mvn deploy:file if you run your own Maven proxy.
I have two maven projects, the second project extends some classes of first project. I want to create the jar file with all dependencies of first project and include it to another project as dependency. I am searching this since long time, is it possible to do it?
I am new to maven, any help on this would be highly appreciated.
Thanks
If you just want to add the dependencies to another project you add the second project dependency to your new project and the first one will be inherited and automatically included. This is what is called a transitive dependency. Read more about it in the free book Maven: The Complete Reference.
If I understand you right, you want to create a uber-jar containing all dependencies, right ?
Please refer to this question How can I create an executable JAR with dependencies using Maven?
In the second project's POM file, specify the first projects maven co-ordinates (groupId,artifactId,version,packaging) under the 'dependency' section. It will transitively acquire all the dependent artifacts.
Although it is possible in Maven to generate a standalone jar with all its dependencies. For that purpose, you can use the maven-shade-plugin. ( Reference )
There are two ways you are create a fat jar. You include the jar itself in the jar of dependent. You will not have much control in this case and the maven assembly plugin would do the work. Alternatively, you can unzip the jar and zip everything together to create the new jar. You have to decide which one best suits you. If you have multiple versions of same class, then include the whole jar in the new jar would help, but if the versions are coherent, it's best to create a jar by unzipping and zipping everything. For the second procedure, I recommend using the maven shade plugin to create uber jar.