繁体   English   中英

使用Gradle Maven插件时如何设置Java目标和源版本?

[英]How to set java target and source version when using gradle maven plugin?

我正在使用gradle 3.5和maven插件进行gradle。

我有一个生成pom.xml的任务,由于Java的源版本和目标版本,生成的pom是错误的。

这为1.5生成了pom.xml(错误):

task createPom << {
    pom {
        project {
            groupId 'com.domain.api'
            artifactId 'gs-gradle'
            version '0.1.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("pom.xml")
}

这会使gradle makePom任务失败:

task createPom << {
    pom {
        project {
            groupId 'com.domain.api'
            artifactId 'gs-gradle'
            version '0.1.0'
            build {
                plugins {
                    plugin {
                        groupId 'org.apache.maven.plugins'
                        artifactId 'maven-compiler-plugin'
                        version '3.7.0'
                        configuration {
                            source '1.8'
                            target '1.8'
                        }
                    }
                }
            }
            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("pom.xml")
}

这是添加build对象时的输出错误:

* What went wrong:
Execution failed for task ':createPom'.
> No such property: _SCRIPT_CLASS_NAME_ for class: org.apache.maven.model.Model

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

这就是我解决目标和来源问题的方式:

pom {
    project {
        groupId 'com.domain.api'
        artifactId 'gs-gradle'
        version '0.1.0'
        properties {
            project {
                build {
                    sourceEncoding 'UTF-8'
                }
            }
            maven {
                compiler {
                    source '1.8'
                    target '1.8'
                }
            }
        }

        inceptionYear '2008'
        licenses {
            license {
                name 'The Apache Software License, Version 2.0'
                url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                distribution 'repo'
            }
        }
    }
}

这样我就可以设置属性。

如果您确实需要自定义build ,则由于负责它的插件,您将无法以相同的方式进行声明。 这是您可以执行的操作:

pom {
    project {
        groupId 'com.domain.api'
        artifactId 'gs-gradle'
        version '0.1.0'
        properties {
            project {
                build {
                    sourceEncoding 'UTF-8'
                }
            }
            maven {
                compiler {
                    source '1.8'
                    target '1.8'
                }
            }
        }

        inceptionYear '2008'
        licenses {
            license {
                name 'The Apache Software License, Version 2.0'
                url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                distribution 'repo'
            }
        }
    }
}.withXml {
    asNode().appendNode('build').appendNode('plugins').with {
        appendNode('plugin').with {
            appendNode('groupId', 'org.springframework.boot')
            appendNode('artifactId', 'spring-boot-maven-plugin')
            appendNode('version', "${springBootVersionDef}")
            appendNode('executions').appendNode('execution').appendNode('goals').with {
                appendNode('goal', 'repackage')
            }
        }
        appendNode('plugin').with {
            appendNode('groupId', 'org.apache.maven.plugins')
            appendNode('artifactId', 'maven-jar-plugin')
            appendNode('version', "3.0.2")
            appendNode('configuration').appendNode('archive').appendNode('manifest').with {
                appendNode('addClasspath', "true")
                appendNode('classpathPrefix', "lib/")
                appendNode('mainClass', "com.domain.api.Application")
            }
        }
    }
}.writeTo("pom.xml")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM