简体   繁体   English

我为我的java库添加了CLI支持,如何方便地将其公开给我的库用户?

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

I maintain a Java library which recently added support for CLI commands, but I'm having trouble understanding how to actually expose this support. 我维护了一个Java库,最近添加了对CLI命令的支持,但是我无法理解如何实际公开这种支持。

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运行的Junit测试,要么依赖于Maven exec插件。 IntelliJ and Maven manage all the dependencies, but I can't expect my library users to do the same. IntelliJ和Maven管理所有依赖项,但我不能指望我的库用户也这样做。 I'm thinking of providing a .bat file for Windows users and an alias for Linux users, which act as a shortcut for: 我正在考虑为Windows用户提供一个.bat文件,为Linux用户提供一个别名 ,它可以作为以下的快捷方式:

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

Is there a better way? 有没有更好的办法?

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. 本文很好地解释了这一点,使用appassembler-maven-plugin来组装所有依赖项并生成脚本助手和maven-assembly-plugin,将它们作为zip和tar等存档捆绑在一起。

The article uses version 2.4 of one of the plugins, where I updated for the latest 3.1.0. 本文使用其中一个插件的2.4版本,我更新了最新的3.1.0。 The only difference for our use case is that the <descriptor> tag is now nested in a <descriptors> tag. 我们的用例的唯一区别是<descriptor>标签现在嵌套在<descriptors>标签中。

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

The maven-assembly-plugin then produces the archive(s): 然后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>

Finally standalone-cli-descriptor.xml tells maven-assembly-plugin what should be included in the archives and what type of archives should be produced: 最后, 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 has an application plugin that creates a distribution zip file. Gradle有一个application插件 ,可以创建分发zip文件。 Your users can unzip this to install it. 您的用户可以解压缩它以进行安装。 It has all the dependencies in a lib folder and Windows bat files and *nix shell scripts to run the application in a bin folder. 它具有lib文件夹和Windows bat文件以及* nix shell脚本中的所有依赖项,以便在bin文件夹中运行应用程序。

I believe Maven has something similar. 我相信Maven也有类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使Java CLI解析库Jopt Simple解析参数而没有任何选择? - How do I make my Java CLI parsing library Jopt Simple parse an argument without any option? 如何在我的电脑中找到 android.support.v7 库的位置? - How can I find the location of android.support.v7 library in my computer? 如何构建我的jar文件,以便使用该库的用户能够在Eclipse中看到javadoc - How can I build my jar file so that users who use the library will be able to see the javadoc in Eclipse 我可以使用Jython将Java库与我的Python代码链接吗 - Can I link a Java library using Jython with my code in Python 如何在我的Java项目中完全包含Batik库? - How do I fully include the Batik library in my Java project? Netbeans我添加的库JAR /文件夹在哪里? - Netbeans Where is my Library JAR/Folder that I have added 如何为我的Java应用程序提供目标库? - How do I make a target library available to my Java app? 如何在Java应用程序中使用Python库? - How do I use a Python library in my Java application? 我直接在src文件夹中添加了一个库,Eclipse似乎可以正确编译,但是找不到类文件? - I added a library directly to my src folder, Eclipse seems to be compiling properly, but can't find the class file? 如何仅将我的 Java 库中的异常传递给实施应用程序? - How can I pass only Exceptions from my Java Library to the implementing application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM