【发布时间】:2016-07-16 16:41:43
【问题描述】:
我正在评估 gradle 以替换 ant 构建脚本,但我无法找到一个解决方案来创建正确管理 dev/prod 的标准构建脚本环境。
ant 脚本(它是用于 java 项目,而不是 android)的结构是这样的:
- 带有标准任务(编译、build-jar、build-war)的通用脚本
- 一个特定的项目脚本,包括第一个脚本,并通过一些属性定义了战争任务应该选择正确文件的位置
我们的项目结构/任务允许在最终战争中覆盖整个目录。让我们考虑这个例子: dev 配置是标准配置,位于 dir webcontent 有多个 prod conf(每个特定安装一个,我们没有超过 10 个不同的 prod 配置)都在 prod 目录下(即 *prod/conf1*m prod/conf2 等)
ant build 有 dev_build 任务作为 prod_conf1_build 之一, prod_conf2_build 一个等 XXX_build 任务做同样的事情:
- 指定包含环境目录/文件的父目录(它是项目属性)
- 使用调用任务中指定的属性调用构建战争的同一个蚂蚁塔
我正在尝试在 gradle 中做同样的事情,但似乎即使从另一个调用 taks 也会产生一些问题(即任务始终是最新的)
这是一个尝试做同样事情的脚本(它是一个工作草案,我正在学习 gradle),但是当我调用 war_prod 时它不起作用,因为它报告了最新的,所以它什么也不做
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 文件(其中配置由一些 dir 和文件表示)?
【问题讨论】: