Gradle - change mainClass depending on config file - Stack Overflow

admin2025-04-22  0

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.

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.

Share Improve this question asked Jan 29 at 18:25 max23max23 491 silver badge7 bronze badges 1
  • More simply, could you not process the config file in Java and launch the appropriate GUI depending on the config, without the need to change the gradle build file at all? – Kris Rice Commented Jan 30 at 21:18
Add a comment  | 

2 Answers 2

Reset to default 1

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.

You can execute arbitrary code in a Gradle build script so just read from your file and use the property value you get to set your main class:

def config = new Properties()
file('config.properties').withReader { config.load(it) }

application {
    switch(config.UI) {
        case 'terminal':
            mainClass = 'com.example.passwordsafe.presentation.terminal.MainTerminal' 
            break
        case 'gui':
            mainClass = 'com.example.passwordsafe.presentation.gui.MainGui2'
            break
        default:
            throw new RuntimeException("Unknown UI setting '${config.UI}'")
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745284419a294302.html

最新回复(0)