3

I'm trying to create a Java code that creates a nifi customized processor ! so in order to do that I need to use windows cmd windows and launch mvn archetype:generate then choose the modele nifi by typing nifi then choose the project by typing1 the I need to write the groupeId, the artifact ...

enter image description here enter image description here enter image description here

I need to do all that automatically by using a java code : I tryed this code :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class CmdTest {
    public static void main(String[]args) throws IOException {
        String line;
        OutputStream stdin = null;
        InputStream stderr = null;
        InputStream stdout = null;

          // launch EXE and grab stdin/stdout and stderr
        ProcessBuilder builder = new ProcessBuilder(
                "cmd.exe", "/c", "cd \"C:\\users\\eya\\desktop\\javaprocessor\" && mvn archetype:generate");
            builder.redirectErrorStream(true);
            Process process = builder.start();
          stdin = process.getOutputStream ();
          stderr = process.getErrorStream ();
          stdout = process.getInputStream ();

          // "write" the parms into stdin
          line = "nifi" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "1" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "33" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "javaproceSSor" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "javaprocessor" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "javapro" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "y" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          stdin.close();

          // clean up if any output in stdout
          BufferedReader brCleanUp =
            new BufferedReader (new InputStreamReader (stdout));
          while ((line = brCleanUp.readLine ()) != null) {

              System.out.println ("[Stdout] " + line);
          }
          brCleanUp.close();

          // clean up if any output in stderr
          brCleanUp =
            new BufferedReader (new InputStreamReader (stderr));
          while ((line = brCleanUp.readLine ()) != null) {
            System.out.println ("[Stderr] " + line);
          }
          brCleanUp.close();
    }
}

but it only read "nifi" the others no ! and here is the result :

    [Stdout] 2679: remote -> us.fatehi:schemacrawler-archetype-plugin-command (-)
[Stdout] 2680: remote -> us.fatehi:schemacrawler-archetype-plugin-dbconnector (-)
[Stdout] 2681: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
[Stdout] 2682: remote -> ws.osiris:osiris-archetype (Maven Archetype for Osiris)
[Stdout] 2683: remote -> xyz.luan.generator:xyz-gae-generator (-)
[Stdout] 2684: remote -> xyz.luan.generator:xyz-generator (-)
[Stdout] 2685: remote -> za.co.absa.hyperdrive:component-archetype (-)
[Stdout] Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1589: Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 
[Stdout] 1: 1.0-alpha-1
[Stdout] 2: 1.0-alpha-2
[Stdout] 3: 1.0-alpha-3
[Stdout] 4: 1.0-alpha-4
[Stdout] 5: 1.0
[Stdout] 6: 1.1
[Stdout] 7: 1.3
[Stdout] 8: 1.4
[Stdout] Choose a number: 8: Define value for property 'groupId': [INFO] ------------------------------------------------------------------------
[Stdout] [INFO] BUILD FAILURE
[Stdout] [INFO] ------------------------------------------------------------------------
[Stdout] [INFO] Total time:  12.592 s
[Stdout] [INFO] Finished at: 2020-04-12T21:13:20+01:00
[Stdout] [INFO] ------------------------------------------------------------------------
[Stdout] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.1.2:generate (default-cli) on project standalone-pom: null: MojoFailureException: NullPointerException -> [Help 1]
[Stdout] [ERROR] 
[Stdout] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[Stdout] [ERROR] Re-run Maven using the -X switch to enable full debug logging.
[Stdout] [ERROR] 
[Stdout] [ERROR] For more information about the errors and possible solutions, please read the following articles:
[Stdout] [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

if there is any way to generate mvn projects automatically then you are very welcome to help

EDIT: I tried this solution but I didn't know how to configure it

maryem neyli
  • 467
  • 3
  • 20
  • Why are you using Java for this? There are more suitable languages for this kind of scripting. – m0skit0 Apr 12 '20 at 20:52
  • @m0skit0 because this is involved in a bigger project and the first step is to generat processors automatically ! and to do that I need to interact with cmd windows using java – maryem neyli Apr 12 '20 at 20:54
  • Even if it's part of a bigger project you don't have to use Java for everything... It's like using a hammer to lay butter on a toast. – m0skit0 Apr 12 '20 at 20:58

1 Answers1

3

Just don't do that. The mvn command could accept all required arguments in command line so there are no interactive actions required. See the documentation on the plugin and supply parameters accordingly into single command. See http://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html There is an example here: Specify archetype for archetype:generate on command line

kan
  • 28,279
  • 7
  • 71
  • 101
  • Ifound this [solution](https://stackoverflow.com/questions/16541124/generate-maven-archetype-from-java-code) but it does not give me the hand to choose the right filter **nifi** or my prefered archetype – maryem neyli Apr 12 '20 at 21:03
  • Sorry but I did not get your solution :( ! – maryem neyli Apr 12 '20 at 21:40
  • 1
    @maryemneyli You don't have the search feature, instead you put in the right groupId, artifactId and version as parameter. But what is the advantage of first putting "nifi" and then "1" instead of directly using `org.apache.nifi:nifi-processor-bundle-archetype`? – J Fabian Meier Apr 13 '20 at 07:44
  • @JFMeier when I enter this command `>mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=org.apache.nifi:nifi-processor-bundle-archetype -DinteractiveMode=false` an error appear : [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.1.2:generate (default-cli) on project standalone-pom: The desired archetype does not exist (org.apache.maven.archetypes:org.apache.nifi:nifi-processor-bundle-archetype:1.0) -> [Help 1] – maryem neyli Apr 14 '20 at 08:24
  • 1
    You must specify `archetypeArtifactId`, `archetypeGroupId` and `archetypeVersion` separately: http://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html – J Fabian Meier Apr 14 '20 at 08:53
  • `mvn archetype:generate -DarchetypeCatalog=remote -DarchetypeGroupId=org.apache.nifi -DarchetypeArtifactId=nifi-processor-bundle-archetype -DarchetypeVersion=1.9.2 -DgroupId=test -DartifactId=test -DartifactBaseName=test -Dversion=1.0 -Dpackage=test.package.test` is working just fine @JFMeier can you please make it as an answer – maryem neyli Apr 14 '20 at 09:08
  • 1
    @maryemneyli Just accept this one. My comments were just clarifications. – J Fabian Meier Apr 14 '20 at 09:34