pardon, I donβt mean for Gradle to load the properties file itself.
How does a Java class load a properties file in a gradle build? When I build via Ant with Netbeans, I put the properties file in the root directory, ie: src. But, when I try that structure with Gradle, using Netbeans, the same source code canβt pick up the properties file. Itβs not copied over?
that is, when running gradle clean run how do I ensure that the properties file is 1.) available, and, 2.) in the right location.
I think the root of the answer that you are looking for is that when Gradle runs tests, the working directory is projectDir. This leaves you wil a couple of options. Iβm your file is locale at βsrc/file.propertiesβ.
[1] Override the working directory for the tests. This can be done in the test configuration:
test {
workingDir 'src'
}
This will affect all of the tests that might be loading files from a relative directory
[2] Tell your tests where the files are,
class MyTest {
final static String FILES_LOCATION = System.getProperty("FILES_LOCATION");
final static File FILES_DIR = = new File(FILES_LOCATION ? FILES_LOCATION : "src" );
}
(Oh boy, it is just so much shorter to write the above in Groovy).
You can then override the location in your build script if you wish
test {
systemProperties FILES_LOCATION : file('src').absolutePath
}
A common way of looking up property files is to lookup via the classpath instead of the file system. I suggest that you follow the standard maven/gradle conventions and move connection.properties from src/main/java to src/main/resources. This will mean that the prop file is available on both the test classpath and also on the classpath of any application using your jar.
Your production and test code can then access the property file at runtime via: SomeClass.class.getClassLoader().getResourceAsStream("connection.properties")