在 Gradle 中创建多个具有不同依赖项的 .WAR 文件 [英] Create multiple .WAR files with different dependencies in Gradle

查看:23
本文介绍了在 Gradle 中创建多个具有不同依赖项的 .WAR 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 war 插件为我在 gradle 中的项目生成一个简单的 .WAR 文件.我想知道如何配置 gradle,以便我可以创建 4 个具有不同依赖项的不同 .WAR 文件.

I am using the war plugin to generate a simple .WAR file for my project in gradle. I'd like to know how to configure gradle so that I can create 4 different .WAR files with different dependencies.

我已经使用进入发行版所需的 jar 配置了依赖项编译配置.src 中的任何代码都不依赖于这些 jar 中的几个,但我想知道如何配置项目以创建

I've configured the dependency compile configuration with the jars that are needed to go into the distribution. None of the code in the src depends on a couple of these jars but I would like to know how to configure the project to create

  • 一个标准的.WAR 文件,其中包含依赖关系图中的所有 jar(即使它们没有被使用 - 没关系 - 我正在测试一些东西)
  • 另一个只包含 qas.jar 的标准 qas-only.WAR 文件
  • 另一个包含 qas.jar 和 log4j 的标准-qas-log4j.WAR 文件

我配置哪些任务来生成使用特定依赖项配置的工件?

What tasks do i configure to have the artifact generated use a particular dependency configuration?

仅供参考:在这种情况下,编译所需的唯一 jar 是 qas.jar.

FYI: The only jar that is required for compilation is qas.jar in this case.

我下面的示例创建了一个仅包含一个 jar 的 war 文件,但我希望使用不同的 jar 生成 5 个不同的 .war 文件.

My example below creates a war file that only includes one jar but i'd like to have 5 different .war files generated with different jars.

build.gradle

apply plugin: 'java'
apply plugin: 'war'

dependencies {
    compile files('/lib/qas.jar','/lib/axis1-1.4.jar','/lib/axis2-kernel-1.3.jar','/lib/dom4j-1.6.1.jar','/lib/log4j-1.2.14.jar')
    providedCompile files('/lib/j2ee-1.4.03.jar')
}

war {
    classpath = ['/lib/qas.jar']
}

task dist(dependsOn: 'war') << {
    copy {
        from war.archivePath
        into "dist/"
    }
}

推荐答案

对于您实际尝试构建多少个 WAR 发行版,我有点困惑.您可以轻松修改它以创建其他 WAR 文件.这是实现这一目标的一种方法:

I got a bit confused on how many WAR distributions you are actually trying to build. You can easily modify it to create additional WAR files. Here's one approach to make this happen:

task createStandardWar(type: War, dependsOn: classes) {
    baseName = 'standard'
    destinationDir = file("$buildDir/dist")
}

task createStandardWarQasOnly(type: War, dependsOn: classes) {
    baseName = 'standard-qas-only'
    destinationDir = file("$buildDir/dist")
    classpath = war.classpath.minus(files('/lib/axis1-1.4.jar','/lib/axis2-kernel-1.3.jar','/lib/dom4j-1.6.1.jar','/lib/log4j-1.2.14.jar'))
}

task createStandardWarQasAndLog4J(type: War, dependsOn: classes) {
    baseName = 'standard-qas-log4j'
    destinationDir = file("$buildDir/dist")
    classpath = war.classpath.minus(files('/lib/axis1-1.4.jar','/lib/axis2-kernel-1.3.jar','/lib/dom4j-1.6.1.jar'))
}

task createDists(dependsOn: [createStandardWar, createStandardWarQasOnly, createStandardWarQasAndLog4J])

此构建脚本摘录通过声明 战争.它假定您仍然希望在 WAR 文件中的 WEB-INF/classes 下拥有已编译的源文件,因此我没有将其从类路径中删除.发行版最终位于 build/dist 目录中.任务 createDists 创建所有这些.

This build script excerpt creates three different WAR files by declaring enhanced tasks of type War. It assumes that you still want to have your compiled source files under WEB-INF/classes within the WAR files so I didn't remove it from the classpath. The distributions end up in the directory build/dist. The task createDists creates all of them.

这篇关于在 Gradle 中创建多个具有不同依赖项的 .WAR 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆