Java – Maven and installation command line programs

Maven and installation command line programs… here is a solution to the problem.

Maven and installation command line programs

What is the recommended way to write a command-line program in Java that uses Maven as a build system, from there to use the program as a command?

Suppose the program is named Foo. In the foo directory, I can run the mvn package to generate target/foo-1.0-SNAPSHOT .jar, which can then be used with java -cp target/foo-1.0-SNAPSHOT.jar foo. Main %* But the command is too long for the user to type. I need to understand the point where typing foo on the command line will run the program. MVN install no; It simply copies the jar to the local maven repository.

What is the recommended way to provide the program as a command?

Solution

You can use Maven Assembler Plugin like this:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.10</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <programs>
                    <program>
                        <mainClass>sandbox. Main</mainClass>
                        <id>app</id>
                    </program>
                </programs>
            </configuration>
        </plugin>

Running the MVN package will generate a Windows (.bat) and Unix shell script in the bin folder of the ${project.build.directory}/appassembler subfolder

Related Problems and Solutions