Creating file while using gradle testkit

When creating a file while using gradle testkit the file ends up in the projectDir for the test instance instead of in the temporary gradle test dir.

gradle.properties

outputJsonFile=build/output.json

build.gradle

task createJson {
	doFirst {
		def jsonFile = new File(outputJsonFile)
		jsonFile.write("DUMMY")
	}	
}

This task creates an file in the projectDir/build/output.json instead of in an tempDir/build/output.json

What am I doing wrong?

I created an minimal sample project: https://github.com/ki82/propertyproblem

Don’t use new File(...) use file(...) instead which is shorthand for project.file(...)

1 Like

Thanks, that solved it!