Gradle: jar任务 - 覆盖来自的jar包

3

我需要定制我的jar任务,使它能够:

  • 像往常一样处理所有类;
  • 仅包含特定资源并将它们放入自定义文件夹中。

我已经创建了一个定制的jar任务,它可以实现我想要的功能:

task customJar(type: Jar) {
    dependsOn classes
    group = 'build'

    // everything except resources process as usual
    from sourceSets.main.output - sourceSets.main.output.resourcesDir

    // and process resources to custom place
    from(sourceSets.main.output.resourcesDir) {
        include 'docs/**'
        include 'messages*.properties'
        into 'custom-folder'
    }
}

但我仍需要用新的jar文件替换内置的。

使用create进行替换。

tasks.create(name: "jar", type: Jar, overwrite: true) {
    // ... custom jar spec
}

...产生的结果

Replacing an existing task that may have already been used by other plugins is not supported

简单配置jar任务不起作用,因为它已经在JavaPlugin中配置了:

jar {
    // jar is already configured
    // with `from sourceSets.main.output`
    // so it will include everything
    // AND create a custom folder

    // does nothing
    from sourceSets.main.output - sourceSets.main.output.resourcesDir

    // adds processed resources into 'custom-folder'
    // in addition to all resources processed by default behaviour
    from(sourceSets.main.output.resourcesDir) {
        include 'docs/**'
        include 'messages*.properties'
        into 'custom-folder'
    }
}

所以,我需要重写(覆盖)jar中默认的from配置。这是否可行?

1个回答

1

you were there almost ..

tasks.create(name: "myJar", type: Jar) {
    // ... custom jar spec
}

jar.enabled = false //if you want to disable the default
build.dependsOn myJar //ensure this always runs

是的,我试过了。结果发现如果我只想创建一个自定义的jar包,这种方法是可以的,但如果我接着使用“project(:submodule)”依赖,则无法运行。 - RomanMitasov
正如我在问题中所述,我已经创建了一个可工作的自定义任务。但是我想要改变原始内置的jar任务的行为。 - RomanMitasov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接