如何使用dev/prod配置覆盖gradle构建



我正在评估替换ant构建脚本的gradle,但我无法找到一个解决方案来创建一个正确管理dev/prod环境的标准构建脚本。

Than ant脚本(用于java项目,而非android)的结构是这样的:

  1. 带有标准任务(编译、构建jar、构建war)的通用脚本
  2. 一个特定的项目脚本,包括第一个,并通过一些属性定义war任务应该在哪里选择正确的文件

我们的项目结构/tak允许在最终战争中覆盖整个目录。让我们考虑这个例子:dev配置是标准配置,它位于目录webcontent中有多个prodconf(每个特定安装中有一个,我们没有超过10个不同的prod配置),所有这些都在prod目录下(即*prod/conf1*mprod/conf2等)

ant构建将dev_build任务作为prod_conf1_build任务、prod_conf2_build任务等XXX_build任务做同样的事情:

  1. 指定包含env目录/文件的父目录(它是项目属性)
  2. 使用调用任务中指定的属性调用构建war的相同ant taks

我试图在gradle中也这样做,但似乎即使从另一个调用taks也会产生一些问题(即任务总是最新的)

这是一个脚本(这是一份工作草案,我正在学习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'
}

有人能为我指出创建正确渐变脚本的正确方向吗?是否有一种特定的渐变方式来使用prod/dev配置构建war文件(其中配置由一些dir和文件表示)?

在这种情况下,任务规则可能非常有用。基本思想是以结构化的方式保持配置,并使用一个通用任务来构建一个定义了配置的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规则是非常自我描述的。它接受两个环境devprod,并通过从适当的文件夹中获取配置来准备war文件。你可以在这里找到一个演示。如果有问题,直接问。

p.S.Gradle的工作模式与蚂蚁有点不同,从基础开始。重要的是,永远不要从其他任务中运行任务。

最新更新