4
votes

I'm trying to migrate from groovy to gradle-kotlin dsl, but I'm new to it, so I dont know how to configure Jfrog Artifactory. Please help me with converting that part of code to gradle-kotlin dsl:

task sourceJar(type: Jar) {
    from sourceSets.main.allJava
}

artifactory {
    contextUrl = "http://10.0.0.49:8081/artifactory"
    publish {
        repository {
            repoKey = 'gradle-dev-local'
            username = artifactory_username
            password = artifactory_password
        }
        defaults {
            publications('mavenJava')
            publishArtifacts = true
            publishPom = true
        }
    }
}



publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId = "ua.tiras.oloader"
            artifactId = 'core'
            version = "1.0.62"
            artifact("$buildDir/libs/${project.getName()}.jar")
            artifact sourceJar {
                classifier "sources"
            }
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.api.allDependencies.each { dependency ->
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', dependency.group)
                    dependencyNode.appendNode('artifactId', dependency.name)
                    dependencyNode.appendNode('version', dependency.version)
                }
            }
        }
    }
}
2

2 Answers

18
votes

I solved it this way:

val sourcesJar by tasks.creating(Jar::class) {
    dependsOn(JavaPlugin.CLASSES_TASK_NAME)
    classifier = "sources"
    from(sourceSets["main"].allSource)
}

publishing {
    publications {
        create<MavenPublication>("mavenJava") {
            groupId = "ua.tiras"
            artifactId = "aloader-core"
            version = "0.0.4"

            artifact(sourcesJar)
            artifact("$buildDir/libs/${project.name}.jar")

            pom {
                withXml {
                    asNode().appendNode("dependencies").let {
                        for (dependency in configurations["api"].dependencies) {
                            it.appendNode("dependency").apply {
                                appendNode("groupId", dependency.group)
                                appendNode("artifactId", dependency.name)
                                appendNode("version", dependency.version)
                            }
                        }
                    }
                }
            }
        }
    }
}

artifactory {
    setContextUrl("http://dev.tirascloud.com:8081/artifactory")
    publish(delegateClosureOf<PublisherConfig> {
        repository(delegateClosureOf<GroovyObject> {
            setProperty("repoKey", "gradle-dev-local")
            setProperty("username", project.findProperty("artifactory_username") ?: "nouser")
            setProperty("password", project.findProperty("artifactory_password") ?: "nopass")
        })
        defaults(delegateClosureOf<GroovyObject> {
            invokeMethod("publications", "mavenJava")
            setProperty("publishPom", true)
            setProperty("publishArtifacts", true)
        })
    })
    resolve(delegateClosureOf<ResolverConfig> {
        setProperty("repoKey", "repo")
    })
0
votes

I found another example - an official one JFrog:

https://github.com/jfrog/project-examples/blob/master/gradle-examples/gradle-kts-example-publish/build.gradle.kts


import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.jfrog.buildinfo", "build-info-extractor-gradle", "4.+")
    }
    configurations.classpath {
        resolutionStrategy {
            cacheDynamicVersionsFor(0, "seconds")
            cacheChangingModulesFor(0, "seconds")
        }
    }
}

plugins {
    java
    `maven-publish`
}

fun javaProjects() = subprojects.filter {
    File(it.projectDir, "src").isDirectory
}

val currentVersion: String by project

allprojects {
    apply(plugin = "com.jfrog.artifactory")

    group = "org.jfrog.test.gradle.publish"
    version = currentVersion
    status = "Integration"

    repositories {
        maven("http://127.0.0.1:8081/artifactory/libs-release")
    }
}

tasks.named<ArtifactoryTask>("artifactoryPublish") {
    skip = true
}

project("services") {
    tasks.named<ArtifactoryTask>("artifactoryPublish") {
        skip = true
    }
}

configure(javaProjects()) {
    apply(plugin = "java")
    apply(plugin = "maven-publish")

    dependencies {
        testImplementation("junit:junit:4.7")
    }

    configure<PublishingExtension> {
        publications {
            register<MavenPublication>("mavenJava") {
                from(components.getByName("java"))
                artifact(file("$rootDir/gradle.properties"))
            }
        }
    }
}

project("api") {
    apply(plugin = "ivy-publish")

    configure<PublishingExtension> {
        publications {
            register<IvyPublication>("ivyJava") {
                from(components.getByName("java"))

                artifact(file("$rootDir/settings.gradle.kts")) {
                    name = "gradle-settings"
                    extension = "txt"
                    type = "text"
                }
                // The config below will add a extra attribute to the ivy.xml
                // See http://ant.apache.org/ivy/history/latest-milestone/concept.html#extra
                descriptor.withXml {
                    val info = asNode().get("info") as groovy.util.NodeList
                    val first = info.first() as groovy.util.Node
                    first.attributes()["e:architecture"] = "amd64"
                }
            }
        }
    }

    tasks.named<ArtifactoryTask>("artifactoryPublish") {
        publications(publishing.publications["ivyJava"])
    }
}

configure<org.jfrog.gradle.plugin.artifactory.dsl.ArtifactoryPluginConvention> {
    clientConfig.isIncludeEnvVars = true

    setContextUrl("http://127.0.0.1:8081/artifactory")
    publish {
        repository {
            setRepoKey("libs-snapshot-local") // The Artifactory repository key to publish to
            setUsername(findProperty("artifactory_user")) // The publisher user name
            setPassword(findProperty("artifactory_password")) // The publisher password
            // This is an optional section for configuring Ivy publication (when publishIvy = true).
            ivy {
                setIvyLayout("[organization]/[module]/ivy-[revision].xml")
                setArtifactLayout("[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]")
                setMavenCompatible(true) // Convert any dots in an [organization] layout value to path separators, similar to Maven"s groupId-to-path conversion. True if not specified
            }
        }

        defaults {
            // Reference to Gradle publications defined in the build script.
            // This is how we tell the Artifactory Plugin which artifacts should be
            // published to Artifactory.
            publications("mavenJava")
            setPublishArtifacts(true)
            // Properties to be attached to the published artifacts.
            setProperties(mapOf(
                "qa.level" to "basic",
                "dev.team" to "core"
            ))
            setPublishPom(true) // Publish generated POM files to Artifactory (true by default)
            setPublishIvy(true) // Publish generated Ivy descriptor files to Artifactory (true by default)
        }
    }
}