【问题标题】:Gradle build on multiple projects using single build.gradleGradle 使用单个 build.gradle 构建多个项目
【发布时间】:2014-11-18 17:35:00
【问题描述】:

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

Project

 - PROJECT_A 
 - PROJECT_B

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

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

【问题讨论】:

    标签: java android gradle


    【解决方案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 文件如下所示。 cmets 应该清楚发生了什么:

    
    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
    

    【讨论】:

      【解决方案2】:

      多项目树-水(主项目),(子项目)蓝鲸和磷虾项目。

      构建布局

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

      settings.gradle

         include 'bluewhale', 'krill'
      

      现在我们重写 water build 脚本并将其简化为一行。

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

      【讨论】:

      • 'bluewhale' 和 'krill' 需要 build.gradle 吗?
      • 投了反对票,因为这没有解释您如何处理每个子项目的依赖关系和其他配置。我也不认为官方文档在这方面做得很好。经过反复试验,我才弄明白。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 2018-03-18
      • 2011-10-30
      • 1970-01-01
      • 2012-08-22
      相关资源
      最近更新 更多