如何将多个罐结合成一个? [英] How to Combine Multiple Jars into One?

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

问题描述

我已经阅读了很多文章/解释,花了太多时间,但一切都太广泛或具体了。



这个问题真的只适用于我做了一个小程序它包含一个类,并需要另外两个Jar库。我把这些包括在项目中(多个项目,因为我在Netbeans和Eclipse中尝试过这个),很容易重新创建一个Class项目)。所有这些都是我的HTML / Web项目不应该处理多个Jar或引用它们。这不是一个复杂的小程序/项目。



最初,我在Netbeans中。它有主包,添加2个jar后,它们放在Libraries区域(resources文件夹)中。在构建它之后,Netbeans为我的一个包/类创建一个Jar,然后将另外两个库放在一个lib目录下。我想让他们在一个可分发的罐子里。从我看过的许多事情中,我已经尝试将以下内容放在build.xml中:

 <目标名称=YourBigJardepends = -  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中部署所有的jar 得到了这一点。我不知道/了解如何使用build.xml,所以我不会惊讶,如果出现问题(显然),但我没有错误/警告消息。



当我在Eclipse中使用它时,它有效地结合了它们,但是当我在实际的Web项目中使用Jar时,它说它找不到其他2个Jars中的类。我不明白如何解决它,但它是一个Classpath问题?在Eclipse中,我创建一个名为lib的新目录,并将Jars放在其中。然后,我右键单击项目,转到Java构建路径,然后添加Jars,并在订单和导出选项卡下进行检查。从我读过的东西中,我右键单击该项目,选择导出,选择Jar,取消选中.classpath和.project文件,只检查导出生成的类文件和资源,然后允许它生成清单文件。就像我说的那样,这会产生一个Jar,但它的内容是我的包,然后是一个lib目录,其中有另外两个jar。清单在那里,但它很空,不引用任何文件(不确定是否重要)。当我把它放在我的网络应用程序中,它表示该小程序找不到其他类。



它看起来很简单 - 一个包/类,两个外部jar ...组合成一个JAR,当作为小应用程序分发时构建。任何想法?



更新:



由于我们开始使用Maven, X-所以我们最终创建了一个新的项目来容纳applet(因为它被用于多个项目),并且在我们的 pom.xml 中使用这个,最后:

 < 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>
<执行>
< 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>
<执行>
< 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>

这是很好的,因为它允许我们使用我们的代码签名证书自动签名。 / p>

解决方案

首先,让我说,真的没有理由结合罐子 - 你可以使用你的类部署一个小程序文件(不在一个jar中)及其单独的依赖jar。



但是,如果你觉得你必须从你的类和它的所有依赖项中创建一个大的jar,那么你可以这样做。



您不能将jar文件放入另一个jar文件,但是 - jar文件不会这样工作。
你需要的是所有的jar文件的所有内容被提取,然后再压缩成一个jar。



如果你'我会建议您使用 Maven 构建工具来完成此操作。
使用Maven设置构建后,您可以将 Maven程序集插件配置为为您构建一个大的jar。



我知道Eclipse支持Maven,我认为NetBeans也是如此。


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

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.

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>

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.

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.

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

UPDATE:

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.

解决方案

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.

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.

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.

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

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

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