Ignore ~/.gradle/init.gradle?

Using gradle 7.1.

Currently we have some default init.gradle files on our CI/CD server in ~/.gradle/init.gradle that configures some default repositories/publications,

I have a project that needs to configure something different in its own build.gradle file in the project repo and it needs to use these instead of the ones defined in ~/.gradle/init.gradle. But it seems that it keeps using ~/.gradle/init.gradle or actually both.

Is there a way to override this - but still use e.g. ~/.gradle/gradle.properties?

You’ll need to make the content of init.gradle configurable. For example, don’t apply the repos if some property is set, or whatever other condition makes sense. Then the special project will set that property/stimulus on order to prevent the defaults from being applied.

The loading of ~/.gradle/gradle.properties is not impacted by the presence or absence of init scripts, if it exists Gradle will load it.

Ok modifying ~/.gradle/init.gradle on the build server is not an option at the moment. Had hoped for something like:

if repository/publishing found/defined in build.gradle in project use that and skip those found i user dir. Something similar to when you can specify mvn -s mySetting.xml in maven to use the specified file instead of the one - that might exist - in ~/.mvn

While you can’t prevent the execution of the build server’s init.gradle, there is nothing immutable about the repositories/publications set there. These types follow normal collection semantics, and you can just call clear() if you know you don’t want to keep what has already been set:

repositories {
    clear()
    maven { url 'https://otherrepo.mycompany.com/maven' }
}

Or if you want to control it outside the actual build script, set a different gradle user home for example using an environment variable and copy over or symlink the properties file you want to keep being used.

Yes I also tried changing the gradle home dir through env var and that works since we are using a shared gradle cache for all the jobs to optimize build time that will break that “pattern”.

I will give the clear() suggestion a try and see if that works out.

Probably I am missing something regarding calling clear() but currently I can’t get it to work.

Right now this is what build agents have in ~/.gradle/init.gradle

allprojects {
    apply plugin: 'java'
    buildscript {
        repositories {
            maven {
                url "https://internal"
                credentials {
                    username repositoryUsername
                    password repositoryPassword
                }
            }
            maven {
                url "https://internal/snapshots"
                credentials {
                    username repositoryUsername
                    password repositoryPassword
                }
            }
            mavenLocal()
        }
    }
    repositories {
        maven {
            url "https://internal"
            credentials {
                username repositoryUsername
                password repositoryPassword
            }
        }
        maven {
            url "https://internal/snapshots"
            credentials {
                username repositoryUsername
                password repositoryPassword
            }
        }
        mavenLocal()
    }

    task sourceJar(type: Jar) {
        from sourceSets.main.allSource
    }
}

I my local repo/build.gradle I have:

repositories {
    clear()
    maven {
        url "https://internal"
        credentials {
            username repositoryUsername
            password repositoryPassword
        }
    }
    mavenLocal()
}

publishing {
    repositories {
        clear()
        maven {
            credentials {
                username repositoryUsername
                password repositoryPassword
            }
            afterEvaluate {
                if (version.toString().endsWith('-SNAPSHOT')) {
                    url "https://internal/snapshots"
                } else {
                    url "https://internal/"
                }
            }
        }
    }

    publications{
        clear()        
        mavenJava(MavenPublication) {
         artifact bootJar
        }
    }    
}

So I have tried to add some clear() in different places but I still get:

* What went wrong:
A problem occurred evaluating script.
> Cannot add task 'publishAllPublicationsToMavenRepository' as a task with that name already exists.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 7s

What am I missing?

If that’s the entire init.gradle on the build servers, I don’t see any reason you would actually need to overwrite it. Publishing isn’t defined at all and your only difference is not including the snapshot repository. The snapshot repository wouldn’t cause any harm if it’s truly a snapshot repository as your dependencies won’t magically change to a snapshot version.

This looks more like a task named publishAllPublicationsToMavenRepository was accidentally defined instead of maybe just a task dependency. This task is created by the maven-publish plugin, so if you’ve also defined it unintentionally, you would get the error shown.

You are right I had accidentally:

apply plugin: 'maven-publish'

in two places. After fixing that (+ removing the clear() calls) I now get:

Error in 'PUT https://internal/snapshots/....jar'. Waiting 2000ms before next retry, 1 retries left
> Task :publishMavenJavaPublicationToMavenRepository FAILED
:publishMavenJavaPublicationToMavenRepository (Thread[Execution worker for ':' Thread 3,5,main]) completed. Took 19.219 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':publishMavenJavaPublicationToMavenRepository'.
> Failed to publish publication 'mavenJava' to repository 'maven'
   > Could not PUT 'https://internal/snapshots....jar'. Received status code 502 from server: Bad Gateway

That error goes away if I remove ~/.gradle/init.gradle (on a local setup mirroring the build agents) so something seem to be wrong in ~/.gradle/init.gradle.

The only thing I found on the above error is:

but that is not the case - the internal server is up and running + it works when I remove ~/.gradle/init.gradle.

Any ideas?

Correction to the above pasted ~/.gradle/init.gradle file. I found out its actually containing the following (on the build agents):

allprojects {
    apply plugin: 'java'
    apply plugin: 'maven-publish'
    buildscript {
        repositories {
            maven {
                url "https://internal"
                credentials {
                    username repositoryUsername
                    password repositoryPassword
                }
            }
            mavenLocal()
        }
    }
    repositories {
        maven {
            url "https://internal"
            credentials {
                username repositoryUsername
                password repositoryPassword
            }
        }
        mavenLocal()
    }

    afterEvaluate {
        publishing {
            repositories {
                maven {
                    credentials {
                        username repositoryUsername
                        password repositoryPassword
                    }
                    if (project.version.endsWith('-SNAPSHOT')) {
                        url "https://internal/snapshots"
                    } else {
                        url "https://internal/releases"
                    }
                }
            }
        }
    }
}

But based on:

it seems there is not much I can do - or is there some “hack” to disable it from a specific project/build?

Update. I appears it selects the “release” repository when evaluating the ~/.gradle/init.gradle on the build server followed by selecting the (correct) “snapshot” repository but still it tries to upload to the “release” repository which then fails (either because the artifact contains -SNAPSHOT or already exists).

Any input appreciated.