Maven test-dependency removes transitive compile-dependency from uberjar - maven

We have splunk-library-javalogging as compile-dependency and added com.squareup.okhttp3/okhttp as a test-dependency. okhttp is also a compile-dependency of the splunk-appender.
Using spring-boot-maven-plugin we realized the okhttp-Dependency is no longer included, when added as a test-dependency. Removing it, it is included again.
So it seems scoping okhttp as test overrides the transitive compile-dependency (excluding it from the jar) - and this doesn't feel correct!?

This does not feel correct, but it is unfortunately the case.
Maven determines the scopes first, then creates the classpaths. And direct dependencies override transitive ones.
It would be smarter to create the compile classpath by just ignoring test scoped dependency entries, but this is not how Maven does it.
Quintessence: Only add a test dependency if the dependency is not already in mvn dependency:list.

Related

What exactly maven does when a transitive dependency is excluded?

Impact of exclusion in the context of jar packaging(simple/fat jar using shade plugin). What i mean here, how can it be verified by extracting the jar.
When a dependency is excluded, it is removed from the dependency tree at that point. You can check this with mvn dependency:tree. It might still come in through other ways (e.g. as transitive dependency of some other dependency). You can check that with mvn dependency:list.

Is there a way to detect if a new dependency has been added to a maven project since it's previous build/release?

When a transitive dependency changes, there is no direct change in the project I am working on. When I update a dependency that itself brings in new dependencies since its previous version, transitive dependencies are difficult to track and it would be good to know if there is any new library added to the project I am building or the version of an existing transitive dependency has changed.
Is there a maven plugin that can detect a dependency change like this or a maven flag?
Use mvn dependency:list -Dsort=true > file to generate all dependencies into file. After POM changes generate second file. Then diff files to see changes
If you don't do any changes also transitive dependencies will not change. This can happen only if you change POM. For example you change version of used dependency.
If a library changes dependencies, version of the library will increase. To be affected by this changes you would need to use that new version in POM.

How can I expect maven to resolve dependencies required by the child jar?

I have a parent project(has its own pom.xml) in which I import the child project as a jar with its own pom.xml.
In the parent pom.xml I have specified my child jar as a dependency - this gets resolved, but i want maven to resolve the dependencies required by my child jar.
My Use case to replicate :
When I include spring-web-mvc.jar the transitive dependencies are resolved automatically.
I have a similar requirement where I include my child.jar into a main framework project and expect the transitive dependencies to get resolved (Notw: the child.jar is not hosted it is packaged as jar and present on the local file system)
Current Structure:
Child Project:
|----/src/main/java
|----/src/main/resources
|----child-pom.xml
>This child project will be a jar as dependency in the parent project
Parent Project
|----/src/main/java
|----/src/main/resources
|----/src/main/webapp/WEB-INF/lib/child.jar
|---- parent-pom.xml
The problem:
When i create a war from parent project i want all the dependecy including transitive ones to show in WEB-INF lib.
Currently this is not happening.
First when talking of parent/ child Maven projects normaly you have your"childs" specified as modules in a common parent project which itself is beeing packed with the packaging type pom rather than make them a dependency of the parent project.
When it comes to the dependencies of your "childs" or generally "dependencies of your dependencies" those are called transitive dependencies and are pretty well explained in the official documentation found here: http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
The resolving of those transitive dependencies is one of Mavens core strenghts and guaranteed by default unless they lead to conflicts that make the build fail.
Two things to help here are having a closer look into the enforcer plugin (http://maven.apache.org/enforcer/maven-enforcer-plugin/) and the shader plugin (http://maven.apache.org/plugins/maven-shade-plugin/) .... well and the official documentation of corse (reading the whole thing takes less than a day - then supports you further for specific topics whereas we gladly further support you too if you already have pom.xml files and you are stuck somewhere.
While the enforcer-plugin covers certain conflicts regarding different versions of the same artefacts the shader plugin will just pack everything you specified to a single jar for reverseengineering (its not the normal use case but i sometimes use it that way if i am not absolutly sure what ends in my final archives).
Also worth a look at is the dependency-plugin already available in the maven distributions - mvn dependency:tree -Dverbose will give you pretty detailed information on the resolved dependencies (and probably version conflicts).

How to manage compile time dependencies in Maven

Trying to avoid the use of jargon, so that I don't get misinterpreted.
Here is the scenario, My project requires a jar in order to get compiled(let say x.jar). My project get once compiled gets converted into a WAR file, which gets deployed somewhere.
Now I want x.jar just to be there for my project to compile and it should not be packed(or part of) inside WAR file.
How can I do this in Maven ? should I used dependency scope as "provided"
You are right, as stated in the Maven FAQs, the scope to use is provided,
How do I prevent including JARs in WEB-INF/lib? I need a "compile only" scope!
The scope you should use for this is provided. This indicates to Maven that the dependency will be provided at run time by its container or the JDK, for example.
Dependencies with this scope will not be passed on transitively, nor will they be bundled in an package such as a WAR, or included in the runtime classpath.
To quickly try it out, you can use
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp
to generate a "toy webapp" project, add a dependency to your project and set it to <scope>provided</scope>.

maven test scope transitive dependencies

I am writing a plugin where I need to get all the dependency artifacts of a project including test scope and all of the transitive dependencies as well. project.artifacts seems to get me all the dependencies if I run the plugin in the install phase (I cannot run it before that), except it does not get me test scope dependencies and any transitive dependencies of these test scope dependencies. How do I get the transitive dependencies of everything?
I have also tried project.dependencyArtifacts which doesn't seem to help. It gets the test scope direct dependency but not the transitive ones.
Working with maven 2.0.9
Posting for reference:
I had to set #requiresDependencyResolution test which then included all the transitive dependencies. It was initially set to runtime. This fixed my issue.

Resources