How do you programmatically access resolved implementation dependencies? - java

I would like to access the metadata about a resolved implementation dependency that has its version set via a platform bom
dependencies {
implementation platform("internal.spinnaker:clouddriver-bom:${spinnakerRelease}")
annotationProcessor platform("internal.spinnaker:clouddriver-bom:${spinnakerRelease}")
testAnnotationProcessor platform("internal.spinnaker:clouddriver-bom:${spinnakerRelease}")
implementation "com.netflix.spinnaker.clouddriver:clouddriver-web"
implementation "com.netflix.spinnaker.clouddriver:clouddriver-core"
implementation "com.netflix.spinnaker.clouddriver:clouddriver-security"
}
I would like to be able to programmatically access the resolved version of clouddriver-web.
All the solutions I can find online keep referencing code like this.
configurations.compile.resolvedConfiguration.firstLevelModuleDependencies
Which you cannot do with the implementation configuration as it throws an error about not being resolvable.
The end goal is to have the resolved dependency be an entry in META-INF/build-info.properties so that it can be used like so: https://www.vojtechruzicka.com/spring-boot-version/
I can’t figure out how to get the version in a task or gradle file.
Any help would be greatly appreciated, cheers!

With Gradle 6.0, the following build.gradle:
plugins {
id 'java'
}
repositories {
jcenter()
}
dependencies {
implementation 'com.google.guava:guava:29.0-jre'
}
task log {
doLast {
println(configurations.compileClasspath.resolvedConfiguration.firstLevelModuleDependencies)
}
}
When run with:
./gradlew :log
Prints:
> Task :log
[com.google.guava:guava:29.0-jre;compile]
Any dependency in the implementation configuration will show up on the compile classpath, as shown by the following figure that I've taken from The Java Plugin.

Related

Gradle spring dependency management plugin and publishing the library

Im have my library, for help me with spring stuff, build.gradle looks like
plugins {
id 'java-library'
id 'maven-publish'
id "io.spring.dependency-management" version "1.0.11.RELEASE"
}
dependencyManagement {
imports {
mavenBom 'org.springframework.boot:spring-boot-dependencies:2.7.0'
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
}
publishing {
// Default maven publish settings
}
When publishing, Gradle generates .module files, and then if the library is used by gradle, it prioritizes .module files over .pom files
The problem is that no dependencyManagement information is written to the .module file, and so when I try to use the library in another project, I get
Could not determine the dependencies of task ':shadowJar'.
> Could not resolve all dependencies for configuration ':runtimeClasspath'.
> Could not find org.springframework.boot:spring-boot-starter-data-mongodb:.
Required by:
project : > project :database > ru.spliterash:spcore-spring-database-mongo:1.0.0-SNAPSHOT:20220714.235637-1
I can solve this problem by disabling the generation of .module files, and gradle will have to use a pom file in which the version is written, but maybe if there is some more correct solution, because it seems to me that this is not entirely correct
I have found a solution to the problem.
In new versions of gradle, this appeared as a built-in solution, and now spring dependency management plugin is not needed. In order to add a maven bom, it is enough just to write, and it will be correctly entered into the module file
dependencies {
implementation(platform('org.springframework.boot:spring-boot-dependencies:2.7.0'))
implementation(platform('org.springframework.cloud:spring-cloud-dependencies:2021.0.2'))
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
}

How to add lombak (annotationProcessor type) dependency in test suites plugin in gradle?

I have created a new test suite using jvm-test-suite plugin.
I have added few implementation type dependencies and it was working fine, no error was coming. But I also want to add lombak dependency in that test suite, I tried it with implementation keyword, after that I checked the project is getting compiled but at the runtime those annotations (Eg: SneakyThrows) of lombak are getting ignored and I was getting error.
After that I tried adding lombak dependency with annotationProcessor keyword which resulting is below given error at gradle sync. So basically it looks like annotationProcessor keyword and testAnnotationProcessor are not getting recognised and thus this error is coming.
Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'serverlessserver'.
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:93)
Caused by: groovy.lang.MissingMethodException: No signature of method: build_aiuizpn3ddvrwt4slowy7mi4q.testing() is applicable for argument types: (build_aiuizpn3ddvrwt4slowy7mi4q$_run_closure4) values: [build_aiuizpn3ddvrwt4slowy7mi4q$_run_closure4#74ada7e2]
Gradle file snippet:-
testing {
suites {
test {
useJUnitJupiter()
}
customTest(JvmTestSuite) {
dependencies {
implementation project
... // other dependencies
annotationProcessor 'org.projectlombok:lombok:1.18.22' // adding this line is resulting in error message
}
}
....
}
}
I communicated with gradle development team on their slack support channel. I got the answer to this question which solved my problem and hence I am posting it here for other people.
plugin donot provide direct annotation processor support inside testing/suites block by default as of now, the team is implementing it and probably they will support it in future releases.
You can still mention this annotation processor in outer most dependencies block of build.gradle file along with test suite name.
Example - build.gradle sample file
dependencies {
.....
// dependencies you already have in your project
// add this line. "customTest" here is the name of test suite you defined.
customTestAnnotationProcessor('org.projectlombok:lombok:1.18.22')
}
One more thing you have to make sure is that you have defined your test suites before this dependencies block in build.gradle file. otherwise, the annotationProcessor statement in dependencies do not recognise the test suite and will give error.
This is what I used to not rewrite the same dependencies twice for test and customTest tasks (I also use lombok for my test):
configurations {
customTestImplementation {
extendsFrom testImplementation
}
customTestCompileOnly {
extendsFrom testCompileOnly
}
customTestAnnotationProcessor {
extendsFrom annotationProcessor
}
}
Directly from the official lombok website.
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
testCompileOnly 'org.projectlombok:lombok:1.18.22'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'
}
Lombok annotations are applied during compilation, so it should be added in compileOnly stage, not in runtimeOnly.

IntelliJ cannot resolve symbol 'spark' when importing Spark Java

I am currently running the latest IntelliJ and I am trying to use SparkJava Webserver for a Gradle project I'm writing.
When I do compile and run with gradle everything works fine, however I am getting an error in
import static spark.Spark.*;
Where IntelliJ cannot resolve the symbol spark, even though I obviously have the correct dependency set in my build.gradle:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation 'mysql:mysql-connector-java:8.0.27'
implementation "com.sparkjava:spark-core:2.9.3"
implementation "org.slf4j:slf4j-simple:1.7.9"
}
test {
useJUnitPlatform()
}
Is there any way to circumvent this?
Many thanks in advance.
I got it:
In IntelliJ, under View -> Tool Windows -> Gradle there's an option to refresh everything Gradle related at the following button:
Using this refreshes the cache and yields the correctly imported dependency.

Adding custom maven dependency to gradle project [duplicate]

This question already has answers here:
How to add a Maven project as a Gradle dependency?
(3 answers)
Closed 2 years ago.
I have created dependency jar using maven project but now i have to add this maven dependency into my gradle project.
Depencency available in my .m2 directory
Am getting the below error from intellij .
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
> Cannot convert URL 'com.example.auth.security:common:0.0.1-SNAPSHOT.jar' to a file.
Please find my build.gradle file
plugins {
id 'org.springframework.boot' version '2.1.16.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
}
group = 'com.example.auth'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation files('com.example.auth.security:common:0.0.1-SNAPSHOT.jar') --> getting error on this line.
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
compileOnly 'org.projectlombok:lombok'
implementation('io.jsonwebtoken:jjwt:0.9.1')
}
Update 1
A problem occurred evaluating root project 'auth-center'.
> Supplied String module notation '0.0.1-SNAPSHOT' is invalid. Example notations: 'org.gradle:gradle-core:2.2', 'org.mockito:mockito-core:1.9.5:javadoc'.
You will need to add a local maven repository like this
repositories {
maven { url new File(pathToYourM2Directory).toURI().toURL() }
}
Additionally the declaration of the dependency is not correct. It should be
dependencies {
implementation group: 'com.example.auth.security', name: 'common', version '0.0.1-SNAPSHOT'
}
You can as well fix the files dependency. However using a local maven repo is more sustainable as by resolving artifacts this way it is transparent for the build process if an artifact is resolved locally or remote.
Can't comment since I don't have sufficient reputation. I believe you shouldn't be trying to add the maven dependency to the gradle project. Instead, host the maven dependency elsewhere and configure gradle to pull dependencies from there. For reference, you can take a look at this answer
How to add a Maven project as a Gradle dependency?
This is another way to import your custom java library(.jar).
What you need to do is that first, make a folder wherever you want under your project, in my case /src/lib, and put JAR file into that folder. Then, write down the below code into your Gradle file.
dependencies {
//Library Auto Implement
//Replace with your folder URI
implementation(fileTree("./src/lib"))
}
Then Gradle will implement your JAR files from that folder, and you are ready to go.

Gradle - Error Could not find method implementation() for arguments [com.android.support:appcompat-v7:26.0.0]

I am trying to open existing android project in android studio and it gradle cannot build the app without the error
Error android studio keeps on throwing
Error:(74, 1) A problem occurred evaluating project ':app'.
> Could not find method implementation() for arguments
[com.android.support:appcompat-v7:26.0.0] on object of type
org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
My Code in build.gradle Which can help to understand my issue
My dependencies
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// google & support
implementation "com.android.support:appcompat-v7:$supportVersion"
implementation "com.android.support:cardview-v7:$supportVersion"
implementation "com.android.support:recyclerview-v7:$supportVersion"
implementation "com.android.support:design:$supportVersion"
implementation "com.android.support:palette-v7:$supportVersion"
implementation "com.android.support:customtabs:$supportVersion"
implementation "com.android.support:support-v4:$supportVersion"
implementation 'com.google.android.exoplayer:exoplayer:r2.0.4'
// utils
implementation 'com.github.bumptech.glide:glide:4.0.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'
implementation 'com.koushikdutta.ion:ion:2.1.7'
implementation 'com.github.Commit451:bypasses:1.0.4'
implementation 'com.jakewharton:butterknife:8.8.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.0'
implementation 'com.drewnoakes:metadata-extractor:2.9.1'
implementation "com.orhanobut:hawk:2.0.1"
}
Please help to solve the issue
Make sure you're adding these dependencies in android/app/build.gradle, not android/build.gradle.
Replace compile with implementation.
compile was recently deprecated and replaced by implementation or api
Make sure your Gradle version is 3.*.* or higher before using "implementation".
Open the project level Gradle file under dependencies:
dependencies{
classpath 'com.android.tools.build:gradle:3.1.2'
}
Open the 'gradle-wrapper.properties' file and set the distributionUrl:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
or latest version.
Sync the project. I Hope this solves your problem.
You need to use at least Gradle 3.4 or newer to be able to use implementation. It is not recommended to keep using the deprecated compile since this can result in slower build times. For more details see the official android developer guide:
When your module configures an implementation dependency, it's letting Gradle know that the module does not want to leak the dependency to other modules at compile time. That is, the dependency is available to other modules only at runtime.
Using this dependency configuration instead of api or compile can result in significant build time improvements because it reduces the amount of projects that the build system needs to recompile. For example, if an implementation dependency changes its API, Gradle recompiles only that dependency and the modules that directly depend on it. Most app and test modules should use this configuration.
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations
Update: compile will be removed by end of 2018, so make sure that you use only implementation now:
Warning:Configuration 'compile' is obsolete and has been replaced with
'implementation'. It will be removed at the end of 2018
change apply plugin: 'java'
to apply plugin: 'java-library'
java-library-plugin
For me I put my dependencies in the wrong spot.
buildscript {
dependencies {
//Don't put dependencies here.
}
}
dependencies {
//Put them here
}
Solved this by adding my dependancies in the app folder
Go to app < Gradle Scripts < gradle.build(Module: app) and add the depandancies in that file and not the global build.gradle file
So ridiculous, but I still wanna share my experience in case of that someone falls into the situation like me.
Please check if you changed: compileSdkVersion --> implementationSdkVersion by mistake
I moved implementation to module-level build.gradle from root-level build.gradle. It solves the issue.
Your Code
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
Replace it By
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
Replace your implementation with classpath. That should work.
As mentioned here, https://stackoverflow.com/a/50941562/2186220, use gradle plugin version 3 or higher while using 'implementation'.
Also, use the google() repository in buildscript.
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
These changes should solve the issue.
As suggested in official docs you need add these :
buildscript {
repositories {
// Gradle 4.1 and higher include support for Google's Maven repo using
// the google() method. And you need to include this repo to download
// Android Gradle plugin 3.0.0 or higher.
google()
...
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0'
}
}
adding these remove my error. Also use implementation instead of compile.
In my case it was because I used Implementation instead of implementation.
For me the problem was that I wrote:
android {
compileSdk 31
…
Instead of:
android {
compileSdkVersion 31
So make sure your build.gradle is correct.
If implementation is not defined, you are writing on a wrong file. On Unity 2019+ the correct file is main template grandle and not some of the others.

Categories