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.
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):
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
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?
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.