Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define different dependencies for subprojects in a multi-projects Gradle ? (Java)

I have a distributes projects with different sub-projects and I want to accomplish the following:

  • (root)
    • client
    • module A
    • module B
    • module C
    • model

I want to put

     protoc {
         artifact = 'com.google.protobuf:protoc:3.5.0'
     }
     plugins {
         grpc {
             artifact = "io.grpc:protoc-gen-grpc-java:1.7.0"
         }
     }
     generateProtoTasks {
         all()*.plugins {
             grpc {}
         }
     } }
    dependencies {
       compile "com.google.api.grpc:proto-google-common-protos:1.0.0"
       compile "io.grpc:grpc-netty:1.7.0"
       compile "io.grpc:grpc-protobuf:1.7.0"
       compile "io.grpc:grpc-stub:1.7.0"
   }

for module A, B and C.

For now I have the following in my root build.gradle

subprojects{
    apply plugin: 'java'

    sourceCompatibility = 1.8
    group 'project'
    version '0.0.1-SNAPSHOT'

    jar {
        manifest {
            attributes 'Main-Class': "com.project.${project.name}.App"
        }
        doFirst {
            from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
        }
    }

    repositories {
        mavenCentral()
        mavenLocal()
    }

    dependencies {
        testCompile 'junit:junit:4.12'
        testCompile 'org.mockito:mockito-core:1.9.5'
    }
}

So every sub-project use java plugin, and has the defines dependencies and jar task. How can I only put the first block for some sub-projects ?

I tried using a variable like in Multi-project Builds - Gradle but I couldn't access it in subprojects block.

Thank you in advance. I'm really interested in using Gradle correctly and it's a bit hard to get into it outside of simple Android/Java projects. Feel free to include any documentations I should read :)

Edit:

Thank you. I wouldn't have posted here if I hadn't search before. Apparently I was missing the keyword "subset" who would have gave me the solution you linked.

like image 483
sjcqs Avatar asked Dec 10 '25 15:12

sjcqs


1 Answers

A solution is described here: https://discuss.gradle.org/t/configure-only-subset-of-subprojects/5379/3

You can run configure() with a list of projects.

project.ext {
    subprojectList = subprojects.findAll{
        it.name == 'subprojectA' ||
        it.name == 'subprojectB' ||
        it.name == 'subprojectC'
    }
}
  configure(project.subprojectList) {
    // insert your custom configuration code
}

or

configure([project(':a'), project(':b'), project(':c')]) {
    // configuration
}
like image 101
tkruse Avatar answered Dec 13 '25 14:12

tkruse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!