【问题标题】:Gradle plugin to generate pom.xmlGradle 插件生成 pom.xml
【发布时间】:2018-03-20 20:42:57
【问题描述】:

我用过gradle plugin to generate pom.xml

按照我添加到的相应文档可能会投射到 build.gradle 文件中的下一个更改:

task writePom {
    doLast {
        pom {
            project {
                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 writePom,结果我在我的项目根目录中生成了pom.xml

问题是我无法使用pom.xml 构建项目,因为:

> maven clean install

[ERROR] /Users/XXX/Documents/projects/workspace/THE_PROJECT/src/main/java/CLASS_PACKAGE/THE_CLASS.java:[45,38] diamond operator is not supported in -source 1.5

(使用 -source 7 或更高版本来启用菱形运算符)

问题原因完全清楚。我的pom.xml 应该使用有效的 java 版本,但其中没有生成 java 版本。

我应该如何配置插件(用于gradle)以生成具有指定java版本的pom.xml

添加更多详细信息

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath group: 'pl.allegro.tech.build', name: 'axion-release-plugin', version: '1.7.1'
        classpath 'org.hidetake:gradle-ssh-plugin:1.1.3'
    }
}

plugins {
    id "nebula.provided-base" version "3.0.3"
}

apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'groovy'
apply plugin: 'osgi'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'jacoco'
apply plugin: 'signing'
apply plugin: 'pl.allegro.tech.build.axion-release'
apply plugin: 'nebula.optional-base'
apply from: 'gradle/dist.gradle'

group = 'org.mnode.ical4j'
description = '''
A Java library for reading and writing iCalendar (*.ics) files
'''

sourceCompatibility = 1.7
targetCompatibility = 1.7
ext {
    slf4jVersion = '1.7.10'
}

repositories {
    mavenCentral()
}

dependencies {
    api "org.slf4j:slf4j-api:$slf4jVersion",
        'commons-codec:commons-codec:1.9',
        'org.apache.commons:commons-lang3:3.3.2',
        'org.apache.commons:commons-collections4:4.0',
        'org.threeten:threetenbp:1.3.3'

    compile 'org.codehaus.groovy:groovy-all:2.3.2', optional

    compileOnly 'biz.aQute.bnd:bndlib:2.3.0'

    testImplementation 'org.codehaus.groovy:groovy-all:2.3.2',
        'org.spockframework:spock-core:0.7-groovy-2.0',
        'commons-io:commons-io:2.4',
        'org.ccil.cowan.tagsoup:tagsoup:1.2.1',
        "org.slf4j:slf4j-log4j12:$slf4jVersion"
}

jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled false
    }
}

javadoc {
    if (JavaVersion.current().isJava8Compatible()) {
        options.addStringOption('Xdoclint:none', '-quiet')
    }
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from 'build/docs/javadoc'
}

task sourcesJar(type: Jar) {
    from sourceSets.main.allSource
    classifier = 'sources'
}

jar {
    manifest {
        instruction 'Import-Package', 'groovy.*;resolution:=optional, org.codehaus.groovy*;resolution:=optional, *'
    }
}

artifacts {
    archives jar
    archives javadocJar
    archives sourcesJar
}

signing {
    required { isReleaseVersion && gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

scmVersion {
    tag {
        prefix = 'ical4j'
    }
    versionCreator 'versionWithBranch'
    branchVersionCreator = [
        'master': 'simple'
    ]
}
version = scmVersion.version

ext {
    isReleaseVersion = !version.endsWith("SNAPSHOT")
}

task writePom {
    doLast {
        pom {
            project {
                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")
    }
}

【问题讨论】:

    标签: java maven gradle build


    【解决方案1】:

    您可以使用 target 命令指定 Java 版本:

    targetCompatibility = '1.7'
    

    试试这个代码:

    apply plugin: 'java'
    compileJava {
      sourceCompatibility = '1.7'
    }
    

    欲了解更多信息,请访问此帖子:

    how specify the required java version in a gradle build?

    【讨论】:

    • 我在您的提议中看到的只是属性,而不是命令。你检查过吗?你能提供命令吗? (我的 gradle 构建文件有这个属性,这是主要原因,为什么 gradle 构建没有失败)
    • @Sergii 我编辑了我的帖子,尝试使用 compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 }
    • 感谢您的回答和 cmets,检查结果...compileJava - pom 生成没有更改。如果我在gradle 阶段使用compileOptions 构建失败。正在尝试获取更多信息并正确设置属性...
    • @Sergii 你能把你的 build.gradle 文件的代码放上去吗?谢谢
    • compileOptions 没有 java 版本的属性,但我正在寻找如何在 4.2.1 版本中设置它
    猜你喜欢
    • 2014-07-27
    • 2019-10-13
    • 2012-04-24
    • 2014-10-29
    • 2013-06-06
    • 2018-12-18
    • 2020-12-01
    • 2013-07-08
    • 2013-06-01
    相关资源
    最近更新 更多