How to build a CLI from a Maven project like mvn, gradle, etc.?

For example, I have a Maven project called blabla, and I want to manage it using the CLI, like mvn (Maven) or gradle. (Examples, $ mvn clean install, $ gradle initetc.).

And instead of starting

$ java BlaBla start 

or

$ java BlaBla stop

etc.

I want to do like this:

$ blabla start
$ blabla status
$ blabla stop

and etc.

Is there a turnkey solution for maven projects?

+4
source share
3 answers

You can look at the appassembler-maven-plugin , which can create a script based on the name and supports shell and bat options ...

+6
source

For Maven, look at the Maven Exec Plugin , you can do something like:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            ...
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.mycompany.BlaBla</mainClass>
          <arguments>
            <argument>start</argument>
            ...
          </arguments>
          <systemProperties>
            <systemProperty>
              <key>myproperty</key>
              <value>myvalue</value>
            </systemProperty>
            ...
          </systemProperties>
        </configuration>
      </plugin>
    </plugins>
  </build>
   ...
</project>

Then you can run something like mvn exec:java

Gradle - Exec Task.

, , bash script , . Maven Gradle - , - , , ( ).

+1

Apache Commons CLI. , OptionLists (, ..), .

 package CLi;

   import org.apache.commons.cli.CommandLine;
   import org.apache.commons.cli.CommandLineParser;
   import org.apache.commons.cli.DefaultParser;
   import org.apache.commons.cli.HelpFormatter;
   import org.apache.commons.cli.Options;
   import org.apache.commons.cli.ParseException;
   import org.apache.poi.EncryptedDocumentException;
   import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

 public class CLi 
   {
 /** 
        * @see http://www.thinkplexx.com/blog/simple-apache-commons-cli-example-            java-command-line-arguments-parsing
        * @see http://stackoverflow.com/a/10798945
        * @see http://stackoverflow.com/questions/36166630/apache-commons-cli-   not-parsing-as-expected
        * 
        * Link to Java-Library:
        * @see https://commons.apache.org/proper/commons-cli/   
        */

   /**
    * Variables
    */
   private String[] args;
   private Options options = new Options();
   private HelpFormatter formater;
   private CommandLineParser parser;
   private CommandLine cmd;
   private Scanner console; 
   private String cmdLine; 
   private boolean noParam;

   /**
    * Constructor.
    * @param args - The command-line arguments.
   */
   public CLi(String[] args) 
   {
       this.args = args;
       buildCLIOptions();
   }

  public void parse() throws Exception
  {
    if(args.length == 0)
    {
        noParam = true;
        args = scanParams();
    }
    else
    {
        try 
        {
            parser = new DefaultParser();

            cmd = null;
            cmd = parser.parse(options, args);      

            /**
             * Print help.
             */
            if (cmd.hasOption("help"))
            {
                noParam = false;
                help();
            }

            /**
             * Shutdown.
             */
            if (cmd.hasOption("exit")) 
            {
                noParam = false;
                System.exit(0);     
            }
        }
    }
}

/**
 * @throws Exception 
 */
public void help() throws Exception
{
    /**
     * This prints out some help
     */
    formater = new HelpFormatter();
    formater.printHelp("Main", options);
    System.out.println(" ");
    noParam = true;
    scanParams();
}

/**
 * Scan params.
 * @return the Parameters
 * @throws Exception 
 */
public String[] scanParams() throws Exception 
{
    System.out.println("Ready... Waiting for input...:");
    System.out.println(" ");

    console = new Scanner(System.in);

    while(noParam)
    {
        cmdLine = console.nextLine();

        args = cmdLine.replace(" -", " #-").split(" #");

        parse();
    }

    console.close();

    return args;
}

public void buildCLIOptions()
{
    options.addOption("exit", "exit", false, "Exiting application.");
    options.addOption("help", "help", false, "show help.");
}
}

:

public class Main
{       
    public static void main(String[] args) throws Exception 
    {
        CLi console = new CLi(args);
        console.parse();
    }
}
+1

Source: https://habr.com/ru/post/1688262/


All Articles