How to run shell commands in init.gradle.kts with gradle 8.13?

In my init.gradle.kts, I have this block:

initscript {
    fun fetchCodeArtifactToken(): String {
        val output = java.io.ByteArrayOutputStream()

         exec {
            commandLine(
                "aws", "codeartifact", "get-authorization-token",

Our conventions plugins are in codeartifact, so without this access nothing works.
exec is now deprecated, and the only alternative I can find documentation for involves a task, which I can’t rely on, since the project object does not exist during this script execution.

What is the correct way to define this function or just to run the shell command in this timing?

Hm, when deprecating this in InitScriptApi they maybe did this accidentally, especially as the deprecation note only contains stuff usable at other places but not in an init script.

I recommend you create an issue over at GitHub so that they can consider reverting this deprecation as there is no proper replacement.

In the meantime, the work-around would probably be to use normal Kotlin means to call external process, so ProcessBuilder for example or whatever the Kotlin substitute is, if there is one.

Thanks,
ProcessBuilder solved it for me.

This should probably be documented as an alternative for when tasks are not available yet, but the point is there is a working solution that does not indicate deprecation.

The point is, that this would imho not be deprecated at this spot and that you should report to Gradle folks, as there is no Gradle alternative there.