如何将多个罐子合二为一? [英] How to Combine Multiple Jars into One?

查看:33
本文介绍了如何将多个罐子合二为一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于此的文章/解释并花费了太多时间,但所有内容要么过于宽泛,要么过于具体.

I've read so many articles/explanations on this and spent too many hours, but everything is either too broad or specific.

这个问题真的只适用于我制作的小程序.它包含一个类,并需要 2 个其他 Jar 库.我已经将这些包含在项目中(多个项目,因为我已经在 Netbeans 和 Eclipse 中尝试过这个......重新创建一个类项目很容易).这一切的重点是我的 HTML/web 项目不应该处理多个 Jar 或引用它们.它也不是一个复杂的小程序/项目.

This question really only applies to an Applet I've made. It contains one Class, and requires 2 other Jar libraries. I've included these in the projects (multiple projects, because I've tried this in Netbeans and Eclipse...it's easy to recreate a one Class project). The point of all this is that my HTML/web project shouldn't have to deal with more than one Jar or reference them. It's not a complicated applet/project at all either.

最初,我是在 Netbeans 中制作的.它有主包,在添加 2 个 Jars 后,它们被放置在库"区域(资源"文件夹)中.构建后,Netbeans 为我的一个包/类创建一个 Jar,然后将其他 2 个库放在它旁边的lib"目录中.我希望它们在一个可分发的 Jar 中.从我看过的很多东西来看,我尝试在 build.xml 中加入以下内容:

Originally, I made it in Netbeans. It has the main package, and after adding the 2 Jars, they are put in a "Libraries" area (the "resources" folder). After building it, Netbeans creates a Jar for my one package/class, and then puts the 2 other libraries in a "lib" directory next to it. I'd like them to be in one, distributable Jar. From the many things I've looked through, I've tried putting the following in build.xml:

<target name="YourBigJar" depends="-post-jar">
  <jar destfile="dist/BigJar.jar">
    <zipfileset src="dist/cxapplet.jar"/>
    <zipfileset src="resources/dpotapi.jar"/>
    <zipfileset src="resources/dpotjni.jar"/>
  </jar>
</target>

但它什么也没产生.我从 NetBeans - 全部部署在一个 jar 中 得到了这个.我不知道/理解如何使用 build.xml,所以如果出现问题(显然)我不会感到惊讶,但我没有收到错误/警告消息.

But it produces nothing. I got this from NetBeans - deploying all in one jar . I don't know/understand how to use build.xml, so I wouldn't be surprised if something's going wrong (obviously), but I get no error/warning messages.

当我在 Eclipse 中创建它时,它有效地组合了它们,但是当我在我的实际 Web 项目中使用 Jar 时,它说它无法从其他 2 个 Jar 中找到类.我不明白如何修复它,但这是一个类路径问题吗?在 Eclipse 中,我创建了一个名为lib"的新目录并将 Jars 放入其中.然后,我右键单击该项目,转到Java 构建路径",并添加 Jars,同时在Order and Export"选项卡下检查它们.根据我读过的内容,我右键单击项目,选择导出",选择Jar",取消选中.classpath"和.project"文件,只选中导出生成的类文件和资源",然后允许它生成清单文件.就像我说的,这会生成一个 Jar,但它的内容是我的包,然后是一个包含另外两个 Jar 的lib"目录.清单在那里,但它很空,并且没有引用任何文件(不确定这是否重要).当我把它放在我的网络应用程序中时,它说小程序找不到其他类.

When I made it in Eclipse, it effectively combines them, but when I use the Jar in my actual web project, it says it cannot find the classes from the other 2 Jars. I wouldn't understand how to fix it, but is it a Classpath problem? In Eclipse, I make a new directory called "lib" and put the Jars in it. Then, I right-click the project, go to "Java Build Path", and add the Jars, also checking them under the "Order and Export" tab. From things I've read, I right-click the project, choose "Export", choose "Jar", uncheck the ".classpath" and ".project" files, only check "Export generated class files and resources", and then allow it to generate the manifest file. Like I said, this generates one Jar, but its contents are my package and then a "lib" directory that has the 2 other Jars. The Manifest is there, but it's pretty empty and doesn't reference any files (not sure if that's important). When I put it in my web app, it says the applet can't find the other classes.

它看起来很简单 - 一个包/类,两个外部 Jar...当构建为作为小程序分发时组合成一个 Jar.有什么想法吗?

It just seems so simple - one package/class, two external Jars...combine into one Jar when built for distribution as an applet. Any ideas?

更新:

自从我们开始使用 Maven 以来,有人研究使用 Maven 插件.所以我们最终创建了一个新项目来容纳小程序(因为它在多个项目中使用),并在我们的 pom.xml 中使用它,最后:

Since we started using Maven, someone looked into using a Maven plugin. So we ended up creating a new project to house the applet (since it's used across multiple projects), and used this in our pom.xml, in the end:

<build>
  <resources>
    <resource>
      <directory>${basedir}/applet</directory>
      <excludes>
        <exclude>codesignstore</exclude>
      </excludes>
    </resource>
    <resource>
      <directory>${basedir}/src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.2-beta-5</version>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <finalName>cxapplet</finalName>
        <archive>
          <index>true</index>
          <manifest>
            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
          </manifest>
        </archive>
        <appendAssemblyId>false</appendAssemblyId>
      </configuration>
      <executions>
        <execution>
          <id>make-my-applet-jar</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jarsigner-plugin</artifactId>
      <version>1.2</version>
      <executions>
        <execution>
          <id>sign</id>
          <goals>
            <goal>sign</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <keystore>${basedir}/applet/codesignstore</keystore>
        <alias>codesigncert</alias>
        <storepass>HIDDEN</storepass>
        <keypass>HIDDEN</keypass>
      </configuration>
    </plugin>
  </plugins>
</build>

这很好,因为它允许我们使用我们的代码签名证书来自动签名.

And it was nice because it allowed us to use our Code Signing Certificate to automatically sign it too.

推荐答案

首先,让我说真的没有理由组合 jars - 你可以使用你的类文件(而不是在 jar 中)部署小程序及其独立的依赖 jar.

First of all, let me say that there really is no reason to combine the jars - you can deploy an applet using your class file (not in a jar) and its separate dependent jars.

但是,如果您觉得必须从类及其所有依赖项中制作一个大罐子,那么您可以这样做.

However, if you feel that you must make one big jar out of your class and all of its dependencies, then you can do so.

您不能将 jar 文件放入另一个 jar 文件中,但是 - jar 文件不能那样工作.您需要的是将所有 jar 文件的所有内容都提取出来,然后再次压缩到一个 jar 中.

You can't put jar files into another jar file, however - jar files don't work that way. What you need is for all of the contents of all of the jar files to be extracted, and then zipped up again into one jar.

如果您打算这样做,我建议您使用 Maven 构建工具来执行此操作.使用 Maven 设置构建后,您可以配置 Maven 程序集插件 为你造一个大罐子.

If you're going to do this, I would suggest using the Maven build tool to do it. Once you set up your build with Maven, you can configure the Maven Assembly Plugin to build one big jar for you.

我知道 Eclipse 支持 Maven,我认为 NetBeans 也支持.

I know that Eclipse supports Maven, and I assume that NetBeans does as well.

这篇关于如何将多个罐子合二为一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆