For my Java project, I have a build.gradle file with:
application {
mainClass = 'com.example.passwordsafe.presentation.gui.MainGui2'
// mainClass = 'com.example.passwordsafe.presentation.terminal.MainTerminal'
}
My project has two user interfaces, terminal and gui. As you can see, at the moment I change the Interface by commenting out one line. I would like to have that the mainClass depends on a config file. I already have a config.properties file for my project. I think of adding a line there like:
# UI: terminal or gui
UI=terminal
How do I need to modify my build.gradle file, so that the mainClass depends on the config file? I would like to use the existing config file, but it would also be possible to create a new one for grade if that is necessary.
Have one main class like such:
class Main {
public static void main(string[] args) {
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream(properties.config)));
String uiType = ""; // or you could specify a default
while((line = br.readLine()) != null) {
String[] parts = line.split("=");
if (parts.length > 1 && parts[0].equals("UI")) {
uiType = parts[1];
}
}
switch(uiType) {
case "Terminal":
new Terminal();
case "GUI":
new GUI();
case "":
throw new RuntimeException("UI not set in config");
}
}
}
Place the properties.config
into your projects resource
directory. Alternatively, read the config from a system path (if you plan to JAR the code later and want the config to still be changable)
Then point your gradle.build
at class Main
. No need to modify the gradle.build
.