简体   繁体   中英

Gradle: jar task - override from

I need to customize my jar task so it would:

  • process all classes as usual;
  • include only particular resources and put them into custom folder inside the jar.

I've already made custom jar task, which does what I want:

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'
    }
}

But I still need to replace built-in jar with a new one.

Replacing it with create

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

...yields

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

and simple configuration of the jar task doesn't work, because it is already configured in 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'
    }
}

So, what I need is to rewrite (override) default from configuration of jar . Is it possible?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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