简体   繁体   English

构建包含其所有依赖项的JAR

[英]Building JAR that includes all its dependencies

This is probably a really fundamental question, but I'm afraid I don't know much about Java and I couldn't find the answer anywhere. 这可能是一个非常基本的问题,但我担心我对Java知之甚少,而且无法在任何地方找到答案。

I'm attempting to build an Ant library which depends on the TFS SDK. 我正在尝试构建一个依赖于TFS SDK的Ant库。 I followed the guide to setting up a project, but when I export it as a JAR and try to run a task using ANT I get the following error: 我按照指南来设置项目,但当我将其导出为JAR并尝试使用ANT运行任务时,我收到以下错误:

java.lang.NoClassDefFoundError: /com/microsoft/tfs/core/util/TFSUser

I realise I could put the TFS SDK JAR in my ANT lib folder, but if possible I'd like my JAR to include it and the library just work without having to do so. 我意识到我可以将TFS SDK JAR放在我的ANT lib文件夹中,但是如果可能的话,我希望我的JAR能够包含它并且库只需要工作而不必这样做。

This answer seems to say it's possible to include all the resources needed to run using Eclipse (I'm using 3.7.2) but it doesn't detail how to actually do it. 这个答案似乎说可以包含使用Eclipse运行所需的所有资源(我正在使用3.7.2),但它没有详细说明如何实际执行它。 What is the option in Eclipse to do so? Eclipse中有哪些选项可以做到这一点?

Select "Extract required libraries into generated JAR" as you do the export. 在导出时选择“将所需库提取到生成的JAR”。

将所需的库提取到生成的JAR中

Select "Extract required libraries into generated JAR" as you do the export. 在导出时选择“将所需库提取到生成的JAR”。

Use File -> Export -> Java -> Runnable JAR file instead from Eclipse. 使用File - > Export - > Java - > Runnable JAR文件,而不是Eclipse。

The "Extract required libraries into generated JAR" should be what you need. “将所需的库提取到生成的JAR中”应该是您所需要的。

When you build a jar you get a JAR containing just your code, and not any dependencies your Jar requires. 当你构建一个jar时,你会得到一个只包含你的代码的JAR,而不是你的Jar需要的任何依赖项。 You could use something like jarjar to combine all the dependencies into one easy to manage Jar file or copy all the depend JARs into a folder for ease of use. 您可以使用jarjar之类的东西将所有依赖项合并到一个易于管理的Jar文件中,或者将所有依赖JAR复制到一个文件夹中以便于使用。 It looks like Eclipse has options to also do this kind of thing (see posts above). 看起来Eclipse也有选择来做这种事情(见上面的帖子)。

The other option would be to use a dependency management system such as Maven or Ivy . 另一种选择是使用依赖管理系统,如MavenIvy This has a higher learning curve, but for a library it is worthwhile as it will allow users of your library to easy grab all the dependencies. 这有一个更高的学习曲线,但对于一个库来说它是值得的,因为它将允许您的库的用户轻松获取所有依赖项。 For an end user application then a single distributable is likely a better option (for which you could use Maven or Ivy to internally manage the dependencies and then something like jarjar or Java Web Start to distribute to your end users). 对于最终用户应用程序,单个可分发可能是更好的选择(您可以使用Maven或Ivy在内部管理依赖项,然后使用jarjar或Java Web Start分发给最终用户)。

Just in case if you're doing with maven. 以防你是否正在使用maven。 You need to include following plugin. 您需要包含以下插件。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <properties>
            <property>
                <name>listener</name>
                <value>com.example.TestProgressListener</value>
            </property>
        </properties>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Read more @ how do I build JAR file with dependencies? 阅读更多@ 如何构建具有依赖关系的JAR文件?

You would need to depend on classpath attribute in manifest file. 您需要依赖清单文件中的classpath属性。 This explained well at How to package libraries into my jar using Ant 这解释了如何使用Ant将库打包到我的jar中

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

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