Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Add sample demonstrating how to configure the mavenDeployer section from the uploadArchives task #142

Closed
bamboo opened this issue Sep 26, 2016 · 1 comment

Comments

@bamboo
Copy link
Member

bamboo commented Sep 26, 2016

Groovy example:

uploadArchives {
   repositories {
       mavenDeployer {

           repository(url: "${nexusUrl}/releases") {
               authentication(userName: "${nexusUser}", password: "${nexusPassword}")
           }

           snapshotRepository(url: "${nexusUrl}/snapshots") {
               authentication(userName: "${nexusUser}", password: "${nexusPassword}")
           }

           pom {

               version = version
               artifactId = 'my-artifact'
               groupId = 'uk.com.foo.app'

               project {
                   parent {
                       groupId "org.springframework.boot"
                       artifactId "spring-boot-starter-parent"
                       version "${springBootVersion}"
                   }
               }
           }
       }
   }
}
@eskatos
Copy link
Member

eskatos commented Jul 19, 2017

Here is a first attempt:

val nexusUrl = "..."
val nexusUser = "..."
val nexusPassword = "..."
val springBootVersion = "..."

tasks {
    "uploadArchives"(Upload::class) {
        val mavenConvention = org.gradle.api.internal.plugins.DslObject(repositories).convention.getPlugin(MavenRepositoryHandlerConvention::class.java)
        val mavenDeployer = mavenConvention.mavenDeployer()
        mavenDeployer.run {

            val repository = javaClass.getMethod("repository", Map::class.java)
                .invoke(this, mapOf("url" to "$nexusUrl/releases"))
            repository.javaClass.getMethod("authentication", Map::class.java)
                .invoke(repository, mapOf("userName" to nexusUser, "password" to nexusPassword))

            val snapshotRepository = javaClass.getMethod("snapshotRepository", Map::class.java)
                .invoke(this, mapOf("url" to "$nexusUrl/snapshots"))
            snapshotRepository.javaClass.getMethod("authentication", Map::class.java)
                .invoke(snapshotRepository, mapOf("userName" to nexusUser, "password" to nexusPassword))

            pom(closureOf<org.gradle.api.artifacts.maven.MavenPom> {
                version = project.version.toString()
                artifactId = "my-artifact"
                groupId = "uk.com.foo.app"


                project.parent.run {
                    groupId = "org.springframework.boot"
                    artifactId = "spring-boot-starter-parent"
                    version = springBootVersion
                }
            })
        }
    }
}

mavenDeployer is only accessible through a dynamic Convention present on the repositories field of the Upload task type. In Groovy you can just type mavenDeployer and dynamic dispatching take care of looking up the convention. Here we do it explicitely. Using the internal DslObject type is a bad idea though.

Then, to create repositories we need to invoke methods from the internal DefaultGroovyMavenDeployer type but we can't cast here and need to rely on reflection because these internal methods expose some types from the org.apache.maven.* packages that aren't present in the build script compilation classpath.

Finally the POM customization only need to use the closureOf() helper that comes with the Kotlin DSL.

@bamboo bamboo self-assigned this Jul 27, 2017
@bamboo bamboo modified the milestones: 0.11.0, 1.0.0 Jul 27, 2017
bamboo added a commit to gradle/gradle that referenced this issue Jul 27, 2017
bamboo added a commit to gradle/gradle that referenced this issue Jul 27, 2017
bamboo added a commit that referenced this issue Jul 28, 2017
@bamboo bamboo closed this as completed Jul 28, 2017
bamboo added a commit to gradle/gradle that referenced this issue Aug 4, 2017
bamboo added a commit to gradle/gradle that referenced this issue Aug 15, 2017
bamboo added a commit to gradle/gradle that referenced this issue Aug 15, 2017
Also:
- Add `Action<T>` overloads to `PomFilterContainer`
- Add `Action<T>` overloads to `MavenRepositoryHandlerConvention`
- Accept `maven` plugin API changes

See gradle/kotlin-dsl-samples#142
bamboo added a commit to gradle/gradle that referenced this issue Aug 18, 2017
Also:
- Add `Action<T>` overloads to `PomFilterContainer`
- Add `Action<T>` overloads to `MavenRepositoryHandlerConvention`
- Accept `maven` plugin API changes

See gradle/kotlin-dsl-samples#142
bamboo added a commit to gradle/gradle that referenced this issue Aug 18, 2017
bamboo added a commit to gradle/gradle that referenced this issue Aug 22, 2017
Also:
- Add `Action<T>` overloads to `PomFilterContainer`
- Add `Action<T>` overloads to `MavenRepositoryHandlerConvention`
- Accept `maven` plugin API changes

See gradle/kotlin-dsl-samples#142
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants