简体   繁体   中英

Gradle: Create a jar with no classes, only resources

I'm trying to create a group of four jars with the following pattern (each jar has its own project. helpRootDir is shared between all four jars. If somebody knows of a way to make one task that does all four, that'd be awesome)

def helpRootDir = 'runtime/datafiles/help/'
project(':schedwinclihelp') {
    def helpDir = 'schedwincli'

    //Include no classes.  This is strictly a resource jar
    sourceSets.main.java {
        exclude 'com/**'
    }

    jar {
        from '${helpRootDir}/${helpDir}'
        include '**/*.*'
    }

}

Anyway as you can see from the above, I want no classes in the jar, and that's working. Unfortunately, all I'm actually getting in the jar is a MANIFEST.MF file. None of the files in the jar definition are getting added. I want the full file tree in ${helpRootDir}/${helpDir} added to the root of the jar. How do I accomplish this?

Figured out I was referencing my variables incorrectly.

The correct syntax is:

def helpRootDir = 'runtime/datafiles/help/'
project(':schedwinclihelp') {
    def helpDir = 'schedwincli'

    //Include no classes.  This is strictly a resource jar
    sourceSets.main {
        java {
            exclude 'com/**'
        }
        resources {
            srcDir  helpRootDir + '/' + helpDir
        }
    }
}

Note srcDir helpRootDir + '/' + helpDir rather than '${helpRootDir}/${helpDir}' . Also, I just made my help dir a resource dir and let the java plugin do its thing automatically.

The following task will create a JAR file named resources.jar with main resource files only (those are placed under src/main/resoures directory).

Kotlin DSL:

tasks {
    task<Jar>("resourcesJar") {
        from(sourceSets["main"].resources)
        archiveFileName.set("resources.jar")
    }
}

Groovy DSL:

tasks.create("resourcesJar", Jar.class) {
    from sourceSets.main.resources
    archiveFileName = "resources.jar"
}

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