maven - 如何将 gradle 中的 groovy 任务转换为 Gradle Kotlin DSL 以生成 pom.xml?

标签 maven gradle kotlin pom.xml gradle-kotlin-dsl

以下 Gradle 脚本的 build.gradle.kts 版本是什么?

apply plugin: 'maven'
apply plugin: 'java'

sourceCompatibility = 7
targetCompatibility = 7

dependencies {
    compile            'com.google.guava:guava:13.0.1'
    compile            'joda-time:joda-time:2.1'

    testCompile        'junit:junit:4.11'
    testCompile        'org.mockito:mockito-core:1.9.5'
}

task writeNewPom << {
    pom {
        project {
            groupId 'org.example'
            artifactId 'test'
            version '1.0.0'

            inceptionYear '2008'
            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }
        }
    }.writeTo("$buildDir/newpom.xml")
}

引用文献

1- Gradle 示例为 here .

最佳答案

我相信这与 build.gradle.kts 文件相同:

plugins {
    java
    maven
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}

repositories {
    jcenter()
}

dependencies {
    compile("com.google.guava:guava:13.0.1")
    compile("joda-time:joda-time:2.1")

    testCompile("junit:junit:4.11")
    testCompile("org.mockito:mockito-core:1.9.5")
}

tasks {
    "writeNewPom" {
        doLast {
            project.the<MavenPluginConvention>().pom {
                project {
                    groupId = "org.example"
                    artifactId = "test"
                    version = "1.0.0"
                    withGroovyBuilder {
                        "inceptionYear"("2008")
                        "licenses" {
                            "license" {
                                "name"("The Apache Software License, Version 2.0")
                                "url"("http://www.apache.org/licenses/LICENSE-2.0.txt")
                                "distribution"("repo")
                            }
                        }
                    }
                }
            }.writeTo("$buildDir/newPom.xml")
        }
    }
}

您必须使用withGroovyBuilder方法将非类型化属性添加到模型

关于maven - 如何将 gradle 中的 groovy 任务转换为 Gradle Kotlin DSL 以生成 pom.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48514138/

相关文章:

android - com.huawei.hms :push:5. 3.0.301 中不推荐使用 "AGConnectServicesConfig"类

java - 在 Android 上将 UTC 转换为 EST 不适用于夏令时

java - Eclipse 为类练习设置 Maven 项目时出错 : Check $M2_HOME environment variable and mvn script match

Android Studio 在构建期间每次都会下载 maven 元数据

java - 构建一个 jar 以包含未获得执行权限的 shell 脚本文件

java - 提交 Android 构建后出现 Flurry CN1Lib 错误

java - 使Maven构建更快的方法?

Android Studio gradle 风格未在 Build Variants 中显示

android - 带 Lint 的StackOverflowError

java - Android Paho Mqtt - close() 与 close() 对比unregisterResources() - 内存不足错误