将蚂蚁任务与gradle并行

时间:2019-03-25 07:18:15

标签: gradle ant

我正在尝试将ant任务拆分为多个并行任务。 例如,JRAntCompileTask使用路径来提供要编译的文件集

task compileJasperReports {
  ext.resDir = file("$buildDir/compiled-reports")
  ext.tmpDir = file("$buildDir/tmp")

  doLast {
    resDir.mkdirs()
    tmpDir.mkdirs()

    ant {
      taskdef(name: 'jrc',
        classname: 'net.sf.jasperreports.ant.JRAntCompileTask',
        classpath: configurations.jasperreports.asPath
      )
      jrc(srcdir: 'reports', destdir: resDir, tempdir: tmpDir, keepjava: 'false', xmlvalidation: 'true') {
        classpath(path: configurations.reports.asPath)
        include(name: '**/*.jrxml')
      }
    }
  }
}

因此,我将路径分成相等的集合。

task compileJasperReports {
  dependsOn ':ReportsBase:jar'

  ext.resDir = file("$buildDir/compiled-reports")
  ext.tmpDir = file("$buildDir/tmp")

  doLast 
  {
    resDir.mkdirs()
    tmpDir.mkdirs()

    ant {
      String[] files = fileset(dir: 'reports', {
        include(name: '**/*.jrxml')
      }).getDirectoryScanner(project).includedFiles

      int tasks = 4
      int partsize = (files.size() + tasks - 1).intdiv(tasks)
      def parts = []

      for (int task in 0..<tasks) {
        def range = new IntRange(false, partsize * task, Math.min(partsize * (task + 1), files.size()))
        println "### task: $task ${range.from}..${range.to}"

        Path part = path {filelist(dir: 'reports', files: files[range].join(','))}
        parts.add(part)
      }

      taskdef(name: 'jrc',
        classname: 'net.sf.jasperreports.ant.JRAntCompileTask',
        classpath: configurations.jasperreports.asPath)

      parallel(threadsPerProcessor: 2) {
        for (Path part in parts) {
          jrc(srcdir: part, destdir: resDir, tempdir: tmpDir, keepjava: 'false', xmlvalidation: 'true') {
            classpath(path: configurations.reports.asPath)
          }
        }
      }
    }
  }
}

但是什么也没发生。使用--debug日志记录,我可以看到很多消息

   [ant:jrc] file.jrxml omitted as file.jasper is up to date.

我在做什么错?有没有更好的方法?

0 个答案:

没有答案