java - Replace File in Gradle Build -
i trying replace file in resource folder (src/main/resources) new file generated gradle build script. i'm having trouble doing this; exclusion seems remembered, , preventing addition of new file.
here's short example illustrates behavior.
project structure:
testproject -- src/main/java ---- entry ------ entrypoint.java ---- run ------ helloworldtest.java -- src/main/resources ---- test.properties // file replace
test.properties contents in src/main/resources:
wrong file text make obvious 1 being put jar based on size
build.gradle:
apply plugin: 'java' task makeprop { def propdir = new file(builddir, "props") ext.propfile = new file(propdir, "test.properties") outputs.file propfile dolast { propdir.mkdirs() propfile.createnewfile() propfile.withwriter('utf-8') { writer -> writer.writeline 'right file' } } } jar { dependson('makeprop') if (project.hasproperty('testexclude')) { sourcesets { exclude('test.properties') } } (makeprop.propfile) { '/' } }
jar contents of ./gradlew build
(both files included):
archive: testproject.jar length date time name -------- ---- ---- ---- 0 08-07-15 14:27 meta-inf/ 25 08-07-15 14:27 meta-inf/manifest.mf 0 08-07-15 13:50 run/ 499 08-07-15 13:50 run/helloworldtest.class 0 08-07-15 13:50 entry/ 1413 08-07-15 13:50 entry/entrypoint.class 95 08-07-15 14:27 test.properties 11 08-07-15 14:03 test.properties -------- ------- 2043 8 files
jar contents of ./gradlew build -ptestexclude
(neither file included):
archive: testproject.jar length date time name -------- ---- ---- ---- 0 08-07-15 14:29 meta-inf/ 25 08-07-15 14:29 meta-inf/manifest.mf 0 08-07-15 13:50 run/ 499 08-07-15 13:50 run/helloworldtest.class 0 08-07-15 13:50 entry/ 1413 08-07-15 13:50 entry/entrypoint.class -------- ------- 1937 6 files
i have done similar , worked me. main objective make sure task runs before jar created , files processed. try out.
// create properties file add folder preprocessing task propertiesfile << { // description 'dynamically creates properties file.' // needed first pass def folder = project.file('src/main/resources'); if(!folder.exists()){ folder.mkdirs() } //write propertiess file def props = project.file('src/main/resources/test.properties') props.delete() props << 'write file!!!!' } processresources.dependson propertiesfile
Comments
Post a Comment