简体   繁体   English

战争档案的摇篮风味

[英]Gradle Flavors For War Files

I'm attempting to build debug and release war files using different java code - much in the same way that Android build flavors work. 我正在尝试使用不同的Java代码来构建调试和发布war文件-与Android构建工具的工作方式几乎相同。 Basically I want to wrap a logger around my DataSource using either P6Spy or datasource-proxy during development, but definitely don't want to ship either in the release build. 基本上,我想在开发过程中使用P6Spydatasource-proxy将记录器包裹在DataSource周围,​​但是绝对不希望在发布版本中附带任何记录器。 I'd prefer to do this in code, rather than at the datasource definition in the app server. 我更喜欢用代码而不是应用服务器中的数据源定义来做到这一点。

My build.gradle looks something like this: 我的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'
}

I have the database management class in both src/release/java and src/release/debug (I haven't applied the wrapper yet, just trying to get everything to compile). 我在src/release/javasrc/release/debug中都有数据库管理类(我还没有应用包装器,只是想让所有东西都可以编译)。

This kind of works - well, at least, compileDebugJava and compileDebugRelease works. 这种工作-至少, compileDebugJavacompileDebugRelease可以正常工作。 However the compileClasses and compileJava tasks fail - and that's to be expected, the code has dependencies on the class that needs to vary. 但是compileClassescompileJava任务失败-可以预料,代码依赖于需要变化的类。

However it appears that compileJava is called from the debugWar task, and I haven't been able to figure out how to remove that dependency, or to get it to just call the debugCompileJava task instead of the compileJava . 但是看来compileJava是从debugWar任务中调用的,而且我还无法弄清楚如何删除该依赖关系,或者使它仅通过调用debugCompileJava任务而不是compileJava

Unfortunately I'm not in a situation where I can use DI, or to modify the code to use an interface. 不幸的是,我无法使用DI或修改代码以使用接口。

There's also the issue of IDE integration. 还有IDE集成的问题。 It looks like most IDEs want to use compileClasses. 看起来大多数IDE都想使用compileClasses。 Since that fails, the IDE integration starts to break. 由于失败,IDE集成开始中断。 What would be ideal, is would be to use all the Debug tasks as the default. 理想的情况是将所有Debug任务都用作默认任务。

I've tried having the release version of the class in the main code, and just having an debug version in the debug code, but that results in a duplicate class error. 我尝试过在主代码中包含该类的发行版,而在调试代码中仅具有一个调试版,但这会导致重复的类错误。 This woul 这会

Is there any way to get this to do what I want, or something close to it? 有什么方法可以让它完成我想要的事情,或者做些接近它的事情?

There are a quite a few hoops I had to go through to get a working configuration, but a working configuration I have. 我必须经过很多步骤才能获得有效的配置,但我必须有一个有效的配置。

First, to avoid the default compile/process resources tasks from failing, I set the java and resources srcDirs to nothing. 首先,为避免默认的编译/处理资源任务失败,我将java和资源srcDirs设置为none。 Then I discovered the war plugin doesn't care about what you have configured in your sourceSets - it's always going to use the same folders for things - so debug and release builds/resources share the same build paths. 然后,我发现war插件并不关心您在sourceSets中配置的内容-它总是使用相同的文件夹进行处理-因此debug和release构建/资源共享相同的构建路径。 And the war plugin also won't pick up from your dependencies, so to get the debugImplemenation for one of the JDBC wrappers, I had to include a "debug" configuration 而且war插件也不会从您的依赖项中获取,因此要获取其中一个JDBC包装程序的debugImplemenation,我必须包括一个“ debug”配置

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    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM