转换为gradle-kotlin-dsl Jfrog,Artifactory配置

ttisahbt  于 2023-05-29  发布在  Kotlin
关注(0)|答案(4)|浏览(200)

我正在尝试从groovy迁移到gradle-kotlin dsl,但我是新手,所以我不知道如何配置Jfrog Artifactory。请帮助我将这部分代码转换为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)
                }
            }
        }
    }
}
63lcw9qa

63lcw9qa1#

我是这样解决的:

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")
    })
oogrdqng

oogrdqng2#

我找到了另一个例子-一个官方的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)
        }
    }
}
wdebmtf2

wdebmtf23#

接受的答案并不适合我的artifactory插件版本4.27.1。一个很好的地方肯定是前面提到的provided example by JFrog。以下是一个基于官方示例的简化摘录:

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

    setContextUrl(findProperty("artifactory_url"))
    publish {
        repository {
            setRepoKey(repoKeyValue) // The Artifactory repository key to publish to
            setUsername(findProperty("artifactory_user")) // The publisher user name
            setPassword(findProperty("artifactory_password")) // The publisher password
            setMavenCompatible(true)
        }

        defaults {
            publications("mypublicationreference")
            setPublishArtifacts(true)            
        }
    }
}

configure<PublishingExtension> {
    publications {
        register<MavenPublication>("mypublicationreference") {
            from(components.getByName("java"))
            artifact("$buildDir/libs/${project.name}.jar")
        }
    }
}

请注意,artifactory_url等引用属性可以在gradle.properties文件中定义。

8zzbczxx

8zzbczxx4#

你可以用这种方式简化接受的答案。

plugins {
    id("com.jfrog.artifactory") version "4.32.0"
}

repositories {
    maven("https://jcenter.bintray.com/")
    mavenLocal()
    mavenCentral()
}

// If you set these values in gradle.properties

val artifactoryContextUrl: String by project
val artifactoryUser: String by project
val artifactoryPassword: String by project

artifactory {
    setContextUrl(artifactoryContextUrl)
    publish {
        repository {
            setProperty("repoKey", "gradle-dev-local")
            setProperty("username", artifactoryUser)
            setProperty("password", artifactoryPassword)
        }
        defaults {
            invokeMethod("publications", "mavenJava")
            setProperty("publishPom", true)
            setProperty("publishArtifacts", true)
        }
    }
    resolve {
        setProperty("repoKey", "repo")
    }
}

相关问题