Gradle:构建文件不应包含package语句

时间:2013-05-13 15:29:14

标签: gradle build.gradle gradlew

我有一个gradle构建项目,它包含一个buildSrc / build.gradle和一个项目级的build.gradle。 buildSrc / build.gradle编译一个jar依赖项,该依赖项包含项目级build.gradle所需的类。但是,当项目级build.gradle尝试实例化buildSrc / build.gradle引入的类的实例时,构建将失败。错误是:

  

无法打开构建文件的no_buildscript类缓存〜/ git / project / build.gradle - 构建文件〜/ git / project / build.gradle不应包含包语句。

特别是,项目级构建需要在buildSrc / build.gradle引入的bacon-test-utilities中的DIGSauceLabsUpdater类。

我尝试将bacon-test-utilities软件包导入到项目级构建中,并从“def dashUpdater”行中的类中删除软件包名称。这给了我相同的构建错误。

任何人都可以帮助我了解在项目级构建中访问buildSrc / gradle.build所引入的类需要做些什么吗?

谢谢,

-erzsebet

这是我的buildSrc / build.gradle文件:

// vim:ft=groovy
apply plugin: 'base'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'groovy'


// automated test dependencies
// ---------------------------
dependencies {
def saucerestVersion = "1.0"

compile gradleApi()

// sauce lab's REST client
compile "com.saucelabs.saucerest:saucelabs-saucerest:$saucerestVersion"

// test utilities 
// Note: Needed here for DIGSauceLabsUpdater use in main build.gradle
compile "com.drillinginfo.global:bacon-test-utilities:0.+"
}

// artifacts and maven stuff
// -------------------------
repositories {

    maven {
    url "${project.mavenPublicUrl}/"
}

}

以下是我的项目级build.gradle的相关位:

// vim:ft=groovy
apply plugin: 'base'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'groovy'

// browser driver extensions for acceptance test tasks
ext.drivers = ["firefox", "chrome", "ie"]


// automated test dependencies
// ---------------------------
dependencies {
    groovy "org.codehaus.groovy:groovy-all:1.8.6"

    def gebVersion = "0.7.2"
    def seleniumVersion = "2.31.0"
    def lazerycodeVersion = "1.1"
    def saucerestVersion = "1.0"

    // spock
    testCompile "org.codehaus.geb:geb-spock:$gebVersion"
    testCompile "org.spockframework:spock-core:0.6-groovy-1.8"

    // gradle plugin for test listener code
    compile files('buildSrc/src/main/lib/gradle-plugins-1.2.jar')

    // test utilities
    testCompile "com.drillinginfo.global:bacon-test-utilities:0.+"

    // Drivers
    drivers.each { driver ->
        testCompile "org.seleniumhq.selenium:selenium-$driver-driver:$seleniumVersion"
    }
}

// automated acceptance tasks
// --------------------------
// define a TestListener implementation to report test results to Sauce Labs
// THE NEXT LINE IS WHERE THE BUILD CHOKES
def dashUpdater = new com.drillinginfo.global.test.utils.DIGSauceLabsUpdater()

// ensure the tests have access to properties passed in on the command line (-D) 
tasks.withType(Test) {

    gradle.addListener(dashUpdater)
}

以下是我的./gradlew运行任务的堆栈跟踪输出:

FAILURE: Build failed with an exception.

* What went wrong:
Could not open no_buildscript class cache for build file '/home/account/git/project/build.gradle' (/home/account/.gradle/caches/1.2/scripts/build_7hf4r97619snd7in1srbtn16n6/ProjectScript/no_buildscript).
Build file '/home/account/git/project/build.gradle' should not contain a package statement.

* Try:
Run with --info or --debug option to get more log output.

* Exception is:
org.gradle.cache.CacheOpenException: Could not open no_buildscript class cache for build file '/home/ecarmean/git/bacon/build.gradle' (/home/account/.gradle/caches/1.2/scripts/build_7hf4r97619snd7in1srbtn16n6/ProjectScript/no_buildscript).
    at org.gradle.cache.internal.DefaultPersistentDirectoryStore.open(DefaultPersistentDirectoryStore.java:54)
    at org.gradle.cache.internal.DefaultPersistentDirectoryStore.open(DefaultPersistentDirectoryStore.java:26)
    at org.gradle.cache.internal.DefaultCacheFactory$CacheFactoryImpl.doOpenDir(DefaultCacheFactory.java:71)
    at org.gradle.cache.internal.DefaultCacheFactory$CacheFactoryImpl.open(DefaultCacheFactory.java:110)
    at org.gradle.cache.internal.DefaultCacheRepository$PersistentCacheBuilder.doOpen(DefaultCacheRepository.java:183)
    at org.gradle.cache.internal.DefaultCacheRepository$PersistentCacheBuilder.doOpen(DefaultCacheRepository.java:133)
    at org.gradle.cache.internal.DefaultCacheRepository$AbstractCacheBuilder.open(DefaultCacheRepository.java:120)
    at org.gradle.groovy.scripts.internal.FileCacheBackedScriptClassCompiler.compile(FileCacheBackedScriptClassCompiler.java:51)
    at org.gradle.groovy.scripts.internal.ShortCircuitEmptyScriptCompiler.compile(ShortCircuitEmptyScriptCompiler.java:35)
    at org.gradle.groovy.scripts.internal.CachingScriptClassCompiler.compile(CachingScriptClassCompiler.java:36)
    at org.gradle.groovy.scripts.DefaultScriptCompilerFactory$ScriptCompilerImpl.compile(DefaultScriptCompilerFactory.java:60)
    at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:121)
    at org.gradle.configuration.BuildScriptProcessor.evaluate(BuildScriptProcessor.java:38)
    at org.gradle.configuration.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:43)
    at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:463)
    at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:75)
    at org.gradle.configuration.ProjectEvaluationConfigurer.execute(ProjectEvaluationConfigurer.java:23)
    at org.gradle.configuration.ProjectEvaluationConfigurer.execute(ProjectEvaluationConfigurer.java:21)
    at org.gradle.configuration.DefaultBuildConfigurer$1.execute(DefaultBuildConfigurer.java:38)
    at org.gradle.configuration.DefaultBuildConfigurer$1.execute(DefaultBuildConfigurer.java:35)
    at org.gradle.api.internal.project.AbstractProject.configure(AbstractProject.java:439)
    at org.gradle.api.internal.project.AbstractProject.allprojects(AbstractProject.java:434)
    at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:35)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:142)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:113)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:81)
    at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:38)
    at org.gradle.launcher.exec.InProcessGradleLauncherActionExecuter.execute(InProcessGradleLauncherActionExecuter.java:39)
    at org.gradle.launcher.exec.InProcessGradleLauncherActionExecuter.execute(InProcessGradleLauncherActionExecuter.java:25)
    at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50)
    at org.gradle.launcher.cli.ActionAdapter.execute(ActionAdapter.java:30)
    at org.gradle.launcher.cli.ActionAdapter.execute(ActionAdapter.java:22)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:200)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:173)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:138)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
    at org.gradle.launcher.Main.doAction(Main.java:48)
    at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
    at org.gradle.launcher.Main.main(Main.java:39)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:50)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:32)
    at org.gradle.launcher.GradleMain.main(GradleMain.java:26)
    at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:33)
    at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:130)
    at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:47)
Caused by: java.lang.UnsupportedOperationException: Build file '/home/account/git/project/build.gradle' should not contain a package statement.
    at org.gradle.groovy.scripts.internal.DefaultScriptCompilationHandler.compileScript(DefaultScriptCompilationHandler.java:126)
    at org.gradle.groovy.scripts.internal.DefaultScriptCompilationHandler.compileToDir(DefaultScriptCompilationHandler.java:67)
    at org.gradle.groovy.scripts.internal.FileCacheBackedScriptClassCompiler$CacheInitializer.execute(FileCacheBackedScriptClassCompiler.java:80)
    at org.gradle.groovy.scripts.internal.FileCacheBackedScriptClassCompiler$CacheInitializer.execute(FileCacheBackedScriptClassCompiler.java:65)
    at org.gradle.cache.internal.DefaultPersistentDirectoryCache.buildCacheDir(DefaultPersistentDirectoryCache.java:100)
    at org.gradle.cache.internal.DefaultPersistentDirectoryCache.access$100(DefaultPersistentDirectoryCache.java:36)
    at org.gradle.cache.internal.DefaultPersistentDirectoryCache$1$1.run(DefaultPersistentDirectoryCache.java:82)
    at org.gradle.cache.internal.DefaultFileLockManager$DefaultFileLock.doWriteAction(DefaultFileLockManager.java:162)
    at org.gradle.cache.internal.DefaultFileLockManager$DefaultFileLock.writeFile(DefaultFileLockManager.java:151)
    at org.gradle.cache.internal.DefaultPersistentDirectoryCache$1.execute(DefaultPersistentDirectoryCache.java:80)
    at org.gradle.cache.internal.DefaultPersistentDirectoryCache$1.execute(DefaultPersistentDirectoryCache.java:70)
    at org.gradle.cache.internal.DefaultPersistentDirectoryStore.withExclusiveLock(DefaultPersistentDirectoryStore.java:73)
    at org.gradle.cache.internal.DefaultPersistentDirectoryCache.init(DefaultPersistentDirectoryCache.java:70)
    at org.gradle.cache.internal.DefaultPersistentDirectoryStore.open(DefaultPersistentDirectoryStore.java:46)
    ... 46 more

3 个答案:

答案 0 :(得分:1)

一位同事和我分离了导致我的gradle构建失败的问题,“build.gradle不应该包含package语句”错误。包含依赖项的jar文件同时包含.class和.groovy文件。当我们重新编译jar以仅包含.class文件时,gradle构建运行没有错误,并且能够从(麻烦!)依赖项中实例化一个类。

我还通过删除buildSrc / build.gradle简化了我的构建结构。它不再需要,因为该目录中没有构建源。为了引入项目级build.gradle中所需的(麻烦!)依赖项,我使用了一个buildScript块。

感谢Peter Niederwieser阅读并回答这个问题!

以下是最终构建脚本的相关位:

// vim:ft=groovy

import com.drillinginfo.global.test.utils.DIGSauceLabsUpdater

apply plugin: 'base'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'groovy'

// automated test dependencies
// ---------------------------
dependencies {

    groovy "org.codehaus.groovy:groovy-all:1.8.6"

    def gebVersion = "0.7.2"
    def seleniumVersion = "2.31.0"
    def lazerycodeVersion = "1.1"
    def saucerestVersion = "1.0"

    // spock
    testCompile "org.codehaus.geb:geb-spock:$gebVersion"
    testCompile "org.spockframework:spock-core:0.6-groovy-1.8"

    // test utilities
    testCompile "com.drillinginfo.global:bacon-test-utilities:0.+"

    // Drivers
    drivers.each { driver ->
        testCompile "org.seleniumhq.selenium:selenium-$driver-driver:$seleniumVersion"
    }
}

buildscript {
    repositories {
        maven {
            url "${project.mavenPublicUrl}/"
        }
    }
    dependencies {
        classpath group: 'com.drillinginfo.global', name: 'bacon-test-utilities', version: '0.0.1+17'
    }
}

/ automated acceptance tasks
// --------------------------
// define a TestListener implementation to report test results to Sauce Labs
def dashUpdater = new DIGSauceLabsUpdater()

// ensure the tests have access to properties passed in on the command line (-D) 
tasks.withType(Test) {

    gradle.addListener(dashUpdater)
}

答案 1 :(得分:0)

我在build.gradle中遇到了以下问题:

buildscript {
  dependencies {
    classpath fileTree(
            dir: "some/directory",
            include: '**/*.jar')
  }
}

some/directory有源jar,所以解决方法是:

buildscript {
  dependencies {
    classpath fileTree(
            dir: "some/directory",
            include: '**/*.jar',
            exclude: '**/-sources.jar')
  }
}

答案 2 :(得分:0)

如果遇到此错误:

Build file '...\build.gradle' should not contain a package statement.

您应该检查上述路径,并删除build.gradle文件第一行中的package语句。希望有帮助。