[英]Gradle change resource files target location in JAR
I am Gradle-fying a legacy project with the following structure: 我正在Gradle从事具有以下结构的旧项目:
root
+--- common
| \--- config
+--- module1
\--- module2
In the original project, config
is just a folder that contains configuration files organized in subfolders for different environments. 在原始项目中, config
只是一个文件夹,其中包含针对不同环境按子文件夹组织的配置文件。 It contains a top level folder props
and many subfolders as in: 它包含一个顶级文件夹props
和许多子文件夹,如下所示:
config
\--- props
+--- prod
+--- dev
+--- john
\--- mike
The project can be configured to use any of the subfolders. 可以将项目配置为使用任何子文件夹。 The configuration gets loaded by a method that looks like this: 通过如下方法加载配置:
Config.class.getClassLoader().getResourceAsStream("props/" + ENV + "/" + name);
where ENV
defines the environment (it's a system property), ie, it can be prod
, dev
, mike
, etc., and name
is just the name of the file to load. ENV
定义环境(它是系统属性),即可以是prod
, dev
, mike
等,而name
只是要加载的文件的名称。
When running tests, I need to have the props
folder in the classpath. 运行测试时,我需要在类路径中有props
文件夹。 When building the production artifacts (JARs and WARs) I wan't to avoid that and manually copy only the files I need in order to avoid possible conflicts or accidents. 在构建生产工件(JAR和WAR)时,我不会避免这种情况,而是仅手动复制所需的文件,以避免可能的冲突或事故。
So I decided to make config
its own Gradle project and add it as a testCompile dependency to other modules that require configuration. 因此,我决定将config
设为自己的Gradle项目,并将其作为testCompile依赖项添加到其他需要配置的模块中。 However, if I add the props
folder as a resource folder in Gradle, the generate JAR file for the config
module will flatten all the subfolders of props
(which is the intended behavior), and thus the code above will simply fail. 但是,如果我将props
文件夹添加为Gradle中的资源文件夹,则为config
模块生成的JAR文件将拉平props
所有子文件夹(这是预期的行为),因此上面的代码将完全失败。
My question is: is there a way to tell Gradle to copy those files to a subfolder called props instead of to the root of the JAR? 我的问题是:有没有办法告诉Gradle将那些文件复制到一个名为props的子文件夹中,而不是复制到JAR的根目录中?
I know it would be easy to refactor the project and move the folders around but we are in a phase of transition from legacy build and deployment tools and want to maintain the original structure as much as possible until we can switch to Gradle completely. 我知道重构项目和移动文件夹很容易,但是我们正处在从旧版构建和部署工具过渡的阶段,并且希望保持尽可能多的原始结构,直到我们完全切换到Gradle。 It's an iterative process and can't happen overnight. 这是一个反复的过程,不可能一overnight而就。 So I need an interim solution. 因此,我需要一个临时解决方案。
Here's how I ended up doing it. 这就是我最终完成此操作的方式。 This is the build.gradle
file for the config
module: 这是config
模块的build.gradle
文件:
apply plugin: 'java'
sourceSets {
main {
resources {
srcDir 'props'
}
}
}
// this is to force Gradle to create the JAR used at
// runtime with the correct folder structure
jar.into('props')
idea.module.iml.withXml {
def node = it.asNode()
// this is to force IntelliJ to create the folders
// used at runtime with the correct folder ('package') structure
node.component.content.sourceFolder.@packagePrefix="props"
}
It works like a charm because the config
module only contains resources in the props folder. 因为config
模块仅在props文件夹中包含资源,所以它的工作原理就像一种魅力。 Whew. 呼。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.