【问题标题】:gradle not using compile time dependencies when buildinggradle在构建时不使用编译时依赖项
【发布时间】:2013-05-28 22:03:41
【问题描述】:

大家好,我是 gradle 新手,遇到以下问题。

当我使用 java 插件并在我的项目上调用 $gradle build 时,它不会将我的第三方依赖项放在类路径上。我的 build.gradle 文件如下所示:

apply plugin: 'java'

sourceSets.main.java.srcDirs = ["src/main/java", "src/main/web"]

repositories {
  flatDir name: 'thirdParty', dirs: 'C:/dev/repo'
}

dependencies {
  compile files('log4j-1.2.12.jar', 'gson-1.7.1.jar')
}

gradle 的错误输出如下

C:\dev\gradling\TestProject\src\main\web\java\org\gradle\example\simple\HelloWorld2.java:3: package com.google.gson does not exist
import com.google.gson.Gson;
                      ^
C:\dev\gradling\TestProject\src\main\web\java\org\gradle\example\simple\HelloWorld2.java:7: cannot find symbol
symbol  : class Gson
location: class org.gradle.example.simple.HelloWorld2
         Gson gson = new Gson();
         ^
C:\dev\gradling\TestProject\src\main\web\java\org\gradle\example\simple\HelloWorld2.java:7: cannot find symbol
symbol  : class Gson
location: class org.gradle.example.simple.HelloWorld2
         Gson gson = new Gson();

我已经说明了我的 repo jar 所在的位置,并告诉它编译时它必须包含上述 jar。

请帮忙。

【问题讨论】:

    标签: build gradle


    【解决方案1】:

    您的依赖声明不正确。您可以或者指定flatDir repo并使用通常的依赖语法来处理外部依赖(group:module:version),或者使用files方法和正确的文件路径(那么您不需要声明存储库)。在前一种情况下,您可以省略依赖项的组。例如:

    repositories {
        flatDir name: 'thirdParty', dirs: 'C:/dev/repo'
    }
    
    dependencies {
        compile ':log4j:1.2.12'
    }
    

    如需更多信息,请咨询Gradle User Guide。

    【讨论】:

    • 谢谢,冒号可以解决问题。有没有办法直接硬编码要包含在上面示例中的 jar 文件?
    • 如我所说,您可以使用files("absolute/or/relative/path/to/the.jar")。例如,如果 Jars 相对于构建的根目录,您可以使用files("$rootDir/some/path")。如果您遵循此方法,则无需声明 flatDir 存储库。
    猜你喜欢
    • 2019-07-10
    • 1970-01-01
    • 2013-01-30
    • 2014-10-07
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    相关资源
    最近更新 更多