gpt4 book ai didi

java - Gradle 子项目不包含在类路径中

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:15:34 24 4
gpt4 key购买 nike

我们设置了 2 个项目,1 个主项目和 1 个子项目,它们是 Java 项目。它们都在同一个目录下。

目录结构如下:

 ./dev
./Project_A
build.gradle
settings.gradle
./Project_B
build.gradle

Project_A 包含 Project_B。

Project_A settings.gradle 看起来像:

includeFlat 'Project_B'

Project_A build.gradle 包含:

compile project(':Project_B')   

问题从命令行 (gradlew clean build) 编译时,Project_A 遗漏了 Project_B 中的类。看起来 Project_B 不属于 Project-A 的类路径。

这是在 Project_A 目录中运行的 gradlew clean build 的(输出的一部分)(之后是所有“package project_b.x.y missing”和“cannot find symbol”(来自 Project_B) :

:clean
:Project_B:clean
:Project_B:compileJava
:Project_B:processResources
:Project_B:classes
:Project_B:jar UP-TO-DATE
:compileJava
...Starts erroring out here...

我猜这是一个类路径问题,但我不知道如何解决它。

提前致谢,JM

PS:编辑问题,因为我能够通过 2 个项目构建(最初是 3 个)重现该问题

最佳答案

基于Java Quckstart: Multi-project Java build :

1) 您的 settings.gradle 文件需要在 dev/ 中,而不是 dev/project_A 并且它应该包含如下内容:

include 'Project_A', 'Project_B'

2) 那么你的dev/Project_A/build.gradle 文件应该包含

dependencies {
compile project(':Project_B')
}

编辑:

我已经按照您在问题中描述的项目布局创建了一个玩具示例。但是,我无法重现该问题。也许您将能够发现导致特定错误的一些差异:

目录树

├── Project_A│   ├── build.gradle│   ├── settings.gradle│   └── src│       └── main│           └── java│               └── a│                   └── A.java└── Project_B    ├── build.gradle    └── src        └── main            └── java                └── b                    └── B.java

Project_A/build.gradle

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'a.A'

dependencies {
compile project(':Project_B')
}

Project_A/settings.gradle

includeFlat 'Project_B'

A.java

package a;
import b.B;

public class A {
public static void main(String[] args) {
System.out.println("From A.main...");
B.call();
}
}

Project_B/build.gradle

apply plugin: 'java'

B.java

package b;

public class B {
public static void call() {
System.out.println("Calling B");
}
}

Project_A 运行 Gradle 时,输出为:

$ gradle clean build run:clean UP-TO-DATE:Project_B:clean UP-TO-DATE:Project_B:compileJava:Project_B:processResources UP-TO-DATE:Project_B:classes:Project_B:jar:compileJava:processResources UP-TO-DATE:classes:jar:assemble:compileTestJava UP-TO-DATE:processTestResources UP-TO-DATE:testClasses UP-TO-DATE:test UP-TO-DATE:check UP-TO-DATE:build:Project_B:assemble:Project_B:compileTestJava UP-TO-DATE:Project_B:processTestResources UP-TO-DATE:Project_B:testClasses UP-TO-DATE:Project_B:test UP-TO-DATE:Project_B:check UP-TO-DATE:Project_B:build:runFrom A.main...Calling BBUILD SUCCESSFUL

关于java - Gradle 子项目不包含在类路径中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23371633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com