Filter output of Gradle's dependencies - gradle

Maven
In Maven, when performing a mvn dependency:tree you can specify a filter argument like so:
-Dincludes=<groupId>:artifactId>
(See filtering a dependency tree)
Gradle
There doesn't seem to be an equivalent option for the dependencies command in Gradle.
Is there way of doing this?

As far as I know the only filtering option available is for a configuration:
gradle <your-module>:dep --configuration compile

You can use depdendencyInsight task to achieve something close to what you are looking for. However, it may not be the same as what maven's dependency:tree can produce.
Refer : https://docs.gradle.org/current/userguide/inspecting_dependencies.html
Assuming your build.gradle has multiple configurations -
configuratuion{
compile
scm
}
In order to filter library say commons-codec, for each configuration, you could run the following -
gradle -q dependencyInsight --dependency commons-codec --configuration scm
gradle -q dependencyInsight --dependency commons-codec --configuration compile
As of Gradle 4.9, this is what I have observed -
the parameter --dependency is mandatory
only 1 value can be provided for --dependency
the parameter --configuration is optional
the default value of --configuration is compileClasspath

Related

How can I automatically run google-java-format as part of my Gradle build?

Google-java-format-gradle-plugin integrates with Gradle, but how can I run it automatically as part of the normal build?
Sherter gradle plugin is automatically integrated to "gradle build". When you run it, it will run "gradle verifyGoogleJavaFormat". In case of violations, the build will fail.
We are using it on jenkins and it works. You will only need dependency to build.gradle file:
compile group: 'com.github.sherter.google-java-format', name: 'com.github.sherter.google-java-format.gradle.plugin', version: '0.8', ext: 'pom'
And also add plugin:
id 'com.github.sherter.google-java-format' version '0.8'
Then just run "gradle build" and you can see in the console, that verifyGoogleJavaFormat was executed.
DependsOn plugin's task that you need from a task that relates to a normal build, for example, you can use preBuild task:
tasks.findByName("preBuild").dependsOn(YOUR_TASK_FROM_PLUGIN)
or shorter
preBuild.dependsOn(YOUR_TASK_FROM_PLUGIN)
Also you can choose another task instead of preBuild.

Gradle: Dependency insight report cannot be generated because the input configuration was not specified

I'm trying to resolve dependency conflict in my gradle applicaiton. My top-level build.gradle is:
archivesBaseName = 'message-sender-rest'
apply plugin: 'paas-publish'
dependencies {
compile(project(':message-sender-api'))
compile(libraries.springBoot)
compile(libraries.loggingRuntime)
compile(libraries.integration)
compile(libraries.serviceFrameworkServer)
compile(libraries.serviceFrameworkApp)
compile(libraries.serviceFrameworkSpringIntegration)
testCompile(libraries.testing)
testCompile(libraries.springTest)
testCompile(libraries.activeMqBroker)
}
When I try to run gradle dependencyInsight --configuration compile, I get the following error:
* What went wrong:
Configuration with name 'compile' not found.
There is a whole bunch of lower level gradle files, but I guess that should just work using the top-level one, isn't it?
When i just try gradle dependencies, it returns pretty much nothing:
gradle dependencies
dockerBuild tag: dev.docker.orbitbenefits.capita/orbit/message-sender-rest:bOD-9656-ConfigServer-n0
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
No configurations
BUILD SUCCESSFUL
Any idea what I may be missing?
I was not specifying the module I wanted to inspect:
The project had a sub-project (module message-sender-server). Specifying the module name actually worked:
gradlew message-sender-server:dependencyInsight --dependency webmvc

Gradle alternative to maven <packaging>pom

In Gradle multi-module project, how to tell Gradle child module not to build any libs/jars and other dependencies ? Is there some alternatives to maven <packaging>pom in Gradle?
So as a result i can specify jar.enabled = false and module will not produce any artifacts, however you can still perform custom tasks like compile/copy in this module.

How to get insight of a dependency for the "implementation" configuration?

Based on documentation (4.7.6 - Getting the insight into a particular dependency) we can get the insights for a particular configuration specifying the configuration itself.
In the example they are using as configuration compile, which is deprecated.
I tried to reproduce the same command replacing, in build.gradle, the compile configuration with the implementation configuration (as I got we are not supposed to use compile anymore).
But when I run:
gradle dependencyInsight --dependency groovy --configuration implementation
Gradle is returning:
Execution failed for task ':dependencyInsight'.
Resolving configuration 'implementation' directly is not allowed
My build.gradle file is the following:
apply plugin: 'java-library'
repositories {
jcenter()
}
dependencies{
implementation 'org.codehaus.groovy:groovy-all:2.4.10'
}
Does it mean I cannot get the insight of a dependency if I'm using implementation or is there another way to get it?
I had a similar problem, asked around and got this answer:
The configuration is compileClasspath. If you have variants, there is a configuration per-variant (e.g. for the release variant, your configuration would be releaseCompileClasspath).
Examples
No variants: gradle dependencyInsight --dependency groovy --configuration compileClasspath;
Release variant: gradle dependencyInsight --dependency groovy --configuration releaseCompileClasspath
Note
There are a few ways to figure out the available configurations.
If you add configurations.each { println it.name } to your top-level gradle file, the next time you run a task, you'll also get a list of all of your configurations.
Run the dependencies task on your top-level module - this will output all dependencies for all configurations. It can be a lot of text, so you could pipe it into a text file for easier searching (gradle dependencies > dependencies.txt)
To get the list of available configuration that can be used with dependencyInsight, the easy way is the following :
Open gradle view in android studio View > Tool Windows > Gradle
Select and run task 'Your App' > Tasks > Android > androidDependencies
Then you have a list of all dependencies for all available configuration, just pick one and run :
gradle :mymodule:dependencyInsight --dependency okhttp --configuration flavourDebugCompileClasspath
Hope it helps

How do I use the latest version of jcommander with a Gradle project

I have a reasonably uncomplicated build that used to work fine in Maven. When I ported it to Gradle, I can see that jcommander-1.12.jar is being pulled in from a transitive dependency (I think through TestNG). This is overriding my explicit dependency on jcommander 1.35.
JCommander 1.12 does not support some annotations and values that I rely on in 1.35.
I can remove all dependencies on jcommander with something like
configurations {
all*.exclude group: 'com.beust', module: 'jcommander'
}
(See http://mrhaki.blogspot.com.au/2012/10/gradle-goodness-exclude-transitive.html)
So how do I prioritise my explicit dependency over the inherited one?
UPDATE
This is the output of gradle dependencyInsight --dependency jcommander
$ gradle.bat dependencyInsight --dependency jcommander
:dependencyInsight
com.beust:jcommander:1.35
\--- compile
Interestingly it doesn't even list jcommander 1.12 at all...
Gradle is not honoring resolutionStrategy.force seems to be a similar problem.
This is somewhat unexpected behavior. By default, when Gradle detects a conflict, it goes with the newer dependency. Trying running gradle dependencyInsight --dependency jcommander to get a little more information on why version 1.12 is being chosen.
Regardless, you can always force a particular version of a dependency.
configurations.all {
resolutionStrategy {
force 'com.beust:jcommander:1.35'
}
}
I don't know why this is a problem, but I found the root cause.
I was actually porting a command line app to a Gradle plugin, which meant that I added the following lines as dependencies:
compile gradleApi()
compile localGroovy()
If I replace compile gradleApi() with compile "org.gradle:gradle-core:2.0", the correct version of JCommander is resolved.

Resources