简体   繁体   English

创建包含第三方(jar)库的Java库

[英]Creating java library containing 3rd party (jar) libraries

I need to produce a jar file that will be used by others as a library (lets call it 'myLib.jar'), thus the jar does not need to be executable. 我需要生成一个jar文件,该文件将被其他人用作库(我们称其为“ myLib.jar”),因此该jar不需要可执行。 The library depends on some other 3rd-party jars but i will not expect from the ones who use 'myLib.jar' to include them in his/hers Buildpath. 该库依赖于其他一些3rd-party jars,但是我不会期望使用“ myLib.jar”的人将其包含在他/她的Buildpath中。

While searching, i found OneJar and followed the 2 steps at the top of the page. 搜索时,我找到了OneJar,并按照页面顶部的2个步骤进行操作。

1. Download one-jar-appgen-0.97.jar
   Generate application, build, and run it.
   $ java -jar one-jar-appgen-0.97.jar

2. Enter project path (project name is last segment): c:/tmp/test-one-jar
   Enter java package name: com.example.onejar
   $ cd c:/tmp/test-one-jar 
   $ ant 
   $ cd build 
   $ java -jar test-one-jar.jar
   test_one_jar main entry point, args=[]
   test_one_jar main is running
   test_one_jar OK.
   Add source code to the src directory, library jars to the lib directory, and rebuild.

Unfortunately, this did not worked for me since when i tried to include 'myLib.jar' in a new project, i was not able to use the expected classes. 不幸的是,这对我没有用,因为当我尝试在新项目中包含“ myLib.jar”时,我无法使用预期的类。


So... 所以...

Q1 : Is OneJar intended only for executable jars? Q1 :OneJar是否仅用于可执行jar? If not how could i achieve what i have described above. 如果没有,我怎么能达到我上面所描述的。

Q2 : If OneJar is intended only for executables, is there another way to produce the 'myLib.jar' library? 问题2 :如果OneJar仅用于可执行文件,是否还有另一种方法来生成“ myLib.jar”库?


PS: If there is a need to add more information please let me know, in order to edit my quetsion. PS:如果需要添加更多信息,请让我知道,以便编辑我的手势。

All the comments are suggesting to avoid packing the 3rd party libraries into a single jar. 所有评论都建议避免将第三方库打包到单个jar中。 There are several good reasons for this: 几个很好的原因

  1. Packing all 3rd party libraries to one jar, permits the ones who use 'myLib.jar' to have up-to-date versions of the 3rd party libraries. 将所有第三方库打包到一个jar中,可以使使用“ myLib.jar”的用户拥有第三方库的最新版本。
  2. It is forbitten for any other library to use the same 3rd party libraries which are packed in 'myLib.jar' because this will cause classpath conflicts. 禁止任何其他库使用打包在“ myLib.jar”中的相同的第三方库,因为这会导致类路径冲突。
  3. You have to be very careful with the licenses. 您必须非常小心使用许可证。 Every 3rd party library will be probably bounded by a specific license. 每个第三方库都可能受特定许可证的限制。 Many licenses are incompatible and cannot be used together. 许多许可证不兼容,不能一起使用。

So, what did i eventually do? 那么,我最终做了什么?

As @rmlan assured me, there is nothing wrong about notifing to the users of the library to add another library to their classpath. 正如@rmlan向我保证的那样,通知库的用户将另一个库添加到他们的类路径中并没有什么错。 Keeping this in mind, i've created the following folder structure (which is actually what many others do) and zipped it in one compressed file: 牢记这一点,我创建了以下文件夹结构(实际上是许多其他文件夹所做的事情)并将其压缩为一个压缩文件:

myLib
-----bin
----------myLib.jar
-----documentation (mentions 'myLib' dependencies among other stuff)
-----lib (contains the 3rd party libraries)
----------thirdPartyLibrary.jar
----------anotherThirdPartyLibrary.jar
-----source (if it is an open source project)

Finally, in my case i could not use maven or another alternative, but if i could this would make the whole procedure much easier. 最后,在我的情况下,我无法使用Maven或其他替代方法,但是如果可以的话,这将使整个过程变得更加容易。


EDIT: To create 'myLib.jar' I've used eclipse 编辑:为了创建“ myLib.jar”,我使用了eclipse

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

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