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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20136214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:24:19  来源:igfitidea点击:

Gradle: common resource dependency for multiple java projects

javaintellij-ideagradle

提问by Eric Wu

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!

非常感谢!

采纳答案by Peter Niederwieser

One solution is to apply the Java plugin also to the dataproject, 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.

一种解决方案是将 Java 插件也应用于data项目,然后使用常规项目依赖项(例如dependencies { runtime project(":data") })。但是,这需要付出一些努力来防止传送测试资源。

Another solution is not to make dataa 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").

另一种解决方案不是创建dataGradle 项目,而是将其资源目录包含在其他两个项目 ( sourceSets.main.resources.srcDir "../data/main/resources"; sourceSets.test.resources.srcDir "../data/test/resources") 中。

回答by Eric Wu

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

回答by Tono Wiedermann

The approach I took was to use project reference

我采取的方法是使用项目参考

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

回答by nucatus

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

您需要选择将容纳您的资源的项目。所有其他需要这些资源的项目都会将它们添加到resourcessourceSets的组件中。

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

回答by Dmitri Korobtsov

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