0

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.

Mohsen
  • 4,536
  • 2
  • 27
  • 49
Tika
  • 2,573
  • 2
  • 12
  • 10
  • you can probably control the cli commands via java exec: https://stackoverflow.com/q/3643939/995891 (and hope it doesn't require interactive keyboard input from a terminal). There is no other api afaik – zapl Oct 15 '18 at 00:44
  • Thanks zapl, I could not find an API either, but I think it would be a nice thing to have. Specially when we are generating multiple applications with Jhipster, it is time consuming and difficult to go through the CLI every time. – Tika Oct 15 '18 at 05:20
  • 2
    Why don't you use the JDL to generate your apps? https://www.jhipster.tech/jdl/#applicationdeclaration – Gaël Marziou Oct 15 '18 at 18:56
  • This helped, thanks! I was not aware that I could use the JDL to generate apps, I was using them to only create the entities. @GaëlMarziou – Tika Oct 24 '18 at 00:47

1 Answers1

2

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();
    }
}
Evertude
  • 184
  • 10
  • Thanks Evertude, How do I call Jhipster? as a system command? if so how do I pass the location of the .yo-rc.json file? – Tika Oct 15 '18 at 05:16
  • JHipster is automatically searching for a .yo-rc.json file in the directory from which it is called. Added some sample code for you – Evertude Oct 15 '18 at 10:53