How to read a properties file


(Thufir) #1

Netbeans builds project β€œT” as a default ant project, and it picks up the properties file fine:

thufir@mordor:~/NetBeansProjects$ thufir@mordor:~/NetBeansProjects$ java -jar T/dist/T.jar Apr 05, 2016 2:42:39 AM net.bounceme.mordor.telnet.Main foo INFO: starting.. Apr 05, 2016 2:42:39 AM net.bounceme.mordor.telnet.PropertiesReader getProps INFO: starting.. Apr 05, 2016 2:42:39 AM net.bounceme.mordor.telnet.PropertiesReader getProps INFO: {wport=3000, port=6789, whost=rainmaker.wunderground.com, host=dune.servint.com} thufir@mordor:~/NetBeansProjects$

What is the gradle way to include a properties file?


thufir@mordor:~/NetBeansProjects$ 
thufir@mordor:~/NetBeansProjects$ tree T/src/
T/src/
β”œβ”€β”€ connection.properties
└── net
    └── bounceme
        └── mordor
            └── telnet
                β”œβ”€β”€ ConsoleReader.java
                β”œβ”€β”€ InputStreamWorker.java
                β”œβ”€β”€ Main.java
                └── PropertiesReader.java

4 directories, 5 files
thufir@mordor:~/NetBeansProjects$ 
thufir@mordor:~/NetBeansProjects$ tree Telnet/src/
Telnet/src/
β”œβ”€β”€ main
β”‚   β”œβ”€β”€ java
β”‚   β”‚   β”œβ”€β”€ connection.properties
β”‚   β”‚   └── net
β”‚   β”‚       └── bounceme
β”‚   β”‚           └── mordor
β”‚   β”‚               └── telnet
β”‚   β”‚                   β”œβ”€β”€ ConsoleReader.java
β”‚   β”‚                   β”œβ”€β”€ InputStreamWorker.java
β”‚   β”‚                   β”œβ”€β”€ Main.java
β”‚   β”‚                   └── PropertiesReader.java
β”‚   └── resources
└── test
    β”œβ”€β”€ java
    └── resources

10 directories, 5 files
thufir@mordor:~/NetBeansProjects$


(Schalk CronjΓ©) #2

I don’t actually know of anything in Gradle that just reads additional properties files, however you can do something like

file("/path/file.properties").withReader { 
   Properties props = new Properties()
   props.load(it)
   project.ext.myProps = props
}

That should keep it isolated enough. You would be able to get to a property via ext.myProps.foo.

For something more powerful, have a look at the Properties plugin.


(Thufir) #3

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.


(Schalk CronjΓ©) #4

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
}

HTH


(Lance Java) #5

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")


(Schalk CronjΓ©) #6

@THUFIR, is this can work for you, it is the best solution.


(Thufir) #7

Yes. solved. Thank you, was just used to doing it differently.