简体   繁体   English

JAR任务从Gradle 1.7更改为4.1

[英]Changes in JAR task from Gradle 1.7 to 4.1

I have a project which is currently built using Gradle version 1.7 and I'm trying to move to version 4.1 as builds are much faster and dependencies can be downloaded in parallel. 我有一个当前使用Gradle 1.7版构建的项目,由于构建速度更快并且可以并行下载依赖项,因此我正尝试移至4.1版。 However I'm seeing some weird behaviour that I don't quite understand. 但是,我看到了一些我不太理解的奇怪行为。 I have a build.gradle file for a couple of sub projects that overrides the main classes task of the java plugin. 我有几个子项目的build.gradle文件,该文件覆盖了Java插件的主类任务。 In it it runs an ant task that generates classes in the build directory. 它在其中运行一个ant任务,该任务在构建目录中生成类。

task classes(overwrite: true) {
  inputs.dir project.ext.inputsPath
  outputs.dir "${project.buildDir}/classes/main"
  doLast {
    ant.taskdef(name: 'xmlbean', classname: 'org.apache.xmlbeans.impl.tool.XMLBean', classpath: configurations.compile.asPath)
    ant.xmlbean(srcgendir: "${project.buildDir}/generated-sources/xmlbeans",
                classgendir: "${project.buildDir}/classes/main",
                javasource: '1.5',
                failonerror: true,
                includeAntRuntime: false,
                classpath: project.configurations.compile.asPath) {
        fileset(dir: schemaPath, includes: project.ext.has('inclusionPattern') ? project.ext.inclusionPattern : '*.xsd')
    }
  }
}

This all works as expected and I get classes generated in { project_dir }/build/classes/main 这一切都按预期工作,我在{ project_dir }/build/classes/main生成了{ project_dir }/build/classes/main

This is the output i get from the console 这是我从控制台获得的输出

> Task :my-task:classes
  Putting task artifact state for task ':my-task:classes' into context took 0.0 secs.
  Executing task ':my-task:classes' (up-to-date check took 0.002 secs) due to:
  [ant:xmlbean] Time to build schema type system: 0.616 seconds
  [ant:xmlbean] Time to generate code: 1.512 seconds
  [ant:xmlbean] Compiling 226 source files to E:\Development\my-task\build\classes\main

  [ant:xmlbean] 4 warnings

> Task :my-task:classes
  [ant:xmlbean] Time to compile code: 6.263 seconds

  :my-task:classes (Thread[Task worker for ':' Thread 3,5,main]) completed. Took 8.899 secs.
  :my-task:jar (Thread[Task worker for ':' Thread 3,5,main]) started.

> Task :my-task:jar
  Putting task artifact state for task ':my-task:jar' into context took 0.0 secs.
  Executing task ':my-task:jar' (up-to-date check took 0.004 secs) due to:
  Output property 'archivePath' file E:\Development\my-task\build\libs\my-task.jar has changed.

  :my-task:jar (Thread[Task worker for ':' Thread 3,5,main]) completed. Took 0.012 secs.
  :my-task:install (Thread[Task worker for ':' Thread 3,5,main]) started.

> Task :my-task:install

The classes task seems to run twice (not sure if this makes any difference) with the task that runs ant running first. 与先运行ant的任务相比,classes任务似乎运行了两次(不确定是否有任何区别)。 As mentioned earlier I do get classes generated by the ant task. 如前所述,我确实获得了ant任务生成的类。

My problem is that the behaviour between Gradle 1.7 -> 4.1 seems to have changed (As you would expect it to) in that for some reason when the jar task runs my classes inside build/classes/main are not archived into the jar, i just get a blank manifest file. 我的问题是,由于某种原因,当jar任务运行build / classes / main内部的类时,由于某种原因,Gradle 1.7-> 4.1之间的行为似乎已更改(正如您期望的那样),我只是得到一个空白的清单文件。 How do I get these classes generated using the ant task into the Jar using the default jar task? 如何使用默认的jar任务将使用ant任务生成的这些类添加到Jar中?

Why are you overriding the classes task? 您为什么要覆盖classes任务? The normal approach would be to create an additional task which writes to a new directory and wire it into the DAG 通常的方法是创建一个附加任务,该任务将写入新目录并将其连接到DAG中

eg: 例如:

apply plugin: 'java'

task xmlBeanClasses {
   def inclusionPattern = project.ext.has('inclusionPattern') ? project.ext.inclusionPattern : '*.xsd'
   inputs.property 'inclusionPattern', inclusionPattern 
   inputs.dir project.ext.inputsPath
   inputs.dir schemaPath
   outputs.dir "$buildDir/classes/xmlbeans"
   outputs.dir "$buildDir/generated-sources/xmlbeans"
   doLast { 
      // TODO: generate classes in $buildDir/classes/xmlbeans
   }
}

// wire the new task into the DAG
classes.dependsOn xmlBeanClasses

// add the generated classes dir to the "main" classesDirs
// (this dir will now be included in the jar etc)
sourceSets.main.output.classesDirs.add files("$buildDir/classes/xmlbeans")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM