2

I currently have 3 subprojects in my Gradle project, structured like so:

Main Project
| 
-- Project A
-- Project B
-- Common Src

Project A has dependency on Lib A and Common Src, and Project B has dependency on Lib B and Common Src. Lib A and Lib B contain different implementations of the same classes.

My plan for Common Src project was to house all of the utility classes, necessarily depending on both Lib A and Lib B.

Is there a way to set this up, and how should my gradle project look like?

Things that I've tried:

  1. I've tried to remove Common Src as a subproject and just include it in the sourceSets of Project A and Project B. However, IntelliJ seems to mark Common Src as sources root of Project A, and even though build-through-gradle works, I cannot get IntelliJ to recognize that Project B also has sources root in Common Src.
  2. Add both Lib A and Lib B to dependency of Common Src, but obviously that doesn't work.

The background:

I've created a Minecraft mod compatible with both Forge (Project A) and Fabric (Project B) modloaders. I have utility classes in both projects that have the same source code but need to be compiled twice, each time with a different dependency. I don't want to have to write the code twice each time I want to change something in the Utility classes.

4

1 回答 1

0

编辑:我找到了一种解决方法:

  1. Project AProject B中,在它们的每一个中添加一个任务,build.gradle以将源文件从复制Common Src到每个项目的构建目录。
  2. 配置sourceSets两者Project AProject B包含我们刚刚复制到的构建文件夹中的源文件
  3. 配置Common Src为 gradle 子项目,并将Lib A(或Lib B)添加到依赖项Common Src(只是为了让 IntelliJ 获得一些上下文)
于 2021-12-15T04:07:08.643 回答