我正在评估 gradle 以替换 ant 构建脚本,但我无法找到创建正确管理 dev/prod 的标准构建脚本的解决方案环境。
ant 脚本(它用于 java 项目,而不是 android)的结构是这样的:
- 具有标准任务(编译、构建 jar、构建 war )的通用脚本
- 一个特定的项目脚本,其中包括第一个脚本并通过一些属性定义 war 任务应该在何处选择正确的文件
我们的项目结构/taks 允许在最终 war 中覆盖整个目录。让我们考虑这个例子: dev 配置是标准配置,并放置在 dir webcontent 中 在 prod 目录下(即 *prod/conf1*m prod/conf2 等)
ant build 有 dev_build 任务作为 prod_conf1_build 一个,prod_conf2_build 一个,等等 XXX_build 任务做同样的事情:
- 指定包含环境目录/文件的父目录(这是一个项目属性)
- 使用调用任务中指定的属性调用构建 war 的相同 Ant 任务
我正在尝试在 gradle 中做同样的事情,但似乎即使从另一个任务中调用任务也会产生一些问题(即任务始终是最新的)
这是一个脚本(这是一个工作草案,我正在学习 gradle),它试图做同样的事情,但是当我调用 war_prod 时它不起作用,taks 什么都不做,因为它报告最新
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
project.ext.envdir = ""
eclipse {
jdt {
sourceCompatibility = 1.8
targetCompatibility = 1.8
javaRuntimeName = "jdk-1.8.x"
}
}
// In this section you declare where to find the dependencies of your project
repositories {
maven {
url 'http://artifactory.zzzz.priv/artifactory/libs-release'
url 'http://artifactory.zzzz.priv/artifactory/libs-snapshot'
credentials {
username 'xxxx'
password 'yyyy'
}
}
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.18'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
task war_prod {
project.ext.envdir='prod/conf1'
project.ext.envdir=project.ext.envdir.replaceAll('\\\\',File.pathSeparator)
project.ext.envdir=project.ext.envdir.replaceAll('/',File.pathSeparator)
tasks.war.execute()
}
war {
eachFile {
println 'endir' + project.ext.envdir
println 'evaluating' + it
FileTree tree = fileTree(dir: project.ext.envdir)
tree.visit { FileVisitDetails file->
if (!file.file.isDirectory()) {
println '\tFileVisitDetails relpath ' + file.relativePath
println '\tsourcepath ' + it.file.getAbsolutePath()
println '\tcontains ' + it.file.getAbsolutePath().contains(project.ext.envdir)
if (it.relativePath == file.relativePath && !it.file.getAbsolutePath().contains(project.ext.envdir)) {
it.exclude()
println '\texcluding ' + it
} else {
if (it!=null) {
//println '\tincluding ' + it
}
}
}
}
}
from 'prod/conf1'
}
谁能指出我创建正确 gradle 脚本的正确方向? 是否有特定的 gradle 方法来构建带有 prod/dev 配置的 war 文件(其中配置由一些目录和文件表示)?
最佳答案
在这种情况下task rules可能非常有用。基本思想是以结构化方式保存配置,并使用通用任务构建一个定义了配置的 war 文件。请查看下面的 build.gradle:
apply plugin: 'war'
repositories {
mavenCentral()
}
tasks.addRule("Pattern: buildWar<ENV>") { String taskName ->
if (taskName.startsWith('buildWar')) {
def env = (taskName - 'buildWar').toLowerCase()
if (env in ['dev', 'prod',]) {
task(taskName, type: War) {
println "Configuring env: $env"
from("src/main/conf/$env") {
into("conf")
}
}
} else {
println "Invalid env: $env, skipping."
}
}
}
此处定义的 buildWarENV
规则非常具有 self 描述性。它接受 dev 和 prod 两个环境,并通过从适当的文件夹中获取配置来准备 war 文件。你可以找到一个演示 here .如有疑问,请提问。
附言Gradle 的工作模型与 Ant 有点不同,从基础开始。重要的是,永远不要从其他任务中运行任务。
关于java - 如何使用 dev/prod 配置覆盖 gradle 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36278999/