Tasks in init.gradle options

Some of my colleagues use an init.gradle in older versions of Gradle that contains custom tasks that they want placed in every projects build script. I am using Gradle 6.8.2 now, and I have discovered through research that you can’t add custom tasks like that anymore inside the init.gradle script because it will produce an error.

How can I achieve the same result in newer versions of Gradle like 6.8.2? For example two of the tasks relate to building and uploading to different repositories (one alpha and one production). I have removed some of the code relating to company stuff.

	allprojects {
	
	System.setProperty("cloudsmith.debug", "true")
	
	apply plugin: 'maven'
	
	configurations {
		deployerJars
	}

	dependencies {
		deployerJars 'io.cloudsmith.maven.wagon:cloudsmith-maven-wagon:0.2.0'
	}

	repositories {

		maven {
			name = "Cloudsmith API Releases"
			url = "..."
		}

		maven { 
			name = "Company Private Repository"
			url "..."            
		}

		mavenCentral()
		
	}

	uploadArchives {
		
		repositories.mavenDeployer {
			
			configuration = configurations.deployerJars

			repository(url: "...") {
				authentication(...)
			}

			snapshotRepository(url: "...") {
				authentication(...)
			} 
		}
	}

    task uploadAlphaArchives(type: Upload) {
        uploadDescriptor = true
        repositories.mavenDeployer {

            configuration = configurations.deployerJars

            repository(url: "...") {
                authentication(...)
            }

            snapshotRepository(url: "...") {
                authentication(password: "...")
            } 
        }

    }

    task uploadInternalArchives(type: Upload) {
        uploadDescriptor = true
        repositories.mavenDeployer {

            configuration = configurations.deployerJars

            repository(url: "...") {
                authentication(password: "...")
            }

            snapshotRepository(url: "...") {
                authentication(password: "...")
            }
        }
    }
}

Hello,

I’d suggest Using Gradle Plugins (script plugin for minimal migration cost).
Below is a suggestion based on initialization scripts and the newer maven-publish plugin (I am not familiar with the legacy maven plugin) that would apply a global configuration to all projects having that plugin (to use with CI for instance):

afterProject { evaluatedProject ->
    evaluatedProject.pluginManager.withPlugin('maven-publish') {
        evaluatedProject.publishing {
            repositories {
                alpha {
                    url = '...'
                    name = 'alpha'
                    credentials {
                        username = System.env('ALPHA_USERNAME')
                        password = System.env('ALPHA_PASSWORD')
                    }
                }
                production {
                    url = '...'
                    name = 'production'
                    credentials {
                        username = System.env('PRODUCTION_USERNAME')
                        password = System.env('PRODUCTION_PASSWORD')
                    }
                }
            }
        }
    }
}

What error are you getting? What you provided successfully configures for me in 6.8.2 (after I fixed the ... in the authentication calls of course). What did your research find? I’m unaware of any such changes to init scripts and I use them to create custom tasks as well, so I’m curious what you found.

This is the error, but the task does not already exist.

* Where:                                                                                                                                                          
Initialization script 'C:\Users\jmccay.OLYMPUS\.gradle\init.gradle' line: 47                                                                                      
                                                                                                                                                              
* What went wrong:                                                                                                                                                
Cannot add task 'uploadAlphaArchives' 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

Are you 110% sure of that? :slight_smile:

  • Do you have any additional scripts in ~/.gradle/init.d?
  • Do you get the same error if you run ./gradlew tasks on a build with an empty build.gradle?

I get the same error when I renamed it. I don’t have an init.d directory, and I ran

gradle init -I /c/Users/jmccay.OLYMPUS/.gradle/init.gradle

with the same results on a completly empty directory.

To clarify, when you renamed ~/.gradle/init.gradle, did you then run gradle with or without -I?

No. I renamed the task itself. The command I usually use is:

gradle init -I /c/Users/me/.gradle/init.gradle

When I run without the following Gradle command:

gradle init

It sometimes works and sometimes doesn’t. When it works, it did use the gradle.init file in my “home” directory where it is supposed to be located. I started added the “-I /c/Users/me/.gradle/init.gradle” to the command to insure it used it.

This will result in the same init script being applied twice, which will then try to create the same tasks a second time. Using -I does not prevent gradle from loading the default ~/.gradle/init.gradle.

Technically, the files are one and the same. Is there an option to have it ignore the ~/.gradle/init.gradle in favor of the file passed in to the program?

I don’t believe so.

You should not have to include ~/.gradle/init.gradle on the command line. If it is sometimes not being applied then it sounds like you have an issue that needs to be debugged. To start, I suggest you add something like println 'HOME/.gradle/init.gradle being applied' to the top of ~/.gradle/init.gradle so that you can get a little bit more tracking information.