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.
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.
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}.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With