I added CLI support for my java library, how can I conveniently expose this to my library users?

Benny Bottema :

I maintain a Java library which recently added support for CLI commands, but I'm having trouble understanding how to actually expose this support.

How can I provide an easy command cross-platform that works with all the dependencies needed?

During my own testing I have either relied on Junit tests that I run from IntelliJ or on the Maven exec plugin. IntelliJ and Maven manage all the dependencies, but I can't expect my library users to do the same. I'm thinking of providing a .bat file for Windows users and an alias for Linux users, which act as a shortcut for:

java -cp all;the;jars.jar my.package.CliSupportingClass command --option value

Is there a better way?

Benny Bottema :

This article explains it quite well, using appassembler-maven-plugin to assemble all the dependencies and producing a script helper and maven-assembly-plugin to bundle it all as an archive such as zip and tar.

The article uses version 2.4 of one of the plugins, where I updated for the latest 3.1.0. The only difference for our use case is that the <descriptor> tag is now nested in a <descriptors> tag.

Include both plugins either as standard build plugins or under a profile:

<profiles>
    <profile>
        <id>standalone-cli</id>
        <build>
            <plugins>
                <!-- appassembler-maven-plugin -->
                <!-- maven-assembly-plugin -->
            </plugins>
        </build>
    </profile>
</profiles>

The appassembler-maven-plugin collects all dependencies for us and produces a script helper for windows and unix:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.8.1</version>
    <configuration>
        <repositoryLayout>flat</repositoryLayout>
        <repositoryName>lib</repositoryName>
        <showConsoleWindow>true</showConsoleWindow>
        <platforms>
            <platform>unix</platform>
            <platform>windows</platform>
        </platforms>
        <programs>
            <program>
                <mainClass>org.simplejavamail.cli.SimpleJavaMail</mainClass>
                <id>sjm</id>
            </program>
        </programs>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>assemble</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The maven-assembly-plugin then produces the archive(s):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <descriptors>
            <descriptor>src/assembly/standalone-cli-descriptor.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>assemble-all</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Finally standalone-cli-descriptor.xml tells maven-assembly-plugin what should be included in the archives and what type of archives should be produced:

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>standalone-cli</id>
    <formats>
        <format>tar</format>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>LICENSE-2.0.txt</include>
                <include>NOTICE.txt</include>
                <include>RELEASE.txt</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/appassembler</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/**</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=106810&siteId=1