java - war 文件的 Gradle flavor

标签 java gradle war

我正在尝试使用不同的 java 代码构建调试和发布 war 文件 - 与 Android 构建风格的工作方式非常相似。基本上我想在我的 DataSource 周围包装一个记录器使用 P6Spydatasource-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 (我还没有应用包装器,只是试图让所有东西都编译)。

这种工作 - 至少,compileDebugJavacompileDebugRelease作品。然而 compileClassescompileJava任务失败 - 这是意料之中的,代码依赖于需要变化的类。

然而似乎 compileJavadebugWar 调用任务,但我无法弄清楚如何删除该依赖项,或者让它只调用 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/

相关文章:

java - 高性能简单 Java 正则表达式

java - 使用 ExecutorService 和 arrayCopy() 并行处理数组

java - 可以将 XmlPullParser 配置为不忽略空格吗?

eclipse - 如何将新的自定义 "sample projects"添加到 Eclipse "New Gradle Project"向导?

java - 我可以使用 IntelliJ IDEA 将多个 WAR 存档文件部署到本地 Tomcat 吗?

独立 servlet 容器中的 Spring Boot 应用程序 war

java - create方法的返回结果和201状态码

android - 停止Cordova CLI恢复build.gradle

java - 使用 Gradle 构建 EAR,在 WAR 之间共享依赖关系

java - 在 Sun Application Server PE 8(大约 2005 年)上部署 {EAR,WAR} Web 应用程序?