How can I force a gradle build to re-download a SNAPSHOT plugin from Maven repository on every build - gradle

As shown below, I have a gradle plugin that is built deployed as a SNAPSHOT version in a maven repository.
I am aware that -SNAPSHOT dependencies should automatically be recognised as changing=true
I also know that the resolutionStrategy should allow forcing a new download for all configurations.
However, in this context, the setting has no effect.
Question: Can you provide a corrected sample, based on the one below, that forces a re-download on every run, or explain how such a configuration can be achieved?
buildscript {
repositories { maven { url "http://moomoomoo.com:8081/artifactory/moomoomoo-plugins" } }
dependencies { classpath 'com.moomoomoo:moomootools:1.3-SNAPSHOT' }
}
configurations.all { resolutionStrategy.cacheChangingModulesFor 0, 'seconds' }
apply plugin: 'com.moomoomoo.moomootool'

To configure the resolution strategy of the classpath configuration for the build script you have to configure it inside the buildscript block:
buildscript {
repositories { maven { url "http://moomoomoo.com:8081/artifactory/moomoomoo-plugins" } }
configurations.all { resolutionStrategy.cacheChangingModulesFor 0, 'seconds' }
dependencies { classpath 'com.moomoomoo:moomootools:1.3-SNAPSHOT' }
}
apply plugin: 'com.moomoomoo.moomootool'

Related

ResolutionStrategy for Gradle SNAPSHOT plugins

I have this pluginManagement section in my settings.gradle:
pluginManagement {
plugins {
id "org.springframework.boot" version "3.0.0-SNAPSHOT"
id "org.springframework.boot.aot" version "3.0.0-SNAPSHOT"
}
}
Gradle will now check for every subproject in the configuring phase if there's a new version of the org.springframework.boot and org.springframework.boot.aot plugin, presumably because they have SNAPSHOT versions.
Is there a way to set the resolutionStrategy for plugins to say, only check every 24 hours for a new version?
I currently have this workaround:
// This is needed to prevent gradle from checking for newer versions of the Spring Boot
// plugin on every build in every sub-project
buildscript {
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:3.0.0-SNAPSHOT")
}
configurations.classpath {
resolutionStrategy {
cacheDynamicVersionsFor 24, 'hours'
cacheChangingModulesFor 24, 'hours'
}
}
}

Gradle plugin with custom group id

Gradle 6.1
I am having difficulties to use the new plugin configuration mode in Gradle with a custom plugin coming from a custom repository.
buildscript {
repositories {
maven {
url = uri("https://custom")
}
mavenCentral()
jcenter()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
}
plugins {
java
idea
id("com.custom.gradle.plugin.myplugin") version "1.1.0"
}
I get this error:
Plugin [id: 'com.custom.gradle.plugin.myplugin', version: '1.1.0'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.custom.gradle.plugin.myplugin:com.custom.gradle.plugin.myplugin:1.1.0')
Searched in the following repositories:
Gradle Central Plugin Repository
Gradle will use the plugin id as its group id.
It works if I use the old ways:
buildscript {
repositories {
maven {
url = uri("https://custom")
}
mavenCentral()
jcenter()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("com.custom:com.custom.gradle.plugin.myplugin:1.1.0")
}
}
apply(plugin = "com.custom.gradle.plugin.myplugin")
Is there a way to specify the group id with the 'id' command? Or am I breaking the plugin definition's contract with that old plugin?
In order to use the newer/preferred plugins { } DSL, the custom plugin must publish a plugin marker artifact.
If the custom plugin is able to be modified, then I suggest updating to make use of the Java Gradle Plugin Development plugin which will create the marker for you.
If the plugin is not able to be updated, then you can still use the plugins { } block, but you'll need to manually resolve the plugin:
In the main build.gradle:
plugins {
id("com.custom.gradle.plugin.myplugin") version "1.1.0"
}
Then resolve the plugin manually in settings.gradle:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.custom.gradle.plugin.myplugin") {
useModule("com.custom:com.custom.gradle.plugin.myplugin:${requested.version}")
}
}
}
}
See Plugin Resolution Rules
for more details.

How to prioritize mavenLocal over artifactory repo in gradle?

I have multi-modules project and I'm using artifactory for resolving custom libraries:
build.gradle of parent project:
...
subporjects {
...
apply plugin: "com.jfrog.artifactory"
artifactory {
resolve {
contextUrl = ext.getProperty('ARTIFACTORY_URL')
repoKey = ext.getProperty('ARTIFACTORY_REPO_NAME')
username = ext.getProperty('ARTIFACTORY_USERNAME')
password = ext.getProperty('ARTIFACTORY_PASSWORD')
}
}
}
It works as expected my library is published to artifactory with gradle artifactoryPublish and then it's fetched from there. But in some cases I want to fetch my custom library from mavenLocal() repo. I have next subproject build.gradle
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile 'my-custom-library'
}
But as I can see it is still resolves from artifactory. Can I somehow prioritize mavenLocal() over it ?
The repository priority will be the order in which they were added to the RepositoryHandler
I'm guessing that the artifactory repository is added when the plugin is applied so you could delay this by
afterEvaluate {
subprojects {
apply plugin: "com.jfrog.artifactory"
// etc
}
}
Or maybe
evaluationDependsOnChildren()
subprojects {
apply plugin: "com.jfrog.artifactory"
// etc
}
If all you want to do is to depend on one subproject from within another, you should declare dependencies using the project notation:
dependencies {
compile project(':shared')
}
https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:project_jar_dependencies

Generate local maven repository with all dependencies using Gradle

What I currently have
dependencies {
compile ...
}
task copyDependencies(type:Copy) {
from configurations.compile
into 'build/dependencies/'
}
This task copies all the required dependencies to the build/dependencies/ directory. Inside the directory, it looks as follows:
/dep1-1.0.jar
/dep2-1.0.aar
...
So essentially what is known as flatDir in gradle terms.
What I'd like to have
It's a local maven repository with all these dependencies, instead of a flatDir.
If I understand your question correctly, you just want to publish to a local directory as if it were mavenLocal() but in a location you specify.
In that case, I believe you just need:
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "/path/to/wherever"
}
}
}
See the Maven Publish plugin docs

Why do we need repositories/dependencies in buildscript task for gradle

In the post Using gradle/clojuresq to build clojure, and the answer https://stackoverflow.com/a/29018574/260127, there are (or seems) duplication of repositories and dependencies.
Why is this? Why do we need another set with the same setup?
buildscript { <- first
repositories {
maven { url "http://clojars.org/repo" }
mavenCentral()
}
dependencies {
classpath "clojuresque:clojuresque:1.7.0"
}
}
...
-> Same set again!
repositories {
maven { url "http://clojars.org/repo" }
mavenCentral()
}
dependencies {
compile "org.clojure:clojure:1.6.0"
}
...
The first is a dependency required for the build script itself. The dependency you are declaring in this case is clojuresque, which contains the Clojure Gradle plugin (apply plugin: 'clojure'). The second is the dependencies for you project, in this case, the Clojure library itself.
Simply, the first is needed by Gradle, the second by your code.

Resources