Gradle使用单个build.gradle构建多个项目

时间:2014-09-24 09:26:17

标签: java android gradle

我有一种情况,我在个人项目PROJECT_A和PROJECT_B在一个公共目录下说项目。我的项目文件夹看起来像

Project

 - PROJECT_A 
 - PROJECT_B

现在我可以使用一个build.gradle文件来构建这两个项目。

注意:我不想为PROJECT_A和PROJECT_B提供单独的build.gradle文件,但gradle文件可以有不同的任务来构建每个项目

2 个答案:

答案 0 :(得分:2)

多项目树 - 水(主项目),(子项目)bluewhale&磷虾项目。

构建布局

water/
   build.gradle
   settings.gradle
   bluewhale/
   krill/

settings.gradle

   include 'bluewhale', 'krill'

现在我们重写水构建脚本并将其简化为一行。

有关gradle多个项目构建的更多详细信息,请使用此链接作为参考here

答案 1 :(得分:1)

这是一个示例,说明如何使用单个build.gradle拥有多个子项目。

项目结构具有两个子项目foobar,并且每个子项目只有一个Java类Foo.javaBar.java。否则,该目录仅包含默认的gradle目录和脚本:

├── bar
│   └── src
│       └── main
│           └── java
│               └── org
│                   └── example
│                       └── Bar.java
├── build.gradle
├── foo
│   └── src
│       └── main
│           └── java
│               └── org
│                   └── example
│                       └── Foo.java
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle

单个build.gradle文件如下所示。这些评论应清楚说明正在发生的事情:


group 'org.example'
version '1.0-SNAPSHOT'

// These settings apply to all subprojects but not the root project.
subprojects {
    apply plugin: 'java'
    repositories {
        mavenCentral()
    }
}

// Get a variable for each project.
Project foo = project(':foo')
Project bar = project(':bar')

// Configure the foo project as you would in foo/build.gradle.
// Just using a misc. dependency for example purpose.
configure(foo, {
    dependencies {
        implementation 'software.amazon.awssdk:s3:2.13.71'
    }
})

// Configure the bar project as you would in bar/build.gradle.
// Just making bar depend on foo for example purpose.
configure(bar, {
    dependencies {
        compile foo
    }
})

Foo.javaBar.java包含:

package org.example;

public class Foo {
    public Foo() {
        System.out.println("Hi, I'm Foo");
    }
}
package org.example;

public class Bar {
    public Bar() {
        System.out.println("Hi, I'm Bar");
        new Foo();
    }
}

然后您可以编译整个项目:

$ ./gradlew compileJava

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 466ms
3 actionable tasks: 3 executed