I'm trying to write an initialization script as explained here. So I wrote an init.gradle.kts
file and put in the following code
import java.net.URI
apply<EnterpriseRepositoryPlugin>()
class EnterpriseRepositoryPlugin : Plugin<Gradle> {
companion object {
const val ENTERPRISE_REPOSITORY_URL = "http://localhost:8081/nexus/content/groups/public/"
}
override fun apply(gradle: Gradle) {
// ONLY USE ENTERPRISE REPO FOR DEPENDENCIES
gradle.allprojects {
repositories {
// Remove all repositories not pointing to the enterprise repository url
all {
if (this !is MavenArtifactRepository || url.toString() != ENTERPRISE_REPOSITORY_URL) {
project.logger.lifecycle("Repository ${(this as? MavenArtifactRepository)?.url ?: name} removed. Only $ENTERPRISE_REPOSITORY_URL is allowed")
remove(this)
}
}
// add the enterprise repository
add(maven {
name = "STANDARD_ENTERPRISE_REPO"
url = uri(ENTERPRISE_REPOSITORY_URL)
})
}
}
}
}
data class RepositoryData(val name: String, val url: URI)
allprojects {
tasks.register("repos") {
val repositoryData = repositories.withType<MavenArtifactRepository>().map { RepositoryData(it.name, it.url) }
doLast {
repositoryData.forEach {
println("repository: ${it.name} ('${it.url}')")
}
}
}
}
When I execute the added task that list the repos
./gradlew --init-script init.gradle.kts repos
it outputs
repository: STANDARD_ENTERPRISE_REPO ('http://localhost:8081/nexus/content/groups/public/')
as expected.
But when I use that initialization script with another project that needs to download artifacts, gradle says
> Switch Maven repository 'STANDARD_ENTERPRISE_REPO(http://localhost:8081/nexus/content/groups/public/)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols.
> For more information, please refer to https://docs.gradle.org/8.2/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol in the Gradle documentation.
The documentation that the error message points to says that I should set allowInsecureProtocol = true
, but where should I do that?
If I add it to my init script it doesn't work. I did the following edit to the init.gradle.kts
I showed you above:
add(maven {
name = "STANDARD_ENTERPRISE_REPO"
url = uri(ENTERPRISE_REPOSITORY_URL)
allowInsecureProtocol = true
})
But then gradle says:
Script compilation error:
Line 27: allowInsecureProtocol = true
^ Unresolved reference: allowInsecureProtocol
So how should I set that property to a repository added via an initialization script?
The correct syntax in Kotlin DSL to allow repository access over standard HTTP is to write:
maven {
isAllowInsecureProtocol = true
}