简体   繁体   English

Gradle:多个java项目的公共资源依赖

[英]Gradle: common resource dependency for multiple java projects

I'm developing a multi-module project with gradle/intellij-idea, and here is the structure of my project home:我正在使用 gradle/intellij-idea 开发一个多模块项目,这里是我的项目主页的结构:

project/
  sub-project-1
    /main/resources
  sub-project-2
    /main/resources
  data
    /main/resources
    /test/resources

As you can see I have multiple sub-projects (all java), how can I make them depend on some common resources (the "data" project, which contains no code but only resources), as well as their own separate resources?如您所见,我有多个子项目(都是 java),我怎样才能让它们依赖于一些公共资源(“数据”项目,它不包含代码,只包含资源),以及它们自己的独立资源?

Also it's best that the intellij-idea can pick up these dependencies with JetGradle automatically (JetGradle do just fine picking up the default gradle java project dependencies within each sub project).此外,最好 intellij-idea 可以使用 JetGradle 自动获取这些依赖项(JetGradle 可以很好地获取每个子项目中默认的 gradle java 项目依赖项)。

Thanks a lot!非常感谢!

The approach I took was to use project reference我采取的方法是使用项目参考

sourceSets {
    main {
        resources {
            srcDirs += [
                project(':data').sourceSets.main.resources
            ]
        }
    }
}

UPDATE: Tested with Gradle7 and it still works.更新:用 Gradle7 测试过,它仍然有效。 Tested with java-library plugin.使用 java-library 插件测试。

One solution is to apply the Java plugin also to the data project, and then use regular project dependencies (eg dependencies { runtime project(":data") } ).一种解决方案是将 Java 插件也应用到data项目,然后使用常规项目依赖项(例如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" ).另一种解决方案不是将data作为 Gradle 项目,而是将其资源目录直接包含在其他两个项目中( sourceSets.main.resources.srcDir "../data/main/resources"; sourceSets.test.resources.srcDir "../data/test/resources" )。

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 的resources组件中。

sourceSets {
    data {
        resources {
            srcDir "${project(':data').projectDir}/src/main/resources"
            include "your_pattern_here**"
        }
    }
    main {
        resources {
            srcDirs += [ data.resources ]
        }
    }
}

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')
}

And here is version for Kotlin DSL - to sync all resources from :data module to root and all sub-modules build/resources folders:这是 Kotlin DSL 的版本 - 将所有资源从 :data 模块同步到 root 和所有子模块 build/resources 文件夹:

// 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)
        }
    }
}
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'
        }
    }    
}

I had another problem with resources in multi-module project, may be it will helpful for somebody.我在多模块项目中遇到了另一个资源问题,可能对某人有帮助。

I have two independent modules:我有两个独立的模块:

- database (contains migrations for whole project)
- main (contains business logic(controllers, services and etc))

Also I use Spring-Boot 2 in my project.我也在我的项目中使用 Spring-Boot 2。

I have integration tests in main and before test is started I need to create schema and tables in DB.我在main进行了集成测试,在测试开始之前我需要在 DB 中创建模式和表。 But all my migrations (use Flyway for migration control) in database module.但是我在database模块中的所有迁移(使用Flyway进行迁移控制)。 Path for migration is src/main/resources/db/migrations .迁移路径是src/main/resources/db/migrations

As a result I need to have all migrations in /test/resources in order to start tests.因此,我需要在/test/resources中进行所有迁移才能开始测试。 Of course, I don't want to duplicate migrations in main module and I found this solution.当然,我不想在main模块中重复迁移,我找到了这个解决方案。 I need to add these lines in files in main module.我需要在main模块的文件中添加这些行。

builde.gradle

dependencies { 
   testImplementation "com.example.module:database:$dbMigrationVersion"
}

application-test.yml

spring:
  flyway:
     locations: classpath:/BOOT-INF/classes/db/migration

After these lines all your migrations from database module will be in main module classpath (in our case for test profile).在这些行之后,您从database模块的所有迁移都将在main模块类路径中(在我们的测试配置文件中)。 And flyway can execute them before starting tests. Flyway 可以在开始测试之前执行它们。

If all your projects are java projects and you apply java plugin to them you can just do the following.如果您所有的项目都是 java 项目并且您将java插件应用于它们,您可以执行以下操作。 Assuming your directory structure you could do:假设你的目录结构你可以这样做:

def dataMainResourcesPath = rootProject.projectDir.path + '/data/main/resources'
def dataTestResourcesPath = rootProject.projectDir.path + '/data/test/resources'

subprojects.each {
    it.sourceSets.main.resources.srcDirs += dataMainResourcesPath
    it.sourceSets.test.resources.srcDirs += dataTestResourcesPath
}

in your root build.gradle .在您的根目录build.gradle中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM