Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 973
StormeHawke Avatar asked Nov 02 '22 11:11

StormeHawke


2 Answers

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.

like image 60
StormeHawke Avatar answered Nov 09 '22 09:11

StormeHawke


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"
}
like image 38
Radosław Panuszewski Avatar answered Nov 09 '22 08:11

Radosław Panuszewski