我的 gradle 配置在构建期间未使用正确的类路径

发布于 2024-11-08 03:31:01 字数 1228 浏览 4 评论 0原文

我有一个多项目设置(ProjectB -> ProjectA),并且我使用 flatDir 在每个项目中指定一个 lib 目录。

ProjectA:

repositories {
    flatDir name: 'localRepository', dirs: 'lib'
}

dependencies {
    compile group: 'com.google.guava', name: 'guava', version: 'r08'
    compile group: 'com.miglayout', name: 'miglayout', version: '3.7.4'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    testCompile group: 'org.easymock', name: 'easymock', version: '2.5.2'
}

ProjectB:

repositories {
    flatDir name: 'localRepository', dirs: 'lib'
}

dependencies {
    compile group: 'yan', name: 'yan', version: '5.0.2'
    runtime group: 'yan', name: 'jfunutil', version: '5.0.2'
    compile project(':ProjectA')
    testCompile group: 'junit', name: 'junit', version: '4.+'
    testCompile group: 'org.easymock', name: 'easymock', version: '2.5.2'

}

当我对 ProjectB 使用 gradle dependency 时,会生成正确的依赖项列表,显示 ProjectA 的传递依赖项(例如,包括 guava-r08)。但是,当我gradle build时,用于javac的实际类路径仅包含ProjectB的直接依赖项,以及构建ProjectA生成的jar。

另一个烦恼是,对于 testCompile 来说,我必须重新声明 ProjectB 对 junit 的依赖,否则 gradle dependency 将不会成功。

非常感谢任何指点 - 我是 Gradle 新手。

I have a multi-project setup (ProjectB -> ProjectA), and I'm using flatDir to specify a single lib directory in each project.

ProjectA:

repositories {
    flatDir name: 'localRepository', dirs: 'lib'
}

dependencies {
    compile group: 'com.google.guava', name: 'guava', version: 'r08'
    compile group: 'com.miglayout', name: 'miglayout', version: '3.7.4'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    testCompile group: 'org.easymock', name: 'easymock', version: '2.5.2'
}

ProjectB:

repositories {
    flatDir name: 'localRepository', dirs: 'lib'
}

dependencies {
    compile group: 'yan', name: 'yan', version: '5.0.2'
    runtime group: 'yan', name: 'jfunutil', version: '5.0.2'
    compile project(':ProjectA')
    testCompile group: 'junit', name: 'junit', version: '4.+'
    testCompile group: 'org.easymock', name: 'easymock', version: '2.5.2'

}

When I use gradle dependencies for ProjectB, the correct dependency list is generated, showing transitive dependencies from ProjectA (eg, including guava-r08). However, when I gradle build, the actual classpath used for javac only includes the direct dependencies of ProjectB, and the jar generated by building ProjectA.

Another annoyance is it seems for testCompile, I have to re-declare the dependency on junit for ProjectB otherwise gradle dependencies will not be successful.

Any pointers much appreciated - I am new to Gradle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风启觞 2024-11-15 03:31:02

关于您的项目结构...

拥有一个 lib 文件夹似乎比每个项目都有一个更好的主意。

您的目录结构是这样的:

project/settings.gradle
project/ProjectA/lib
project/ProjectA/src
project/ProjectB/lib
project/ProjectB/src

您想要为每个子项目都有一个 lib 文件夹的任何特殊原因吗?这似乎是一个更好的主意:

project/settings.gradle
project/lib
project/ProjectA/src
project/ProjectB/src

您可以在项目的根目录 (project/build.gradle) 中创建一个包含以下内容的 build.gradle:

subprojects{
   apply plugin: 'java'
   repositories {
        flatDir name: 'localRepository', dirs: "$rootProject.projectDir/lib"
   }
}

这样您就可以将所有依赖项放入 project/lib 中。

关于您的测试依赖项...

您还可以将 testCompile 依赖项放入此根 build.gradle 文件中。就变成了:

subprojects{
   apply plugin: 'java'
   repositories {
        flatDir name: 'localRepository', dirs: "$rootProject.projectDir/lib"
   }
   dependencies{
        testCompile group: 'junit', name: 'junit', version: '4.+'
        testCompile group: 'org.easymock', name: 'easymock', version: '2.5.2'
   }
}

这样你就不必在每个子项目的 build.gradle 文件中指定 testCompile 依赖项。

但是,当我构建 gradle 时,
仅用于 javac 的实际类路径
包括直接依赖项
ProjectB,以及生成的jar
建设项目A。

为了编译ProjectB,您只需要ProjectA。只有 ProjectA 是您的编译依赖项; ProjectA 编译依赖项成为 ProjectB 的传递依赖项。

About your project structure...

It seems like a better idea to have a single lib folder instead of having one for each project.

Your directory structure is like this:

project/settings.gradle
project/ProjectA/lib
project/ProjectA/src
project/ProjectB/lib
project/ProjectB/src

Any particular reason why you want to have a lib folder for each subproject? This seems like a better idea:

project/settings.gradle
project/lib
project/ProjectA/src
project/ProjectB/src

You can create a build.gradle in the root of the project (project/build.gradle) that contains the following:

subprojects{
   apply plugin: 'java'
   repositories {
        flatDir name: 'localRepository', dirs: "$rootProject.projectDir/lib"
   }
}

This way you can drop all your dependencies into project/lib.

About your test depencendies...

You may also place your testCompile dependencies into this root build.gradle file. It becomes:

subprojects{
   apply plugin: 'java'
   repositories {
        flatDir name: 'localRepository', dirs: "$rootProject.projectDir/lib"
   }
   dependencies{
        testCompile group: 'junit', name: 'junit', version: '4.+'
        testCompile group: 'org.easymock', name: 'easymock', version: '2.5.2'
   }
}

This way you do not have to specify the testCompile dependency in each subproject's build.gradle file.

However, when I gradle build, the
actual classpath used for javac only
includes the direct dependencies of
ProjectB, and the jar generated by
building ProjectA.

In order to compile ProjectB, you only need ProjectA. Only ProjectA is your compile dependency; ProjectA compile dependencies become ProjectB's transitive dependencies.

久隐师 2024-11-15 03:31:02

我同意之前的回答。经过多次尝试,我们有了相同的结构

> shared
 - build.gradle
 - gradle.properties
 - settings.gradle
> project-a 
 - gradle.properties
 - settings.gradle
> project-b 
 - gradle.properties
 - settings.gradle

Shared 拥有所有公共代码,在我们的例子中它处理签入、模块部署、代码质量(cobertura)、编译等
Shared 还定义了由子项目继承的类路径,然后子项目可以添加额外的依赖项。

对于您的问题:

您是否在项目 A 中定义了以下内容?

settings.gradle includeFlat('项目 B')

您是否在项目 B 中定义了以下内容?

settings.gradle includeFlat('项目 A')

I agree with the previous answer. After a number of attempts we have the same structure

> shared
 - build.gradle
 - gradle.properties
 - settings.gradle
> project-a 
 - gradle.properties
 - settings.gradle
> project-b 
 - gradle.properties
 - settings.gradle

Shared has all the common code, in our case it handles checkins, module deploy, code quality (cobertura), compile etc
Shared also defines the classpath which is inherited by sub-projects which can then add additional dependencies.

For your problem:

Have you defined the following in your Project A?

settings.gradle includeFlat('Project B')

Have you defined the following in Project B?

settings.gradle includeFlat('Project A')

柠檬色的秋千 2024-11-15 03:31:02

您原始帖子中的构建脚本很好。传递依赖解析不起作用的原因是项目只会使用自己的存储库来解析其配置。因此,解决问题的一种方法是只有一个 lib 目录。另一个解决方案是为 B 声明第二个存储库,该存储库指向 A 的 lib 目录。

另一个烦恼是对于 testCompile 来说,我必须为 ProjectB 重新声明对 junit 的依赖,否则 gradle 依赖将不会成功。

不确定你在这里说什么,但这可能是我上面解释的结果。也许以下信息也有帮助:依赖项目不会引入其 testCompile/testRuntime 依赖项。预计您必须为每个需要 JUnit 的项目声明它。为了避免重复,您可以使用配置注入来声明您的项目。之前的答案已经给出了具体的例子(例如subprojects { ... })。

The build scripts in your original post are fine. The reason why transitive dependency resolution doesn't work is that a project will only ever use its own repositories for resolving its configurations. Hence, one way to overcome your problem is to have just one lib directory. Another solution is to declare a second repository for B that points to A's lib directory.

Another annoyance is it seems for testCompile, I have to re-declare the dependency on junit for ProjectB otherwise gradle dependencies will not be successful.

Not sure what you are saying here, but it might be a consequence of what I explained above. Maybe the following information also helps: Depending on a project doesn't pull in its testCompile/testRuntime dependencies. It's expected that you have to declare JUnit for each project that needs it. To avoid repetition, you can use configuration injection to declare the commonalities between your projects. Previous answers already gave concrete examples for this (e.g. subprojects { ... }).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文