Nice programing

릴리스를 하나의 JAR 파일로 병합하는 가장 쉬운 방법

nicepro 2020. 10. 31. 10:08
반응형

릴리스를 하나의 JAR 파일로 병합하는 가장 쉬운 방법


여러 JAR 파일을 하나 JAR 파일로 쉽게 병합하는 도구 또는 스크립트가 있습니까? 보너스는 메인 파일 매니페스트를 쉽게 설정하고 실행 가능하게 만드는 것입니다.

구체적인 사례는 Java 재구성 된 텍스트 도구 입니다. 다음과 같이 실행하고 싶습니다.

java -jar rst.jar

내가 말할 수있는 한, 쉬운 단일 파일 도구가 아니어야한다는 것을 나타내는 종속성이 없지만 다운로드 한 ZIP 파일에는 많은 라이브러리가 포함되어 있습니다.

      0  11-30-07 10:01   jrst-0.8.1/
    922  11-30-07 09:53   jrst-0.8.1/jrst.bat
    898  11-30-07 09:53   jrst-0.8.1/jrst.sh
   2675  11-30-07 09:42   jrst-0.8.1/readmeEN.txt
 108821  11-30-07 09:59   jrst-0.8.1/jrst-0.8.1.jar
   2675  11-30-07 09:42   jrst-0.8.1/readme.txt
      0  11-30-07 10:01   jrst-0.8.1/lib/
  81508  11-30-07 09:49   jrst-0.8.1/lib/batik-util-1.6-1.jar
2450757  11-30-07 09:49   jrst-0.8.1/lib/icu4j-2.6.1.jar
 559366  11-30-07 09:49   jrst-0.8.1/lib/commons-collections-3.1.jar
  83613  11-30-07 09:49   jrst-0.8.1/lib/commons-io-1.3.1.jar
 207723  11-30-07 09:49   jrst-0.8.1/lib/commons-lang-2.1.jar
  52915  11-30-07 09:49   jrst-0.8.1/lib/commons-logging-1.1.jar
 260172  11-30-07 09:49   jrst-0.8.1/lib/commons-primitives-1.0.jar
 313898  11-30-07 09:49   jrst-0.8.1/lib/dom4j-1.6.1.jar
1994150  11-30-07 09:49   jrst-0.8.1/lib/fop-0.93-jdk15.jar
  55147  11-30-07 09:49   jrst-0.8.1/lib/activation-1.0.2.jar
 355030  11-30-07 09:49   jrst-0.8.1/lib/mail-1.3.3.jar
  77977  11-30-07 09:49   jrst-0.8.1/lib/servlet-api-2.3.jar
 226915  11-30-07 09:49   jrst-0.8.1/lib/jaxen-1.1.1.jar
 153253  11-30-07 09:49   jrst-0.8.1/lib/jdom-1.0.jar
  50789  11-30-07 09:49   jrst-0.8.1/lib/jewelcli-0.41.jar
 324952  11-30-07 09:49   jrst-0.8.1/lib/looks-1.2.2.jar
 121070  11-30-07 09:49   jrst-0.8.1/lib/junit-3.8.1.jar
 358085  11-30-07 09:49   jrst-0.8.1/lib/log4j-1.2.12.jar
  72150  11-30-07 09:49   jrst-0.8.1/lib/logkit-1.0.1.jar
 342897  11-30-07 09:49   jrst-0.8.1/lib/lutinwidget-0.9.jar
2160934  11-30-07 09:49   jrst-0.8.1/lib/docbook-xsl-nwalsh-1.71.1.jar
 301249  11-30-07 09:49   jrst-0.8.1/lib/xmlgraphics-commons-1.1.jar
  68610  11-30-07 09:49   jrst-0.8.1/lib/sdoc-0.5.0-beta.jar
3149655  11-30-07 09:49   jrst-0.8.1/lib/xalan-2.6.0.jar
1010675  11-30-07 09:49   jrst-0.8.1/lib/xercesImpl-2.6.2.jar
 194205  11-30-07 09:49   jrst-0.8.1/lib/xml-apis-1.3.02.jar
  78440  11-30-07 09:49   jrst-0.8.1/lib/xmlParserAPIs-2.0.2.jar
  86249  11-30-07 09:49   jrst-0.8.1/lib/xmlunit-1.1.jar
 108874  11-30-07 09:49   jrst-0.8.1/lib/xom-1.0.jar
  63966  11-30-07 09:49   jrst-0.8.1/lib/avalon-framework-4.1.3.jar
 138228  11-30-07 09:49   jrst-0.8.1/lib/batik-gui-util-1.6-1.jar
 216394  11-30-07 09:49   jrst-0.8.1/lib/l2fprod-common-0.1.jar
 121689  11-30-07 09:49   jrst-0.8.1/lib/lutinutil-0.26.jar
  76687  11-30-07 09:49   jrst-0.8.1/lib/batik-ext-1.6-1.jar
 124724  11-30-07 09:49   jrst-0.8.1/lib/xmlParserAPIs-2.6.2.jar

보시다시피 수동으로 수행 할 필요가없는 것이 다소 바람직합니다.

지금까지 AutoJar와 ProGuard 만 사용해 보았습니다. 둘 다 실행하기가 상당히 쉬웠습니다. JAR 파일의 상수 풀에 문제가있는 것 같습니다.

분명히 jrst는 약간 망가 졌기 때문에 고칠 것입니다. 메이븐 pom.xml 내가 jrst 고정하기 전에 ... 내가 버그 자석 :-) 같은 느낌 해결해야하므로 파일은 명백하게도 부서졌다


업데이트 :이 응용 프로그램을 고칠 수는 없었지만 뚱뚱한 JAR을 기반으로하는 Eclipse 의 "실행 가능한 JAR 내보내기 마법사"를 확인했습니다 . 내 코드를 배포하는 데 사용하기가 매우 쉽다는 것을 알았습니다.

다른 훌륭한 제안 중 일부는 Eclipse가 아닌 환경의 빌드에 더 좋을 수 있으며, oss는 Ant를 사용하여 멋진 빌드를 만들어야 할 것입니다 . (Maven, 지금까지 나에게 고통을 주었지만 다른 사람들은 그것을 좋아합니다.)


Eclipse 3.4 JDT의 Runnable JAR 내보내기 마법사.

Eclipse 3.5에서는 확장되었습니다. 이제 참조 된 JAR 파일을 처리 할 방법을 선택할 수 있습니다.


개미의 zipfileset역할

<jar id="files" jarfile="all.jar">
    <zipfileset src="first.jar" includes="**/*.java **/*.class"/>
    <zipfileset src="second.jar" includes="**/*.java **/*.class"/>
</jar>

몇 가지 다른 솔루션을 시도한 결과 One-JAR 이 가장 쉽게 작업 할 수 있다는 것을 알았으며 필요한 모든 것을 포함하는 단일 실행 가능한 JAR을 생성했습니다.

One-JAR는 중첩 된 리소스를 탐색 할 수있는 사용자 지정 클래스 로더를 사용합니다. 다운로드에서 .bat 파일을보십시오. jrst-0.8.1.jar의 org.codelutin.jrst.JRST가 메인 클래스이므로 매니페스트는 다음과 같아야합니다.

Main-Class: com.simontuffs.onejar.Boot
One-Jar-Main-Class: org.codelutin.jrst.JRST

정말 멋진 점은 One-JAR가 명령 줄 인수 전달을 처리한다는 것입니다. 클래스 경로는 필요한 모든 리소스가 단일 JAR에 번들되어 있다고 가정하고 사용자 정의 클래스 로더에 의해 처리됩니다.

The easiest way to use One-JAR is with ant; there's a custom "one-jar" ant task which works as follows (assuming your manifest is called "rst.mf"):

<target name="jar-rst">
    <one-jar destfile="rst.jar" manifest="rst.mf">
        <main jar="jrst-0.8.1.jar" />
        <lib>
            <fileset dir="${pathToJars}">
                <include name="batik-util-1.6-1.jar" />
                <include name="icu4j-2.6.1.jar" />
                <include name="commons-collections-3.1.jar" />
                <!-- Snip -->
            </fileset>
        </lib>
    </one-jar>
</target>

If you are a Maven user, typically the assembly plugin do what you want, or potentially the shade plugin, and in some cases a combination.

With the assembly plugin you put a manifest file in your project with any necessary settings, although the defaults are usually quite good. Building is then done with

mvn assembly:assembly

Or if you have more special things to deal with, one of the other goals. All JAR files to include, are picked up by Maven's dependency resolver. If you use the shade plugin, it is typically part of the install goal, and in one particular project I'm doing now I do

mvn install
mvn assembly:single

The assembly:single goal is to work around lifetime issues, in this case in a Spring application.


You can use JarJar which will use package shadowing to make sure your JAR file doesn't conflict with others.


There is ProGuard which does not only pack your JAR files into one, but it can also optimize, cleanup or obfuscate your class files, making the resulting JAR file much smaller than the sum of all JAR files before.

I actually tried ProGuard with the JRST tool, and it is as you reported. I tried to track the problem down and found it to relate to a bug in the ICU4J library referenced by jrst. The problem is, that the used ICU version is far outdated right now. So I replaced the icu.jar file with ICU4J version 3.2. Now ProGuard finds a bunch of other errors/warnings about incosistencies with the libraries of JRST.

My guess is that ProGuard works as expected, but the libraries of jrst are just not consistent. I don't know if you can do much more than talk with its developers since they should check and update the dependencies of the project.


(based on Andrian's):

<jar id="files" jarfile="all.jar">
  <zipgroupfileset dir="${library.dir}" includes="*.jar" excludes="test-helper.jar"/>
  <zipfileset src="first.jar" includes="**/*.java **/*.class"/>
  <zipfileset src="second.jar" includes="**/*.java **/*.class"/>
  <fileset dir=".">
    <include name="LICENSE"/>
    <include name="NOTICE"/>
  </fileset>
</jar>

One-JAR 0.97 has just been released at http://one-jar.sourceforge.net, and it has been extended wih support for frameworks such as Spring and Guice, which may present trouble to other approaches. It also handles classloader-inversion -- where some JAR files are external to the One-JAR (for example, JDBC drivers which may not be shipped bundled).

One-JAR is command-line, with Ant and Maven 2 plugins. It's also simple to build just using the "jar" tool.

I can also recommend the Eclipse Jar Exporter (Runnable) on which Ference Hechler wrote: he did a great job in coming up with a simple approach to wrapping a set of JAR files. He and I worked on One-JAR, but the Jar Exporter is based on a different codebase.


There is a tool called autojar which will scan your bytecode and compile a .jar file with the classes it finds, including referenced (imported) classes.

Doesn't always work with something like Spring, though, where you specify the classnames in configuration and it gets loaded by the framework.


Or using the Maven assembly plugin (mvn assembly:assembly)


I think that the tool you need here is JarSplice: http://ninjacave.com/jarsplice

It does not require Ant or Maven, has its own GUI, it is straightforward to use and do exactly what you asked --> It Merges the content of several jar files into a single one (please note it still need to add its own classloader).


You should use maven shading plugin to do that. I often use maven to build standalone jar file and it's so powerful

See more:

http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html


Sounds like Apache Ant is what you're looking for.

참고URL : https://stackoverflow.com/questions/81260/easiest-way-to-merge-a-release-into-one-jar-file

반응형