问题描述
是否有一种标准方法可以在Gradle Project Layout中的所有子项目中共享日志记录配置(例如,为log4j或logback)?
我现在要做的是在每个子项目中放入logback.xml(或log4j.properties)的副本,但这会导致此配置文件的大量不必要的重复
推荐答案
使用Gradle中的多个工作集可以很容易地克服.
在项目的根部添加一个新文件夹,示例"共享 - 资源"将我们的配置放在其中,然后简单地将以下行添加到子项目
上sourceSets { main { resources { srcDirs = ["src/main/resource", "../shared-resources"] } } }
这应该将两个文件添加到您的Jar文件中.
可以在 github 中找到一个示例.
其他推荐答案
创建一个共享util模块,其中包含您的Log4j2配置在其src/main/resources目录中.
然后将util模块导入其他模块.
dependencies { compile project(":util"); }
我还使用util模块进行可重复使用的Java代码,而不仅仅是用于log4j2的一次性配置.
问题描述
Is there a standard way to share a logging config (for log4j or logback for example) across all sub projects in a gradle project layout?
What I do right now is put a copy of logback.xml (or log4j.properties) in src/main/resources in each sub-project but this results in a lot of unnecessary duplication of this config file
推荐答案
This can be easily overcome using multiple working sets in gradle.
Add a new folder in the root of the project, example "shared-resources" put our configs inside it, and simple add the following line to your build.gradle on the sub-project
sourceSets { main { resources { srcDirs = ["src/main/resource", "../shared-resources"] } } }
This should add both files to your jar file.
An example can be find in github
其他推荐答案
Create a shared util module containing your Log4j2 configuration in its src/main/resources directory.
Then import the util module into others.
dependencies { compile project(":util"); }
I also use the util module for re-usable Java code, not just for once-off configuration of Log4j2.