Maven dependency scope vs Transitive dependency - maven

I was learning maven scope and I encountered a doubt.
If scope of all the dependencies in a project , say A , is compile , then they will be present in its jar too. So, it is said, for any other project , say B, that depends on this project A , will get transitive dependencies too of A. But they are already present in the jar of project A ? Why download them again ?

They are not "present in the jar". Transitive dependencies of a jar are not bundled into the jar, unless you explicitly build a fat jar, e.g. with the assembly plugin or shade plugin.
Fat jars, though, are not meant to be dependencies of other artifacts, they are only meant to be run standalone.
For ears and wars, the situation is different (standard is to bundle everything), but wars and ears do not serve as libraries that you depend on.

Related

Maven build include obfuscated jar as dependency

I have a maven project that should be obfuscated by proguard plugin.
This works fine when my dependency jar is not obfuscated.
But the result is that the dependency jar (also owned by me) is included in my obfuscated app not obfuscated. The opposite when i include the already obfuscated jar in my project i get compile errors for not finding packages and classes.
How can i obfuscate both my app and my dependency jar?

Can Gradle read transitive dependencies from pom.xml contained in local JAR files?

Unlike external dependencies (from Maven, Ivy, etc.) local JAR files usually do not provide a list of transitive dependencies for Gradle. Unless they theoretically do in form of files pom.xml and pom.properties in directory META-INF/maven/<groupId>/<artifactId>. As far as I understand these are the same files Maven uses to provide transitive dependencies for an artifact.
So I wonder if Gradle is somehow able to read these transitive dependencies from a local JAR file as if the local JAR was an external dependency. Only adding the local JAR as dependency seems to ignore the embedded pom.xml.
Use case: I am writing an Plugin API JAR for an internal product which should be used by our developers to develop plugins. The API JAR has some external dependencies (Hibernate Annotations in domain classes, dom4j, stuff like that) and it would be great if the developer wouldn't have to define these dependencies by himself (they could change with newer API version). I also don't want to create a fat JAR containing all dependencies because a) the size! and b) it would not contain the sources of the external dependencies.

How to include jar dependency classes in gradle?

We are building one jar and that jar depending on many jars. Currently we are adding dependency jars explicitly like below.
compile files('libraries/amqp-client-3.1.3.jar')
compile files('libraries/antisamy-1.5.1.jar')
compile files('libraries/antlr-2.7.7.jar')
compile files('libraries/aopalliance-1.0.jar')
So, instead of adding all the dependency jars explicitly, we need to take required classes from dependency jars and those should be included as part of jar preparation. Can any one tell me how to achieve? thanks.
Depending on your build system, you want the shade plugin (maven): http://maven.apache.org/plugins/maven-shade-plugin/ or the shadow plugin (gradle): https://github.com/johnrengelman/shadow
If you are using ant, I would try to leverage the maven plugin: http://maven.apache.org/ant-tasks/index.html
However, see the warnings in my answer here: Gradle shadow jar with conflicting dependencies

Building all dependencies in Maven

I have a Ear, war and 2 jar projects in my eclipse.
I want to automatically build 2 jars, war and ear project, when i run the pom inside ear project.
I remember doing this in maven in the past. But i forgot since i lost touch working with Maven for few years now.
Someone please remind me of that..
I used dependency compile, but it is not building jar, when i build the ear directly.
Should i first run pom in jar? does it not build that jar automatically when i build ear?
Create a multi module build that will build it all for you in the reactor. Read more about it e.g. http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-pom-best-practice.html#pom-relationships-sect-multi-vs-inherit
I guess you should define tha .jar and .war projects as dependencies of your .ear project. It is also advisable to have a parent pom, where all the projects are defined as modules, including the .ear project.
In this book you can find a well explained step-by-step setup of a maven multimodule project (with downloadable code).
There is also a great working example of an enterprise multimodule project in the JBoss quickstart examples.

Are dependencies supposed to be packaged with a maven jar artifact?

Sonatype's Maven:The Complete Reference says that a compile scoped dependency is on all classpaths and is packaged with the artifact.
Compile is the default scope; all dependencies are compile-scoped if a
scope is not supplied. compile dependencies are available in all
classpaths, and they are packaged.
I can't see that they are packaged though . . . doesn't this mean that they should be contained in the jar file? If not, what does it mean?
You are correct. Compiled scope dependencies does not get packaged with the output jar. (with JAR plugin). I think the 'package' refers to the end product (binary executable).
I came across this stackOverflow thread (How can I create an executable jar with dependencies using Maven?). Here they are packaging all the dependencies to build a executable out of Main class. In that case you need all the compile time dependencies in your packaged executable. (since JAVA lazy loads it is not a must, but preferable to have all the compile time dependencies)

Resources