Gradle: common resource dependency for multiple java projects

后端 未结 6 501
鱼传尺愫
鱼传尺愫 2020-12-28 17:47

I\'m developing a multi-module project with gradle/intellij-idea, and here is the structure of my project home:

project/
  sub-project-1
    /main/resources
         


        
相关标签:
6条回答
  • 2020-12-28 18:26

    You need to choose the project which will hold your resources. All the other projects that require those resources, will add them to the resources component of the sourceSets.

    sourceSets {
        data {
            resources {
                srcDir "${project(':data').projectDir}/src/main/resources"
                include "your_pattern_here**"
            }
        }
        main {
            resources {
                srcDirs += [ data.resources ]
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-28 18:31

    Ok, I figured it out. It's actually pretty simple. Just treat the "data" folder as another project and add dependency declarations to sub projects will do the trick. For example:

    dependencies {
        compile project (':data')
        testCompile project (':data')
    }
    
    0 讨论(0)
  • 2020-12-28 18:33

    And here is version for Kotlin DSL - to sync all resources from :data module to root and all sub-modules build/resources folders:

    // Synchronizing resources from :data module to project root: build/resources
    synchronizeSharedResources()
    
    subprojects {
        // Synchronizing resources from :data module to all submodules: build/resources
        synchronizeSharedResources()
    }
    
    fun Project.synchronizeSharedResources() {
        sourceSets {
            main {
                resources.srcDir(project(":data").sourceSets["main"].resources.srcDirs)
            }
            test {
                resources.srcDir(project(":data").sourceSets["test"].resources.srcDirs)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-28 18:39

    One solution is to apply the Java plugin also to the data project, and then use regular project dependencies (e.g. dependencies { runtime project(":data") }). However, this would require a bit of effort to prevent shipping the test resources.

    Another solution is not to make data a Gradle project but literally include its resource directories in the other two projects (sourceSets.main.resources.srcDir "../data/main/resources"; sourceSets.test.resources.srcDir "../data/test/resources").

    0 讨论(0)
  • 2020-12-28 18:42

    The approach I took was to use project reference

    sourceSets {
        main {
            resources {
                srcDirs += [
                    project(':data').sourceSets.main.resources
                ]
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-28 18:51
    sourceSets {
        main {
            resources {
                srcDirs += [
                    '../sub-project-1/src/main/resources',
                    '../sub-project-2/src/main/resources',
                    '../data/src/main/resources'
                ]
            }
        }
    
        test {
            resources {
                srcDir '../data/src/test/resources'
            }
        }    
    }
    
    0 讨论(0)
提交回复
热议问题