Tika
Tika

Reputation: 2823

Can I create a JHipster application through a java code?

I want to generate a JHipster application programmatically.

Is there an API in Jhipster to which I can pass all the parameters that are required (when I generate the app in the CLI, I pass several parameters one after the other), and get the application generated?

It would be great if I could save the parameters required in a file in somewhere, and then call the Jhipster application generation API and get the application generated.

Upvotes: 0

Views: 295

Answers (1)

Evertude
Evertude

Reputation: 204

Jhipster writes all your chosen settings in the .yo-rc.json file so you could generate it yourself with the desired parameters and then call JHipster - it will detect it and generate everything accordingly

There's probably a better way but this should work for Windows:

public static void main(String[] args) {
    String dir = "path/to/dir";
    String json = "{\n" +
            "  \"generator-jhipster\": {\n" +
                "<your settings>" +
            "  }\n" +
            "}";
    try {
        PrintWriter out = new PrintWriter(dir + ".yo-rc.json");
        out.println(json);
        out.close();
        ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "cd \"" + dir + "\" && jhipster");
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
            System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 2

Related Questions