Java 渐变更改JAR中的资源文件目标位置
我正在对一个具有以下结构的遗留项目进行分级:Java 渐变更改JAR中的资源文件目标位置,java,jar,gradle,classpath,Java,Jar,Gradle,Classpath,我正在对一个具有以下结构的遗留项目进行分级: root +--- common | \--- config +--- module1 \--- module2 在原始项目中,config只是一个文件夹,其中包含在不同环境的子文件夹中组织的配置文件。它包含一个顶级文件夹props和许多子文件夹,如: config \--- props +--- prod +--- dev +--- john \--- mike 可以将项目配置为使用任何子文件夹。配
root
+--- common
| \--- config
+--- module1
\--- module2
在原始项目中,config
只是一个文件夹,其中包含在不同环境的子文件夹中组织的配置文件。它包含一个顶级文件夹props
和许多子文件夹,如:
config
\--- props
+--- prod
+--- dev
+--- john
\--- mike
可以将项目配置为使用任何子文件夹。配置由如下方法加载:
Config.class.getClassLoader().getResourceAsStream("props/" + ENV + "/" + name);
其中ENV
定义了环境(它是一个系统属性),即它可以是prod
、dev
、mike
,等等,name
只是要加载的文件的名称
运行测试时,我需要在类路径中有props
文件夹。在构建生产工件(JAR和WAR)时,我不想避免这种情况,而是只手动复制我需要的文件,以避免可能的冲突或事故
因此,我决定让config
成为自己的Gradle项目,并将其作为testCompile依赖项添加到其他需要配置的模块中。但是,如果我在Gradle中添加props
文件夹作为资源文件夹,那么config
模块的generate JAR文件将展平props
的所有子文件夹(这是预期的行为),因此上面的代码将失败
我的问题是:有没有办法告诉Gradle将这些文件复制到名为props的子文件夹中,而不是复制到JAR的根目录中
我知道重构项目和移动文件夹很容易,但我们正处于从遗留构建和部署工具的过渡阶段,希望尽可能保持原始结构,直到我们完全切换到Gradle。这是一个迭代过程,不可能在一夜之间发生。因此,我需要一个临时解决方案。以下是我最终的做法。这是
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"
}
它就像一个符咒,因为
config
模块只包含props文件夹中的资源。唷。我就是这样做的。这是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"
}
它就像一个符咒,因为config
模块只包含props文件夹中的资源。呼