簡體   English   中英

我為我的java庫添加了CLI支持,如何方便地將其公開給我的庫用戶?

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

我維護了一個Java庫,最近添加了對CLI命令的支持,但是我無法理解如何實際公開這種支持。

如何提供一個簡單的命令跨平台,適用於所需的所有依賴項?

在我自己的測試過程中,我要么依賴於我從IntelliJ運行的Junit測試,要么依賴於Maven exec插件。 IntelliJ和Maven管理所有依賴項,但我不能指望我的庫用戶也這樣做。 我正在考慮為Windows用戶提供一個.bat文件,為Linux用戶提供一個別名 ,它可以作為以下的快捷方式:

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

有沒有更好的辦法?

本文很好地解釋了這一點,使用appassembler-maven-plugin來組裝所有依賴項並生成腳本助手和maven-assembly-plugin,將它們作為zip和tar等存檔捆綁在一起。

本文使用其中一個插件的2.4版本,我更新了最新的3.1.0。 我們的用例的唯一區別是<descriptor>標簽現在嵌套在<descriptors>標簽中。

將這兩個插件作為標准構建插件或在配置文件下包含:

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

appassembler-maven-plugin為我們收集所有依賴項,並為windows和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>

然后maven-assembly-plugin生成存檔:

<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>

最后, standalone-cli-descriptor.xml告訴maven-assembly-plugin應該包含在檔案中的內容以及應該生成什么類型的檔案

<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>

Gradle有一個application插件 ,可以創建分發zip文件。 您的用戶可以解壓縮它以進行安裝。 它具有lib文件夾和Windows bat文件以及* nix shell腳本中的所有依賴項,以便在bin文件夾中運行應用程序。

我相信Maven也有類似的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM