简体   繁体   English

Java - 创建一个库并导入可选的

[英]Java - make a library and import optional

I have a library that I'm using in an Java application - it's important for certain functionality, but it's optional. 我有一个我在Java应用程序中使用的库 - 它对于某些功能很重要,但它是可选的。 Meaning that if the JAR file is not there, the program continues on without issue. 这意味着如果JAR文件不存在,程序将继续运行而不会出现问题。 I'd like to open source my program, but I can not include this library, which is necessary to compile the source code as I have numerous import statements to use the API. 我想开源我的程序,但我不能包含这个库,这是编译源代码所必需的,因为我有许多import语句来使用API​​。 I don't want to maintain two code sets. 我不想维护两个代码集。 What is the best way to remove the physical jar file from open source release, but still maintain the code to support it where other people could still compile it? 从开源版本中删除物理jar文件的最佳方法是什么,但是仍然维护代码以支持其他人仍然可以编译的文件?

the typical approach taken is to define the wrapper API (ie interfaces) and include those interfaces in the open sourced code, and then provide configuration options where one can specify class names of classes that implement certain interfaces. 采用的典型方法是定义包装器API(即接口)并在开源代码中包含这些接口,然后提供配置选项,其中可以指定实现某些接口的类的类名。

You will import API interfaces instead of importing classes directly into your open sourced code. 您将导入API接口,而不是直接将类导入到开源代码中。 This way, you are open sourcing the API but not the implementation of the parts that you do not want to open source or you cannot open source. 这样,您就是开源API而不是您不想开源的部分的实现,或者您无法开源。

There are many examples, but take a look at JDBC API (interfaces) and JDBC drivers (implementation classes) for starters. 有很多示例,但请看一下初学者的JDBC API(接口)和JDBC驱动程序(实现类)。

I was pretty much typing the same thing as smallworld with one addition. 我几乎和smallworld打了一个相同的东西。 If this API were necessary you can use a project build tool like Maven to handle the dependencies on you project. 如果需要此API,您可以使用Maven等项目构建工具来处理项目的依赖项。 If someone checks it out from source control with the pom they can download the dependencies for themselves and you don't have to include them in a source repo. 如果有人使用pom从源代码控制中检出它们,他们可以为自己下载依赖项,而不必将它们包含在源代码库中。

There's probably a number of ways to fix this, here's a couple I can think of: 可能有很多方法可以解决这个问题,这里有一些我能想到的:

  • If you have only a couple of methods you need to invoke in the 3rd party library, you could use reflection to invoke those methods. 如果您只需要在第三方库中调用几种方法,则可以使用反射来调用这些方法。 It creates really verbose code, that is hard to read though. 它创建了非常冗长的代码,但很难阅读。

  • If you don't have too much of the API in the 3rd party library you use, you could also create a separate JAR file, containing just a non-functional shell of the classes in the library (just types with the same names and methods with the same signatures). 如果你在第三方库中没有太多的API,你也可以创建一个单独的JAR文件,只包含库中类的非功能shell(只是具有相同名称和方法的类型)具有相同的签名)。 You can then use this JAR to distribute and compile against. 然后,您可以使用此JAR进行分发和编译。 At run-time you'd replace it with the real JAR if available. 在运行时,如果可用的话,你可以用真正的JAR替换它。

  • The most common way is probably to just create a wrapper API in a separate module/project for the code that is dependent on the 3rd party library, and possibly distribute a pre-built JAR. 最常见的方法可能是在单独的模块/项目中为依赖于第三方库的代码创建包装器API,并可能分发预构建的JAR。 This might go against your wish to not maintain two code sets, but may prove to be the best and less painful solution in the long run. 这可能违背了您不希望维护两个代码集的愿望,但从长远来看可能是最好的,也不那么痛苦的解决方案。

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

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