简体   繁体   English

使用 gradle 将子项目 A 中的类包含在子项目 B 的类路径中

[英]Use gradle to include classes from sub-project A in the classpath for sub-project B

I'm very new to gradle and was having problems in a more complicated project which provides context to this overall question, so I decided to create a new, simple project to try to get the concept to work, but I still can't get it to work.我对 gradle 很陌生,并且在一个更复杂的项目中遇到了问题,该项目为这个整体问题提供了上下文,所以我决定创建一个新的、简单的项目来尝试让这个概念发挥作用,但我仍然无法得到它工作。

I have one sub-project called core and one sub-project called db .我有一个名为core的子项目和一个名为db的子项目。 I have a class in the db project called com.kenny.db.DBMain which has a main() function that I want to run.我在db项目中有一个名为com.kenny.db.DBMain的类,它有一个我想运行的main()函数。 DBMain.main() needs to use com.kenny.core.* classes that are defined in the core project. DBMain.main()需要使用core项目中定义的com.kenny.core.*类。

I'm able to add a dependency in my db/build.gradle so that it depends on :core , but my code in db isn't able to see the classes from core .我可以在我的db/build.gradle中添加一个依赖项,以便它依赖于:core ,但是我在db中的代码无法看到core中的类。

db/build.gradle:数据库/build.gradle:

plugins {
    id 'java'
}

dependencies {
    project(":core")
}

db/src/main/java/com/kenny/db/DBMain.java: db/src/main/java/com/kenny/db/DBMain.java:

package com.kenny.main;

import com.kenny.core.*;

public class DBMain {

    public static void main(String[] args) {
        CoreStuff stuff = new CoreStuff();
        System.out.println("db test, name="+stuff.name);
    }
}

When I try to build DBMain.java, it fails because it can't find com.kenny.core.CoreStuff() which was defined in the core project.当我尝试构建 DBMain.java 时,它失败了,因为它找不到在core项目中定义的com.kenny.core.CoreStuff()

/Users/kenny/projects/gradlewtf/db/src/main/java/com/kenny/db/DBMain.java:3: error: cannot find symbol
import com.kenny.core;
                ^
  symbol:   class core
  location: package com.kenny

How do I get the classes from core available when compiling code in the db project?db项目中编译代码时,如何从可用的core获取类?

-- Edit: Changed the core project code package to com.kenny.core and the db project code package to com.kenny.db . -- 编辑:将core项目代码包更改为com.kenny.core ,将db项目代码包更改为com.kenny.db Still the same problem, the db project isn't getting the core code into the classpath so it cannot compile.仍然是同样的问题, db项目没有将core代码放入类路径,因此无法编译。

I finally got it to work.我终于让它工作了。 I tried following the documentation on the gradle website itself, but at every turn, their example code failed to even compile.我尝试按照gradle 网站本身的文档进行操作,但每一次,他们的示例代码都无法编译。 That's because you need to already understand gradle in order to realize that you need to change their example code, but they don't really tell how/what you need to change.那是因为您需要已经了解 gradle 才能意识到您需要更改他们的示例代码,但他们并没有真正告诉您如何/需要更改什么。 I mean, they try to tell you inside a paragraph later on, but unless you are already a gradle expert, those instructions don't make much sense.我的意思是,他们稍后会尝试在一段中告诉您,但除非您已经是 gradle 专家,否则这些说明没有多大意义。

I added this to my core/build.gradle file:我将此添加到我的 core/build.gradle 文件中:

// this will create a configuration with the name "instrumentedJars" 
// that will include the implementation (i.e. my classes and 
// classes from dependencies)
configurations {
    instrumentedJars {
        canBeConsumed = true
        canBeResolved = false
        extendsFrom implementation, runtimeOnly
    }
}

// this will declare an "artifact" (i.e. a produced output) 
// for my :core project. It references the configuration name 
// "instrumentedJars" and says that it should include the 
// outputs of the task named "jar"
artifacts {
    instrumentedJars(jar)
}

Then in my project where I need to include those core classes, I added this to my db/build.gradle file:然后在我需要包含这些core类的项目中,我将其添加到我的 db/build.gradle 文件中:

dependencies {
    implementation(project(path: ":core", configuration: 'instrumentedJars'))
}

Then I can build my db project and it is able to find the classes I need.然后我可以构建我的db项目,它能够找到我需要的类。 Then when I need to run my main() method, I added a run task to my db/build.gradle:然后当我需要运行我的 main() 方法时,我在我的 db/build.gradle 中添加了一个运行任务:

task run(type: JavaExec) {
    dependsOn(":db:compileJava")

    mainClass = 'com.kenny.db.DBMain'
    classpath = sourceSets.main.runtimeClasspath
}

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

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