代码之家  ›  专栏  ›  技术社区  ›  Mark

战争档案的梯度口味

  •  0
  • Mark  · 技术社区  · 6 年前

    我正尝试使用不同的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 任务失败-这是意料之中的,代码依赖于需要改变的类。

    但是看起来 编译Java 是从 debugWar 任务,但我还没有弄清楚如何删除该依赖项,或者让它只调用 debugCompileJava 任务而不是 编译Java .

    不幸的是,我不能使用di,也不能修改代码以使用接口。

    还有ide集成的问题。看起来大多数ide都想使用compilecases。由于失败,ide集成开始中断。理想的做法是使用所有调试任务作为默认任务。

    我尝试在主代码中包含类的发布版本,在调试代码中仅包含调试版本,但这会导致重复的类错误。这个世界

    有什么办法让它做我想做的事吗,或是接近它的东西?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Mark    6 年前

    我需要经历很多困难才能得到一个有效的配置,但是我有一个有效的配置。

    首先,为了避免默认编译/进程资源任务失败,我将Java和资源SRCCDRE设置为零。然后我发现war插件并不关心您在源集中配置了什么——它总是使用相同的文件夹来处理事情——所以调试和发布构建/资源共享相同的构建路径。而且war插件也不会从依赖项中获取信息,所以为了获得其中一个jdbc包装器的调试实现,我必须包含一个“调试”配置

    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    
    }