我正在尝试使用不同的 java 代码构建调试和发布 war 文件 - 与 Android 构建风格的工作方式非常相似。基本上我想在我的 DataSource
周围包装一个记录器使用 P6Spy或 datasource-proxy在开发过程中,但绝对不想在发布版本中发布。我更喜欢在代码中执行此操作,而不是在应用服务器中的数据源定义中执行此操作。
我的 build.gradle 看起来像这样:
apply plugin: 'java'
apply plugin: 'war'
configurations {
// debug
debugImplementation.extendsFrom implementation
debugRuntimeOnly.extendsFrom runtimeOnly
debugCompileOnly.extendsFrom compileOnly
// release
releaseImplementation.extendsFrom implementation
releaseRuntimeOnly.extendsFrom runtimeOnly
releaseCompileOnly.extendsFrom compileOnly
}
sourceSets {
debug {
java {
compileClassPath += main.compileClasspath
runtimeClassPath += main.runtimeClasspath
srcDirs = ['src/debug/java','src/main/java']
}
}
release {
java {
compileClassPath += main.compileClasspath
runtimeClassPath += main.runtimeClasspath
srcDirs = ['src/release/java','src/main/java']
}
}
}
dependencies {
debugImplementation 'logging:artifact'
}
task debugWar(type: War, group: "Build") {
from sourceSets.debug.output
classifier = 'debug'
}
我在
src/release/java
中都有数据库管理类和 src/release/debug
(我还没有应用包装器,只是试图让所有东西都编译)。这种工作 - 至少,
compileDebugJava
和 compileDebugRelease
作品。然而 compileClasses
和 compileJava
任务失败 - 这是意料之中的,代码依赖于需要变化的类。然而似乎
compileJava
从 debugWar
调用任务,但我无法弄清楚如何删除该依赖项,或者让它只调用 debugCompileJava
任务而不是 compileJava
.不幸的是,我无法使用 DI,也无法修改代码以使用接口(interface)。
还有IDE集成的问题。看起来大多数 IDE 都想使用 compileClasses。由于失败,IDE 集成开始中断。理想的情况是使用所有 Debug 任务作为默认值。
我尝试在主代码中使用该类的发布版本,而在调试代码中仅使用调试版本,但这会导致重复的类错误。这将
有什么办法可以让它做我想要的,或者接近它的东西?
最佳答案
我必须经历很多圈才能获得工作配置,但我有一个工作配置。
首先,为了避免默认的编译/处理资源任务失败,我将 java 和资源 srcDirs 设置为空。然后我发现 war 插件并不关心你在 sourceSets 中配置了什么——它总是会使用相同的文件夹来做事——所以调试和发布构建/资源共享相同的构建路径。而且 war 插件也不会从您的依赖项中获取,因此为了获得其中一个 JDBC 包装器的 debugImplementation,我必须包含一个“调试”配置
sourceSets {
// override the main, telling gradle main has no sources.
main {
java { srcDirs = [] }
resources { srcDirs = [] }
}
// debug builds
debug {
java {
compileClasspath += main.output
runtimeClasspath += main.output
// combine code from main and debug java code
srcDirs = ['src/debug/java', 'src/main/java']
}
// combine resources
resources {
srcDirs = ['src/debug/resources', 'src/main/resources']
}
// override the resources and classes output, for compatibility with the war plugin
output.resourcesDir = 'build/resources/main'
output.classesDir = 'build/classes/java/main'
}
// release builds
release {
java {
compileClasspath += main.output
runtimeClasspath += main.output
// combine code from main and release java code
srcDirs = ['src/release/java', 'src/main/java']
}
// combine resources
resources {
srcDirs = ['src/release/resources', 'src/main/resources']
}
// override the resources and classes output, for compatibility with the war plugin
output.resourcesDir = 'build/resources/main'
output.classesDir = 'build/classes/java/main'
}
}
task debugWar(type: War, group: 'Build') {
classifier 'SNAPSHOT'
from sourceSets.debug.output
// add the debug dependencies
classpath configurations.debug
}
task releaseWar(type: War, group: 'Build') {
from sourceSets.release.output
}
关于java - war 文件的 Gradle flavor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50910966/