4

For instance I have a Maven project called blabla, and I want to control it by CLI like mvn (Maven) or gradle.(Examples, $ mvn clean install, $ gradle init etc).

And instead of launching

$ java BlaBla start 

or

$ java BlaBla stop

etc

I want to do like this:

$ blabla start
$ blabla status
$ blabla stop

etc.

Is there any ready solution for maven projects?

Evanda
  • 83
  • 4

3 Answers3

6

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

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
1

For Maven, take a 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>

You can then run something like mvn exec:java

There is something similar with Gradle via the Exec Task.

Though I don't know the specific use case related to your question, it may be a better idea to use a bash script or another way specifically designed to run software. Maven and Gradle are build automation tools, their primary goal is not to run your program but compile your code, run tests and create artifact (among other things).

Pierre B.
  • 11,612
  • 1
  • 37
  • 58
1

Use the library Apache Commons CLI. It's a library there you can build OptionLists (arguments, parameters, etc.) like this.

 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.");
}
}

In the Main class do:

public class Main
{       
    public static void main(String[] args) throws Exception 
    {
        CLi console = new CLi(args);
        console.parse();
    }
}
MHDx
  • 51
  • 5
  • Very useful library, thanks. But I will need anyway bash script for launching from command line as I want. and I decided to create it. – Evanda Oct 27 '17 at 09:24