1

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

无法打开构建文件 ~/git/project/build.gradle 的 no_buildscript 类缓存 - 构建文件 ~/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
4

3 回答 3

1

我和一位同事隔离了导致我的 gradle 构建失败并出现“build.gradle 不应包含包语句”错误的问题。包含依赖项的 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)
}
于 2013-05-15T21:56:30.450 回答
1

如果您遇到此错误:

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

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

于 2020-12-09T12:33:11.767 回答
0

我遇到了以下问题build.gradle

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

some/directory有源罐子,所以修复是:

buildscript {
  dependencies {
    classpath fileTree(
            dir: "some/directory",
            include: '**/*.jar',
            exclude: '**/-sources.jar')
  }
}
于 2015-03-30T17:38:51.993 回答