问题
I have two projects ProjA and ProjB, here ProjB is depends on ProjA. So, how to include ProjA in ProjB? And please let me know in case change in the ProjA build.gradle file?
回答1:
Okay, so you have two projects, whereas ProjA depends on ProjB.
If you have already set up a gradle build for those, both of them will have a file settings.gradle, which at least defines the project name and build.gradle, where you can specify the dependencies.
Example: settings.gradle
rootProject.name = 'ProjA'
build.gradle
group = 'mygroup'
version = '1.0'
By further adding one of Gradle's publishing plugins (see Maven publishing or Ivy publishing), you can configure a publication. A publication in turn contains artifacts, that is the files you want to upload (and usually some repositories). Example for a minimal Maven publication:
apply plugin: 'maven-publish'
publishing {
publications {
core(MavenPublication) {
from components.java
}
}
}
The maven-publish plugin will add the task publishToMavenLocal
(among others) to your project which installs the jar somewhere to ~/.gradle/caches/
.
In build.gradle of ProjB you can then define a compile time dependency on ProjA like so:
dependencies {
compile(group: 'mygroup', name: 'ProjA', version: '1.0')
}
回答2:
If your projects are co-located, you could have a multi build project.
Say you have a root folder with these contents:
- some root directory
- project-a
project-a.gradle
- src
- project-b
project-b.gradle
- src
build.gradle
settings.gradle
- project-a
settings.gradle
contains:
include 'project-a', 'project-b'
project-b.gradle
then depends on project A:
dependencies {
compile project(':project-a')
}
After this, project a will always compile and assemble before project-b and the jar file for project-a jar will be on the classpath when compiling project-b.
来源:https://stackoverflow.com/questions/21305091/how-to-add-other-projects-in-current-project-in-gradle