简体   繁体   English

Java API在单独的JAR文件中

[英]Java API in separate JAR file

I'm making a Spigot minecraft plugin with an API to allow other developers to hook into it. 我正在制作一个带有API的Spigot minecraft插件,以允许其他开发人员加入它。 How can I create the API in a separate JAR file that will function the same as if it was in the plugin itself? 如何在单独的JAR文件中创建API,其功能与插件本身相同?

I need this because the plugin will not be openly distributed, but I need to allow access to certain methods/classes/objects within. 我需要这个,因为插件不会公开分发,但我需要允许访问其中的某些方法/类/对象。

Desired outcome: Plugin.jar is running on the server. 期望的结果:Plugin.jar正在服务器上运行。 I have Plugin-API.jar sitting in a Maven repository for other developers to hook into that will allow interaction with plugin.jar. 我将Plugin-API.jar放在Maven存储库中,供其他开发人员使用,以便与plugin.jar进行交互。

I'm fairly new to creating my own APIs in general, so fully explained responses would be greatly appreciated. 我对创建自己的API一般都很陌生,因此非常感谢完全解释的响应。 Thanks! 谢谢!

Review 评论

Allow me to rehash your question. 请允许我重新提出你的问题。 You have a plugin - Special - whose distribution is limited/restricted for one reason or another). 你有一个插件 - 特别 - 由于某种原因而限制/限制其分发。 Instead, you want to provide a publicly available API - SpecialApi - to allow other developers to incorporate and/or use features provided by Special . 相反,您希望提供公开可用的API - SpecialApi - 以允许其他开发人员合并和/或使用Special提供的功能。

This is a common approach used by various plugins (eg Vault, Citizens2, PermissionsEx, etc.), though for different reasons than yours. 这是各种插件(例如Vault,Citizens2,PermissionsEx等)使用的常用方法,但原因不同于您的。 The structural relationship of which is represented in the following component diagram. 其结构关系在以下组件图中表示。

在此输入图像描述

Given that you mentioned submodules in the comments, let me digress for a moment as to why they may not be a good solution. 鉴于你在评论中提到了子模块,让我先谈谈为什么它们可能不是一个好的解决方案。 A common use of submodules is to create a segregation of code based on some criteria. 子模块的一个常见用途是根据某些标准创建代码隔离。 For example, a web application may be split functionally into modules for its GUI, business logic, data persistence, etc; 例如,Web应用程序可以在功能上分成用于其GUI,业务逻辑,数据持久性等的模块; whereas a “big data” service may have multiple modules segregating libraries for various techniques. 而“大数据”服务可能具有多个模块,用于分离各种技术的库。 A good example of modules in terms of plugins is PermissionsEx, which is split to support different target environments. 关于插件的模块的一个很好的例子是PermissionsEx,它被拆分以支持不同的目标环境。 It has one module containing common code; 它有一个包含公共代码的模块; another module wrapping its core as a Bukkit plugin; 另一个模块将其核心包装为Bukkit插件; and third module wrapping its core as a Sponge plugin. 和第三个模块将其核心包装为Sponge插件。

Answer 回答

Since your primary purpose is code isolation via an API, the easiest and most direct approach is to create two separate Maven projects – one for your plugin and one for your API. 由于您的主要目的是通过API进行代码隔离,因此最简单,最直接的方法是创建两个单独的Maven项目 - 一个用于您的插件,另一个用于您的API。

SpecialApi Project SpecialApi项目

APIs should not be taken lightly. API不应掉以轻心。 This may make more sense if you think of an API as a contract between you and developers that guarantees certain functionality. 如果您将API视为保证某些功能的开发人员之间的合同,这可能会更有意义。 In short, keep these points in mind: 简而言之,请牢记以下几点:

  • Once released, an API is a commitment 一旦发布,API就是一种承诺
  • You should never remove functionality from an API 您永远不应该从API中删除功能
  • You can always add functionality to an API 您始终可以向API添加功能
  • You can change underlying code to your heart's content provided it continues to implement the API 如果继续实施API,您可以将基础代码更改为您的内容

Your API project will define classes, interfaces, and data structures required to interface with your plugin. 您的API项目将定义与插件接口所需的类,接口和数据结构。 Do not make the mistake of making the API a plugin itself. 不要错误地将API作为插件本身。 You can certainly use SpigotAPI classes and interfaces, even to the point of defining a plugin class; 您当然可以使用SpigotAPI类和接口,甚至可以定义插件类; however, it should never have to be loaded by Spigot. 但是,它永远不应该由Spigot加载。 It is just a JAR that will be referenced as a provided dependency by others but also deployed as part of your actual plugin. 它只是一个JAR,它将作为其他人provided依赖项引用,但也作为实际插件的一部分进行部署。

How you provide the API JAR is up to you. 您如何提供API JAR取决于您。 If you have some spare space on a public web server, you can manually create a small repository without having to consider using a service such as Nexus, Artifactory, etc. 如果公共Web服务器上有一些备用空间,则可以手动创建一个小型存储库,而无需考虑使用Nexus,Artifactory等服务。

Special Plugin Project 特别插件项目

The plugin project contains everything else. 插件项目包含其他所有内容。 You are free to add, change, correct, or remove internal components, provided it does not affect the API. 您可以自由添加,更改,更正或删除内部组件,前提是它不会影响API。 It is a regular and normal plugin except that in addition to its resources, classes, and data normally provided, it also includes the same for your API; 它是一个常规和普通的插件,除了它通常提供的资源,类和数据之外,它还包括相同的API; often referred to as a fat JAR . 通常被称为胖JAR Otherwise, you would have to load your API separately as a plugin or as an additional entry on the server's classpath. 否则,您必须将API作为插件单独加载,或者作为服务器类路径上的附加条目加载。 These last two options just beg for version and configuration management issues. 最后两个选项只是讨论版本和配置管理问题。

Instead, you use maven-assembly-plugin or similar tools to deliver plugin and API code in the same JAR. 相反,您使用maven-assembly-plugin或类似工具在同一个JAR中提供插件和API代码。 For example, this assembly configuration will combine any project dependency with a scope of compile into a single JAR. 例如,此程序集配置将任何项目依赖项与compile范围组合到单个JAR中。

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4.1</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase> 
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

For anything more complex than this, you will want to look into maven-shade-plugin . 对于比这更复杂的东西,你会想要看看maven-shade-plugin

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM