简体   繁体   中英

how gradle add code and resources from another sub-project to the sub-project

i have a minecraft fabric mod project

this is its struct

airgame-api-parent:
    airgame-api-common:
    airgame-api-client:
    airgame-api-server:
    airgame-api-all:

Initially, i build them as a single project, but with the increase of code and function, i added some another depend into my project. such as mysql-connector and HikariCP .
its only needed in server side because the client does not need to connect my sql.
but mysql-connector is too big. it caused my jar file size increase to 4MB+ from 100KB+.
I think it's unbearable.

So I disassembled my project.

the project airgame-api-common is universal environment code: it can be running with client and server.

the project airgame-api-client is client side only code. it just can be running with client. it depend with api-common .

the project airgame-api-server is server side only code. it just can be running with server. it depend with api-common too.

the api-server include some server-side code. example as mysql-connector and HikariCP .

and finally, the api-all include all code of api-common , api-client and api-server . In this way, I don't need to import api-client and api-server at the same time when coding other projects. (Actually, I can't do that because api-client and api-server used the same mod_id . If I import them, when I execute the test, the running environment will contain both dependencies, and then crash due to mod_id conflict.)

okay, first i try to use api project(":airgame-api-common") in the api-client , but it now work, other project that depend api-client still can not see api-common . i guess may plugin fabric-loom changed gradle's build or depend logic.

the fabric-loom docs say that i need use modApi , i tried, but it look like cant be use to import self sub-project.

OK, I'm sorry to say a lot of things that have nothing to do with the problem, but I just want to show that I've done my best to solve the problem.

So now I guess there's one way left: add classpath and resources from api-common to other projects before gradle starts compiling code. I think modifying build.gradle can do it, but I don't know what to do.

I tried to read gradle's documentation, but I really didn't know much about the software, so I couldn't find much useful information. Can someone tell me?

I need the api-client compile file have both its own code and api-common code, and the api-common code needs to be visible to the projects that depend on the api-client .(This is also required for api-server and api-all . But I think if you teach me to configure api-client , I should be able to configure others.)

Finally, my English is not very good, but I try my best to express my intention. I don't mean any harm to anyone. If I offend you, please forgive me.

OK, I found the answer in another answer: Gradle: common resource dependency for multiple java projects

its

sourceSets.main {
    java.srcDirs += [
        project(':api-common').sourceSets.main.java,
        project(':api-common-server').sourceSets.main.java
    ]
    resources.srcDirs += [
        project(':api-common').sourceSets.main.resources,
        project(':api-common-server').sourceSets.main.resources
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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