簡體   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