java - 使用gradle maven插件时如何设置java目标和源版本?

标签 java maven gradle build.gradle maven-plugin

我正在使用 gradle 3.5 和 gradle 的 Maven 插件。

我有一个生成 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'
            }
        }
    }
}

这样我就可以设置属性。

如果您确实需要自定义构建,则由于负责它的插件,您将无法以相同的方式声明它。您可以这样做:

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

关于java - 使用gradle maven插件时如何设置java目标和源版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337733/

相关文章:

java - 如何隐藏URL中的值

java - 错误 "Mapping (RESOURCE) not found : HibernateExposed/Person.hbm.xml"

java - heroku - system.properties 和 java 版本

java - Gradle-从Jar文件中忽略特定的jar

java - 为什么我们应该使用接口(interface)而不是具体类型?

java - 用于获取 success.jsp 页面上的值的 ModelDriven 接口(interface)

java - Pom 文件中的这段代码是如何实现的呢?

android - React Native 0.61 版本中的 Google Play 64 位要求

android - dependency commons-logging :commons-logging:1. 2 被调试忽略,因为它可能与 Android 提供的内部版本冲突

Java:将数组转换为列表异常