Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a resource file in Gradle

Tags:

java

gradle

build

One of my Gradle project has 10 subprojects. For each of these projects from Gradle task I want to generate a Java properties file which will specify the project name, because I need to know project name during runtime. The properties file should go to resources directory of each project. Because the number of projects might grow over time, I thought to not add a resource file for each of them, but generate it via Gradle.

For example, for project :custom:a I need to have a project-info.properties file in resources directory with contents:

project=a

For project :custom:b I want a similar project-info.properties files:

project=b

How can I generate a file and put it to Java resources directory?

I tried to add the following rule in all projects but it doesn't seem to generate these files anymore (maybe I've introduced some breaking changes):

processResources {
    def resourcesDir = sourceSets.main.output.resourcesDir
    resourcesDir.mkdirs()
    def contents = "projectInfo.project=$project.name"
    new File(resourcesDir, "project-info.properties").text = contents
}

Edit: Files should be generated in build directory, not in source.

like image 878
X. Wo Satuk Avatar asked Aug 01 '18 16:08

X. Wo Satuk


People also ask

What is the Resources folder in Gradle project?

The resource directory is used mainly for storing files that were created before runtime, so that they can be accessed during runtime. It is not meant to be written to during runtime.

How do I create a tar file in Gradle?

You can run gradle distZip to package the main distribution as a ZIP, or gradle distTar to create a TAR file. To build both types of archives just run gradle assembleDist . The files will be created at $buildDir/distributions/${project.name}-${project. version}.

How do I create a directory in Gradle?

Creating directories All core Gradle tasks ensure that any output directories they need are created if necessary using this mechanism. As described in the Apache Ant manual, the mkdir task will automatically create all necessary directories in the given path and will do nothing if the directory already exists.


2 Answers

I strongly advise against generating files in normal source directories, i.e. those that are checked into source control (SCM). It's far better to generate the files under the build directory somewhere and add the location to the relevant source set.

The first step is to create a task that generates the properties file:

ext.genOutputDir = file("$buildDir/generated-resources")

task generatePropInfo {
    ext.outputFile = file("$genOutputDir/project-info.properties")
    outputs.file(outputFile)
    doLast {
        outputFile.text = "projectInfo.project=${project.name}"
    }
}

You might even want to consider making this a custom task type so you don't have to use extra properties and the runtime API for incremental build.

With that done, you just need to configure the source set so that the files in the generated-resources directory are included as outputs of the source set. Put this line after the task definition:

sourceSets.main.output.dir genOutputDir, builtBy: generatePropInfo

If you want to have multiple tasks that generate "output" files for a source set, then create an aggregate task that depends on all of them — say generateResources — and update the builtBy argument to that task — buildBy: generateResources.

Feel free to ask any questions about the solution.

like image 66
Peter Ledbrook Avatar answered Oct 02 '22 23:10

Peter Ledbrook


Try to run it in the execution phase, not configuration:

processResources {
    doFirst {
        def resourcesDir = sourceSets.main.output.resourcesDir
        resourcesDir.mkdirs()
        def contents = "projectInfo.project=$project.name"
        new File(resourcesDir, "project-info.properties").text = contents
    }
}

doFirst should do the trick and make a file creation just before this task is running

like image 22
Stanislav Avatar answered Oct 03 '22 00:10

Stanislav