Deprecation warnings

2020-08-09 08:15发布

问题:

I have this code:

task fatJar(type: Jar) << {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',
                'Implementation-Version': version,
                'Main-Class': 'mvc.MvcMain'
    }
    baseName = project.name + '-all'
    with jar
}

I got this warning:

Configuring child specs of a copy task at execution time of the task has been deprecated and is scheduled to be removed in Gradle 4.0. Consider configuring the spec during configuration time, or using a separate task to do the configuration. at build_b2xrs1xny0xxt8527sk0dvm2y$_run_closure4.doCall

and this warning:

The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.

How to rewrite my task?

回答1:

The line that is essentially causing both of the warnings is

task fatJar(type: Jar) << {

In the Gradle version you are using (which is presumably some 3.x version, the leftShift Groovy operator is calling the doLast method. Everything passed to << (leftShift) is executed in the doLast task action of that task.

To fix leftShift deprecation

The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.

You would want to change your task to:

task fatJar(type: Jar) {
  doLast {
    manifest {
      attributes 'Implementation-Title': 'Gradle Jar File Example',
                 'Implementation-Version': version,
                 'Main-Class': 'mvc.MvcMain'
    }
    baseName = project.name + '-all'
    with jar
  }
}

Now, however, you will still see the other warning:

Configuring child specs of a copy task at execution time of the task has been deprecated and is scheduled to be removed in Gradle 4.0. Consider configuring the spec during configuration time, or using a separate task to do the configuration.

This warning is telling you that you are modifying the configuration of your fatJar task at execution time, which is a bad thing to do. It messes upGradle up-to-date checking and incremental builds.

The way to fix this warning, is to follow one of the suggestions from the previous CLI output. For example, here is "configuring the spec during configuration time".

task fatJar(type: Jar) {
  manifest {
    attributes 'Implementation-Title': 'Gradle Jar File Example',
               'Implementation-Version': version,
               'Main-Class': 'mvc.MvcMain'
  }
  baseName = project.name + '-all'
  with jar
}

Notice there is no longer a doLast block since we aren't adding any different actions.



回答2:

it might look about like this; this should at least fix one of the warnings:

task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',
                   'Implementation-Version': version,
                   'Main-Class': 'mvc.MvcMain'
    }
    baseName = project.name + '-all'
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        } 
    }
    with jar
}


标签: gradle