How to get spring-boot dependency version in gradle? - java

I have a standard spring-boot project with org.springframework.boot:spring-boot-gradle-plugin. i know i can override dependency version with e.g.
ext['slf4j.version'] = '1.7.5'
but how can i get the version currently imported by spring-boot plugin so i can use it later in the script? for example:
currentSlf4jVersion = xxx('slf4j.version')

The dependency management plugin that Spring Boot's plugin applies for you provides programmatic access to the properties in imported boms.
You can get the value of the slf4j.version property like this:
dependencyManagement.importedProperties['slf4j.version']

You can find your gradle project dependencies using command
gradle dependencies
For All:
gradle listAllDependencies
For sub-project:
gradle :<subproject>:dependencies
This will output dependencies

Related

Spring Boot project converting from gradle to maven

I'm new to Spring Boot and I'm having some issues combining several sample projects because their dependencies are managed by Maven and Gradle respectively. My goal now is to convert all the Gradle projects (build.gradle) tp Maven (pom.xml) but can't seem to find a suitable tool.
Is there any guide on the steps to accurately convert a build.gradle file to pom.xml or it's simply not possible?
I've searched and tried the maven-publish plugin, and thought it would give a ready-to-use pom.xml but the pom-default.xml generated contained some syntax errors and can't be used for setting up a maven project.

Gradle dependency not working on same version of build

At our company, we use Artifactory to manage artifacts and dependencies of Gradle.
We have library that was build with Gradle 6.0.1, in addition, have a micro-service that was built with Gradle 6.0.1 that is using this library as a dependency.
I verified that this library exists in the declared repo.
When we try to build the project we get an error that this library doesn't exist in the declared repositories and that we should declare the correct one.
The weird part is that if we downgrade the micro-service to Gradle version 5.6.2 the library does get download and working.
We also tested it with other older micro-services that we have based on a template project that is built with Gradle version 4.10.3 and It's also working in them.
What could be the issue?
The library I was referring to in my question didn't have the POM file published with it.
So either I will need to publish it again with the POM being generated (since the library itself was built with Gradle and not Maven - there is a way to generate POM with Gradle)
or:
I will add the following code to build.gradle file so Gradle will download the artifact even though it doesn't have POM file.
repositories {
maven {
url uri('lib')
metadataSources {
artifact()
}
}
}

JUnit5 (aggregator) provides old dependencies [duplicate]

I am using Gradle 5's BOM (Bill of Materials) feature. This is how I describe it for my JUnit 5 dependencies:
testImplementation(enforcedPlatform("org.junit:junit-bom:5.4.0")) // JUnit 5 BOM
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation("org.junit.jupiter:junit-jupiter-params")
My assumption is that providing the BOM will resolve the versions of the dependencies to 5.4.0. However, they get resolved to 5.1.1. I am not sure why. (I also request enforcedPlatform() to lock the specified version)
Inspecting JUnit 5's BOM we see that all org.junit.jupiter dependencies are listed with version 5.4.0 (resolving to 5.1.1 in the project) and all org.junit.platform dependencies are listed with version 1.4.0 which resolve correctly in the project.
I am not sure what I am missing and was hoping to get some help here. Thanks!
EDIT:
I used Sormuras response and moved all BOMs at the top of the dependencies {} block but was still not getting version 5.4.0. Then I suspected it might be coming from the Gradle Spring Dependency Management plugin that I use, so when I commented it out, I got version JUnit 5.4.0. How do I disable JUnit coming from the Gradle Spring Dependency Management plugin?
FINALLY:
I decided to use the Spring Boot Dependencies BOM directly and remove the Gradle plugin:
implementation(platform("org.springframework.boot:spring-boot-dependencies:2.0.5.RELEASE"))
I imagine the plugin was created for those version of Gradle before Gradle 5 where you couldn't use a BOM file. Now with the BOM support I can directly include it. This way my version of JUnit is as I have specified it in the enforcedPlatform() block.
I accepted Sam Brannen's answer below because he explains well how the issue occurs and what solves it and I think it's relevant for those who use older versions of Gradle.
How do I disable JUnit coming from the Gradle Spring Dependency Management plugin?
For starters, if you are using the dependency management plugin from Spring, you should not be importing the junit-bom since that results in duplicate (and potentially conflicting) management of those dependencies.
Aside from that, whenever you use the dependency management plugin from Spring and want to override a managed version, you have to do it by overriding the exact name of the version defined in the BOM used by the plugin.
This is documented in Spring Boot for Gradle and for Maven.
For Spring Boot the name of the JUnit Jupiter version is "junit-jupiter.version". You can find the names of all managed versions for Spring Boot 2.1.2 here.
So, in Gradle you would override it as follows.
ext['junit-jupiter.version'] = '5.4.0'.
You can see that I have done exactly that here.
With Maven you would override it as follows.
<properties>
<junit-jupiter.version>5.4.0</junit-jupiter.version>
</properties>
Further background information here: https://docs.spring.io/platform/docs/current/reference/html/getting-started-overriding-versions.html
JUnit 5.4.0 simplified its artifacts, and now delivered a single artifact for Jupiter - org.junit:junit-jupiter. I.e., you should simplify your Gradle file too:
testImplementation(enforcedPlatform("org.junit:junit-bom:5.4.0")) // JUnit 5 BOM
testImplementation("org.junit.jupiter:junit-jupiter")
Ensure to include JUnit's BOM before other BOMs that also refer to JUnit. First BOM wins and locks version of all later artifacts.
See this issue for a similar setup using Maven and Spring Boot: https://github.com/sormuras/junit-platform-maven-plugin/issues/29#issuecomment-456958188

maven is not downloading any dependency that I have add

I have install maven and JDK and tried to create maven project and add some dependencies, as you can see in the screenshot but the dependency is not downloading so I can not import. can you help.
That's because you are using dependencyManagement, where you only specify dependency meta-information, but not the actual dependencies.
More details about managing dependencies via Maven you can find here:
maven dependency mechanism
maven dependency management
Change the version of guice to 4.1.0
What it seems like there is no 4.1
https://mvnrepository.com/artifact/com.google.inject/guice

Use maven transitive dependency in build

Is there a way to use the transitive dependency of some maven module instead using the default maven lib version?
For example:
My project has a dependency on qulice-maven-plugin, which depends on qulice-checkstyle, which depends on checkstyle libs. I want to to run checkstyle in my project, but using the same version and configuration which is loaded from these transitive dependencies.
If I run mvn checkstyle:checkstyle, which is the command for running checkstyle, it loads a default checkstyle version and uses its default configuration. I don't want to copy all my configurations from these dependencies, I just want to maven to be smart enough to execute checkstyle using the dependencies defined above.
Is there a way of doing this?
If I run mvn checkstyle:checkstyle it loads a default checkstyle version
If yours is loading version 6.18 then see https://stackoverflow.com/a/27359107/1016482 on how to override it to use a newer version.
run mvn checkstyle:checkstyle, which is the command for running checkstyle
If you want to fail the build when there is a checkstyle violation in your project, then this is not the command. As shown at https://maven.apache.org/plugins/maven-checkstyle-plugin/checkstyle-mojo.html , this will just generate a file report and not fail the build if there are checkstyle violations in your project.
For this purpose they recommend checkstyle:check and https://maven.apache.org/plugins/maven-checkstyle-plugin/usage.html#Checking_for_Violations_as_Part_of_the_Build shows an example on how to configure the it for your custom parameters.
I don't want to copy all my configurations from these dependencies
You should be able to use the embedded configuration just like sun and google configurations which are embedded in checkstyle. Just add it as a dependency and specify the config location, like /my/path/my_config.xml from the root of the dependency like you would loading a resource.

Categories