首页 > 解决方案 > 如何解决“无法获取未知属性'sourceJar”

问题描述

我使用 Gradle 在 Jenkins 中构建项目。但是在执行作业时,它会输出“Could not get unknown property 'sourceJar' for object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication”错误。我对这个错误一无所知。

错误:

+ gradle projects
Starting a Gradle Daemon (subsequent builds will be faster)

FAILURE: Build failed with an exception.

* Where:
Build file '/home/jenkins/workspace/QWASP/RELEASE_BUILD_COMMON_QA/processing-common/build.gradle' line: 34

* What went wrong:
A problem occurred evaluating root project 'processing-common'.
> Could not get unknown property 'sourceJar' for object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication.

脚本:(这是一个 bash 脚本;Gradle 版本 5.0)

PATH=$PATH:/opt/gradle/gradle-5.0/bin/gradle

git clone -b $Git_Branch git@git.zone24x7.lk:QWASP/processing-common.git

cd processing-common/
echo "`git tag`"

gradle projects 

#./gradlew clean build tag publish -Prelease -P${release_type} -PmavenSettingsLocation='/opt/.m2/qv2.settings.xml' -PnexusUrl='https://nexus.zone24x7.lk/repository/releases'

if [ ! -z "${bump_component}" -a "${bump_component}" != "none" ]; then
    ./gradlew clean build tag publish -Prelease -PbumpComponent=${bump_component} -P${release_type} -PmavenSettingsLocation='/opt/.m2/qv2.settings.xml' -PnexusUrl='https://nexus.zone24x7.lk/repository/releases' -x test
else
    ./gradlew clean build tag publish -Prelease -P${release_type} -PmavenSettingsLocation='/opt/.m2/qv2.settings.xml' -PnexusUrl='https://nexus.zone24x7.lk/repository/releases' -x test
fi

echo "`git tag`"

git push --tags

cd ..

build.gradle 文件(错误说问题在第 38 行,即“artifact sourceJar”行)

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        classpath 'net.linguica.gradle:maven-settings-plugin:0.5'
    }
}

plugins {
    id "org.sonarqube" version "2.7.1"
}

allprojects {
    group 'com.zone24x7.qwasp.common'

    apply plugin: 'java'
    apply plugin: 'jacoco'
    apply plugin: 'idea'
    apply plugin: 'java-library'
    apply plugin: 'maven-publish'
    apply plugin: 'net.linguica.maven-settings'
    sourceCompatibility = 1.8

    repositories {
        mavenCentral()
    }

    version = rootProject.version

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
                artifact sourceJar
                artifact packageJavadoc
            }
        }
        repositories {
            maven {
                // change to point to your repo, e.g. http://my.org/repo
                name = 'nexus'
                url project.hasProperty('nexusUrl') ? project.property('nexusUrl') : 'https://nexus.zone24x7.lk/repository/snapshots'

            }
        }
    }

    mavenSettings {
        userSettingsFileName = project.hasProperty('mavenSettingsLocation') ? project.property('mavenSettingsLocation') : '/opt/.m2/qv2.settings.xml'
    }

    javadoc {
        source = sourceSets.main.allJava
        classpath = configurations.compileClasspath

        options {
            setMemberLevel JavadocMemberLevel.PUBLIC
            setAuthor true

            links "https://docs.oracle.com/javase/8/docs/api/"
        }
    }

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

    task packageJavadoc(type: Jar) {
        from javadoc
        classifier = 'javadoc'
    }

    jacocoTestReport {
        reports {
            xml.enabled true
        }
    }

    plugins.withType(JacocoPlugin) {
        tasks["test"].finalizedBy 'jacocoTestReport'
    }
}

标签: jenkinsgradlebuild.gradlejenkins-groovycicd

解决方案


推荐阅读