Thursday, September 29, 2022

Add resources and config files to your JAR using Gradle Build for Grails Application

How do I add config files or any other resources into my jar using gradle?
How to include everything inside src/main/java to compile

I have a project. Inside the project I have few files like x.properties, x.csv and many more under src/main/webapp package, I used gradle to compile, I didn’t see src/main/webapp directory in my build folder.

How can I include everything to build? Because I need to include contents inside src/main/webapp directory to be include in my jar so that resources can be accessible by who will use my jar.
So first step is to tell compiler to include files in jar file using below code snippet -
Add below code snippet to build.gradle:

sourceSets {
    main {
        resources {
            srcDir "src/main/webapp"
            include "*.properties"
            include "*.csv"
            exclude "*.png"
            exclude "a1"
            exclude "a2"

            srcDir "src/main/webapp/a1"
            exclude "**/*.text"
            include "**/*.csv"

            srcDir "src/main/webapp/a2"
            include "**/*.html"
        }
    }
}
My project structure is like below:
And the files stored in the generated jar file as below:
Now the process to use the resources file into the project, add below configuration to build.gradle file (which project will use jar created above):

copy{
    from(zipTree("path_to_jar_file/plain.jar"))
    into("./")
    include "src/main/webapp/**/*.*" [will extract all to root directory]
    include "src/main/webapp/kkk/*.*" [will extract all files in folder named kkk to root directory]
}
Above configuration will extract all file from (jar) "src/main/webapp" to projectRoot/src/main/webapp directory as below:


No comments:

Post a Comment