简体   繁体   English

如何在Gradle中将根项目添加为类路径

[英]How to add root Project as classpath in Gradle

In JAVA eclipse, the main class of the java project is defined as one of the class in the JAR of the 'lib' folder, and the root project (fooBar) is added as a dependency in the classpath. 在JAVA eclipse中,java项目的主类被定义为'lib'文件夹的JAR中的一个类,并且根项目(fooBar)被添加为类路径中的依赖项。

This can be acheived in eclipse by simply adding the root project in the classpath. 只需在类路径中添加根项目,就可以在eclipse中实现这一点。 However, how do we add the dependency of the root project in gradle? 但是,我们如何在gradle中添加根项目的依赖项?

dependencies {
    compile project(':fooBar');
}


> Project with path ':fooBar' could not be found in root project 'fooBar'.

The following is the project structure: 以下是项目结构:

fooBar
-src/main/java
-src/test/java
-JRE System Library
-gradle
--wrapper
--launch.gradle
-lib
-build.gradle
-settings.gradle
-gradle-apps.settings
-gradle.properties
-gradlew
-gradlew.bat

I guess I got your point. 我想我明白了。 First of all I have a doubt. 首先,我有一个疑问。 Why are you adding 你为什么要加

dependencies {
    compile project(':fooBar');
} 

this entry in build.gradle of fooBar project? 这个条目在fooBar项目的build.gradle中? This can cause cyclic dependency and results in build failure. 这可能导致循环依赖并导致构建失败。 So I guess remove the above entry from build.gradle of fooBar project. 所以我想从fooBar项目的build.gradle中删除上面的条目。

And please add the below code in your settings.gradle file: 请在settings.gradle文件中添加以下代码:

rootProject.name = 'fooBar'

include ':fooBar'

Hope this will work. 希望这会奏效。

As far as the project method is concerned, the root project has no name. 就项目方法而言,根项目没有名称。 So this is the syntax in the subproject's build.gradle: 所以这是子项目的build.gradle中的语法:

dependencies { compile project(':') } 依赖项{compile project(':')}

However, it is rarely a good idea to do this. 但是,这样做很少是个好主意。 It is too easy to end up with circular dependencies. 结束循环依赖很容易。 Most multi-module projects have a separate "main" projects (called something like "main", "core" or "base"), and other modules can easily depend on that using compile project(':core') or whatever. 大多数多模块项目都有一个单独的“主”项目(称为“主”,“核心”或“基础”),其他模块可以很容易地依赖于使用编译项目(':core')或其他任何项目。

(Originally answered over here .) (最初在这里回答。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM