我使用此build.grafle文件时出错,并且不知道如何修复它

时间:2018-05-16 09:13:55

标签: gradle build.gradle

这是错误:

  

失败:构建因异常而失败。

     
      
  • 其中:构建文件'/home/wieland/GitGradlePackaging/build.gradle'行:22

  •   
  • 出了什么问题:评估根项目'GitGradlePackaging'时出现问题。

         
        

    无法为org.gradle.api.internal.initialization.DefaultScriptHandler类型的对象获取未知属性'org'。

      
  •   

这是我的build.gradle文件:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/4.6/userguide/tutorial_java_projects.html
 */

//From example: http://mrhaki.blogspot.co.at/2015/04/gradle-goodness-use-git-commit-id-in.html



buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        //Add dependencies for build script, so we can access Git from our build script     
        classpath 'org.ajoberstar:grgit:1.1.0'
    }
    def git = org.ajoberstar.grgit.Grgit.open(file('.'))
    //To save Githash
    def githash = git.head().abbreviatedId
}

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    // Apply the application plugin to add support for building an application
    id 'application'

    // Apply the groovy plugin to also add support for Groovy (needed for Spock)
    id 'groovy'

    id 'distribution'
}


// Set version
project.version = mainProjectVersion + " - " + githash

project.ext.set("wholeVersion", "$project.version - $githash")
project.ext.set("buildtimestamp", "2000-01-01 00:00")

def versionfilename = "versioninfo.txt"



def GROUP_DEBUG = 'Debug'
// Task to print project infos
task debugInitialSettings {
    group = GROUP_DEBUG
    doLast {
        println 'Version: ' + project.wholeVersion
        println 'Timestamp: ' + project.buildtimestamp
        println 'Filename: ' + project.name 
    }
}

// To add the githash to zip
task renameZip {
    doLast {
        new File ("$buildDir/distributions/$project.name-${project.version}.zip")
        .renameTo ("$buildDir/distributions/$project.name-${project.wholeVersion}.zip")
    }
}
distZip.finalizedBy renameZip

// To add the githash to tar
task renameTar{
    doLast {
        new File ("$buildDir/distributions/$project.name-${project.version}.tar")
                .renameTo ("$buildDir/distributions/$project.name-${project.wholeVersion}.tar")
    }
}
distTar.finalizedBy renameTar


// Define the main class for the application
mainClassName = 'App'

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
    compile 'com.google.guava:guava:23.0'

    // Use the latest Groovy version for Spock testing
    testCompile 'org.codehaus.groovy:groovy-all:2.4.13'

    // Use the awesome Spock testing and specification framework even with Java
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    testCompile 'junit:junit:4.12'
}

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

//To generate Testreports as HTML
test {
    reports {
        junitXml.enabled = false
        html.enabled = true
    }

}

distributions {
    main {
        contents {
            from { 'build/docs' }
            into ('reports') {
                from 'build/reports'
            }
        }
    }
}




//To make sure that test and javadoc ran before zip and tar
distTar.dependsOn test
distZip.dependsOn test
distTar.dependsOn javadoc
distZip.dependsOn javadoc

请记住,我对gradle知之甚少,因为我刚刚开始学习它! 在此先感谢:)

1 个答案:

答案 0 :(得分:1)

您必须将githash定义移到buildscript

之外
buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        //Add dependencies for build script, so we can access Git from our build script     
        classpath 'org.ajoberstar:grgit:1.1.0'
    }
}

def git = org.ajoberstar.grgit.Grgit.open(file('.'))
//To save Githash
def githash = git.head().abbreviatedId

原因是当逐行评估buildscript块时,其依赖关系尚未加载。在评估脚本的其余部分时,已加载buildscript块的依赖项。这实际上是buildscript块存在的原因:在构建的其余部分之前运行并准备设置。

相关问题