Gradle How can i specify the cacheResolutionStrategy for the SNAPSHOT version in my buildscript block? - gradle

I am having problems with resolutionStrategy.cacheChangingModulesFor.
My project build.gradle looks similar to this
apply plugin: 'base'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply from: "gradle/mixins/cachestrategy.gradle"
configurations.all {
resolutionStrategy.cacheDynamicVersionsFor 5, 'minutes'
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
buildscript {
repositories {
maven {
url artifactoryUrl
}
}
dependencies {
classpath (group: 'com.myorg', name: 'aCustomPlugin', version: '1.5.0-SNAPSHOT') {
changing = true
}
}
}
allprojects {
apply plugin: 'base'
apply plugin: 'com.myorg.aCustomPlugin'
}
my question is: How can i specify the cacheResolutionStrategy for the SNAPSHOT version in my buildscript block?

specifying it outside the block, doesn't work (since the buildscript block is evaluated first, in order to build the scripts... ) so the cache strategy rules defined in the scripts haven't been evaluated yet.
the resolution strategy should be placed in the buildscript block like this
buildscript {
repositories {
mavenLocal()
maven {
url artifactoryUrl
}
}
dependencies {
classpath (group: 'com.myorg', name: 'aCustomPlugin', version: '1.5.0-SNAPSHOT') {
changing = true
}
}
configurations.all {
resolutionStrategy.cacheDynamicVersionsFor 5, 'minutes'
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
}

Related

Could not find method mavenContent() for arguments

I am trying to add a private nexus maven repo/utility to a gradle project (referred: https://docs.gradle.org/current/userguide/declaring_repositories.html#maven_repository_filtering).
Posting this issue as I haven't found any solution yet.
// Apply the java-library plugin to add support for Java Library
apply plugin: 'eclipse'
apply plugin: 'application'
apply plugin: 'spring-boot'
apply plugin: 'net.researchgate.release'
apply plugin: 'maven'
// Configure project-level settings.
project.ext {
mainClass = "${mainClass}"
configFile = "$rootProject.projectDir/src/resources/application.properties"
finalJarName = "${name}-${version}.jar"
javaVersion = '1.8'
springBootVersion = '1.3.4'
springBootActuatorClient = '1.5.3.RELEASE'
apacheCommonIOVersion = '2.5'
camelVersion = '2.17.2'
solrVersion = '5.4.0'
}
// JAR/FatJAR configuration
mainClassName = "${mainClass}"
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
flatDir {
dirs 'lib'
}
maven {
url "http://nexus.repo.com/nexus/content/repositories/releases/"
credentials {
username = "${mavenUser}"
password = "${mavenPassword}"
}
mavenContent {
releasesOnly()
}
}
}
// Configure the build script.
buildscript {
ext {
springBootVersion = '1.3.6.RELEASE'
systemrulesVersion = '1.16.1'
}
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'net.researchgate:gradle-release:2.6.0'
}
}
dependencies {
........
compile group: 'com.some.utility.third-party-integrations', name: 'third-party-integrations', version: '1.0.29'
}
Firstly, the above implementation gives me error as,
Could not find method mavenContent() for arguments [build_6vwlfgalblecd5o2a6rgk023i$_run_closure1$_closure3$_closure6$_closure8#36b310aa] on org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_Decorated#10c2064a.
Secondly, if I remove that it generates an error for the dependency that I have added as below,
Could not resolve com.some.utility.third-party-integrations:third-party-integrations:1.0.29.
> inconsistent module metadata found. Descriptor: com.some.utility.third-party-integrations:third-party-integrations:1.0.0-SNAPSHOT Errors: bad version: expected='1.0.29' found='1.0.0-SNAPSHOT'

Gradle: create .jar from .class files and include it in EAR_dir/lib when EAR is built

You'd think this'd be easy enough. Gradle/Maven were designed specifically to get rid of build nightmares. And yet... I have scoured the web, including SO. I would prefer to be using Maven but alas this is not in my control.
My master build.gradle file looks like this:
buildscript {
repositories {
maven { url "https://aaa.com/xxxx/aaa-mvn" }
}
dependencies {
{redacted}
classpath "com.aaa.plugin.gradle:module-plugin:1.+"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.4"
}
}
apply plugin: 'maven-publish'
apply plugin: {redacted}
apply plugin: {redacted}
apply plugin: 'build-defaults'
apply plugin: 'module-plugin'
apply plugin: 'war'
description = 'xxxxxx'
defaultTasks 'build','install'
dependencies {
jbossModule(group: 'com.aaa.bbbb', name: 'inf-jdbc', version: '3.0.2')
}
artifactory {
publish {
repoKey=version.endsWith("SNAPSHOT") ? 'aaa-mvn-dist-snapshots' : 'aaa-mvn-dist'
defaults {
publications('mavenJava')
}
}
}
publishing {
publications {
mavenJava(MavenPublication) {
artifact project(':xxxEAR').ear
}
}
}
repositories {
maven { url "https://xxxx.aaa.com/artifactory/aaa-mvn" }
}
This is gradle.build for my .war. I have a .war inside an .ear.
description = 'Pricing'
buildscript {
repositories {
maven { url "https://xxx.aaa.com/artifactory/aaa-mvn" }
}
dependencies {
classpath "com.aaa.plugin.gradle:ucd-publish-plugin:1.+"
classpath "com.aaa.plugin.gradle:build-defaults-plugin:1.+"
classpath {redacted}
classpath {redacted}
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jacoco'
apply plugin: {redacted}
apply plugin: 'ucd-publish'
apply plugin: 'build-defaults'
apply plugin: {redacted}
apply plugin: 'maven'
apply plugin: 'maven-publish'
repositories {
maven { url "https://xxxx.aaa.com/artifactory/aaa-mvn" }
}
war {
archiveName 'xxxx.war'
}
dependencies {
compile {redacted}
compileOnly group: 'javax', name: 'javaee-api', version:'7.0'
testCompile "junit:junit:4.12"
compile 'io.swagger:swagger-annotations:1.5.10'
compile(group: 'com.aaa.inf', name: 'inf-jdbc', version: '3.0.2', classifier: 'sources')
compile(group: 'com.aaa.inf', name: 'inf-throttle', version: '3.0.1')
compile(group: 'com.ibm.db2', name: 'db2jcc', version: '3.64.133')
compile {redacted}
}
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.0
}
}
}
}
The build.gradle file for my .ear is:
description = 'xxxxx'
buildscript {
repositories {
maven { url "https://xxxx.aaa.com/artifactory/aaa-mvn" }
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.5"
classpath "com.aaa.plugin.gradle:build-defaults-plugin:1.+"
classpath "com.aaa.plugin.gradle:ucd-publish-plugin:1.+"
classpath "com.aaa.plugin.gradle:openapi-generator-gradle-plugin:1.+"
}
}
apply plugin: 'ear'
apply plugin: 'maven'
apply from: 'build.local-jboss.gradle'
apply plugin: 'maven-publish'
apply plugin: {redacted}
apply plugin: 'ucd-publish'
apply plugin: 'build-defaults'
apply plugin: {redacted}
configurations{
gen
}
dependencies {
deploy project (path: ":pricingWAR", configuration: 'archives')
earlib 'com.aaa.inf:inf-api-auth:1.4.+'
earlib {redacted}
earlib group: 'javax.security.enterprise', name: 'javax.security.enterprise-api', version: '1.0'
earlib (group: {redacted}) {
exclude group: 'javax.security.dddddd'
}
gen "io.swagger:swagger-codegen:2.2.2"
}
artifacts{
archives ear
}
repositories {
maven {
url "https://xxxx.aaa.com/artifactory/aaa-mvn"
}
}
task cleanVolumes(type: Delete) {
delete fileTree(dir: "./volumes/deployments/")
}
task copyEar(type: Copy) {
tasks.cleanVolumes.execute()
from "build/libs"
into "${project.projectDir}/volumes/deployments"
fileMode = 0644
}
build.finalizedBy(generateSwagger)
swaggerConfig {
archive = ear.archivePath
outputFormat = "JSON"
outputPath = project.buildDir.toString() + "/swagger"
}
Just looking to do this: .jar up the .class files compiled in my business classes and add them to EAR_file\lib.
I've gone so far as to try to use Gradle's native Groovy nature to write code that builds the .jar and moves it into EAR_file\lib. This of course is ridiculous. And worse still, it does not quite work. Paired with auto-deploy there are timing issues.
Some of you no doubt are wincing.
To save me from such insanity, please let me know what you know about this. I have of course tried various permutations of:
apply plugin: 'java'
...
jar {
...
}
to no avail.
Thank you in advance.
Finally have it working after helpful input from coworkers. For others' benefit here is the whole thing it ended up as:
In build.gradle in the EAR project level:
// My project is named: xxx-svc and the .war subproject is named pricing-theAPI
dependencies {
deploy project (path: ":xxx-theAPI", configuration: 'archives') // This was here already
earlib project(path: ":xxx-theAPI", configuration: 'customJar') // This is new
}
In build.gradle in the WAR project level:
configurations {
customJar
}
task doJar(dependsOn:classes, type: Jar){
from sourceSets.main.output
include 'com/xyz/xxxxx/yyyyyy/zzzz/**' // See Note 1
}
artifacts {
customJar doJar
}
Note 1: I added this modifier b/c in my case I needed only the .class files at the given location in the .jar and in fact had to exclude all others. Note the Gradle docs have the modifier "include" misidentified. They call it "includes" but in fact it is "include".

Gradle changing module from nexus snapshot repo not updated

I have a problem with a snapshot artifact not being uploaded.
I'm using snapshot version. Atrifact is marked explicitly as changing: true and cacheChangingModulesFor is set to 0 seconds.
When i run --refresh-dependecies the artifact is redownloaded properly.
I found the problem while using gradle 2.9. But after upgrading to 2.14.1 the issue remains.
Below is my build.gradle file:
buildscript {
ext {
springBootVersion = '1.3.5.RELEASE'
}
repositories {
mavenCentral()
maven { url 'http://repo.spring.io/plugins-release' }
}
// dependencies for plugins
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.7'
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'propdeps-idea'
configurations.all {
// Check for updates every build
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
jar {
baseName = 'someproject'
version = '0.0.1-SNAPSHOT'
}
war {
baseName = "someproject"
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
maven {
url 'http://nexus.example.com:8081/nexus/content/repositories/java-libs-snapshots/'
credentials {
username "someuser"
password "somepassword"
}
}
}
// enables to run with dev profile: $ gradle local bootRun
task local << {
bootRun.systemProperty 'spring.profiles.active', 'local'
}
bootRun {
jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"]
}
dependencies {
compile 'mysql:mysql-connector-java'
compile 'org.springframework.boot:spring-boot-starter-web'
compile group: "pl.example", name: "name", version: "0.7.6.1-SNAPSHOT", changing: true
compile 'org.jadira.usertype:usertype.core:5.0.0.GA'
compile group: 'com.rometools', name: 'rome', version: '1.6.0'
compile group: 'org.jsoup', name: 'jsoup', version: '1.9.2'
compile 'org.hibernate:hibernate-search:5.5.3.Final'
compile 'org.projectlombok:lombok:1.16.6'
}
I think you maybe have the syntax for the changing dependency slightly wrong.
Can you try replacing:
compile group: "pl.example", name: "name", version: "0.7.6.1-SNAPSHOT", changing: true
with:
compile ("pl.example:name:0.7.6.1-SNAPSHOT"){ changing = true }
Can you also try adding a line to the resolutionStrategy for cacheDynamicVersionsFor as follows:
configurations.all {
resolutionStrategy {
cacheDynamicVersionsFor 0, "seconds"
cacheChangingModulesFor 0, "seconds"
}
}

Gradle: buildscript's resolution strategy

Is it possible to set the resolution strategy for the builscript, so that the version of a gradle plugin can be set centrally? For example:
build.gradle:
buildscript {
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin'
}
}
apply from: 'common.gradle'
apply plugin: 'spring-boot'
dependencies {
...
}
common.gradle:
allprojects { project ->
buildscript {
repositories {
jcenter()
}
}
configurations.all {
resolutionStrategy {
eachDependency { details ->
if (details.requested.group == 'org.springframework.boot' and details.requested.name == 'spring-boot-gradle-plugin')
details.useVersion '1.3.1.RELEASE'
}
}
}
}
I've tried about 100 different variants of the above, all result in an error saying the spring boot gradle plugin version can't be resolved (which is still empty)
A slightly different approach to centrally set the plugin version: use init.gradle
In your init.gradle:
allprojects {
ext.springBootGradlePluginVersion = '1.3.1.RELEASE'
println "spring-boot-gradle-plugin version set in init.gradle to $springBootGradlePluginVersion"
}
Then in your individual projects:
buildscript {
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootGradlePluginVersion"
}
}
apply from: 'common.gradle'
apply plugin: 'spring-boot'
You can either use a global init.gradle in your home folder, or invoke it per project while running gradle with the -I command line option.

How to apply Cobertura plugin to all projects or all subprojects

I'm trying to apply the Cobertura plugin to all projects and subprojects in my Gradle build scripts. However, the scripts are unable to find the plugin when applied to all. Here is what I've got:
buildscript {
repositories {
jcenter()
}
}
allprojects {
beforeEvaluate {
project.buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'net.saliman:gradle-cobertura-plugin:2.2.7'
}
}
}
}
subprojects {
apply plugin: 'net.saliman.cobertura'
}
This is how build.gradle should look like:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'net.saliman:gradle-cobertura-plugin:2.2.7'
}
}
allprojects {
apply plugin: 'net.saliman.cobertura'
}
You may also find that you want to merge all your subprojects cobertura reports into one beautiful top level report for the entire project.
To do this you will need cobertura gradle plugin 2.2+ I believe and the configuration for that is something like:
// note - all non-cobertura config is stripped out of this example
allprojects {
apply plugin: 'cobertura'
}
subprojects {
cobertura {
coverageIgnoreTrivial = true
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "net.saliman:gradle-cobertura-plugin:2.2.2"
}
}
def files = subprojects.collect { new File(it.projectDir, '/build/cobertura/cobertura.ser') }
cobertura {
coverageFormats = [ 'xml', 'html' ]
coverageSourceDirs = subprojects.sourceSets.main.allSource.srcDirs.flatten()
coverageMergeDatafiles = files
}
test.dependsOn(subprojects.test)
which is from syncsynchalt's great comment on the issue here:
https://github.com/stevesaliman/gradle-cobertura-plugin/issues/10

Resources