Creating an executable jar. Various problems

I am working on an executable Java command line program. This is at the testing stage, and packaging it turned out to be a bit of a problem. The essence of this problem is related to the MANY dependencies that my application has. My lib folder (containing jars) is closed at 500mb.

I am currently using the Eclipse Runnable Jar Export wizard. It works flawlessly, except that it needs to be done from Eclipse. It generates a JAR of about 500 MB in size (don’t ask ... suffice it to say that there should be many COBOL programs in this program). This process takes <30 s.

Ideally, I would like it to be some kind of Ant task running through Jenkins and published to the repository. That way, the user can just grab the Jar and run it.

Parameters I investigated:

Custom class loader (FatJar, OneJar, etc.).

  • Work
  • Slow execution at runtime required to unpack the banner.
  • The file structure is basically a Jar filled with banks, and the main class is delegated through a custom class loader.
  • Fast assembly time (5 minutes)

    <target name="hello" depends="compile"> <property name="classes.dir" value="onejar" /> <property name="build.dir" value="bin" /> <one-jar destfile="hello.jar"> <manifest> <attribute name="One-Jar-Main-Class" value="mainclass" /> <attribute name="Class-Path" value="." /> <attribute name="One-Jar-Show-Expand" value="true" /> </manifest> <main> <fileset dir="${build.dir}"/> </main> <lib> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> </lib> </one-jar> </target> 

Manually collect dependencies, unpack them into a folder, and then create all kinds of MANIFEST files.

  • Cannot get it to work.
  • LARGE (1500mb +)
  • Slow build time (20 min +)

     <target name="compile" depends="resolve"> <javac srcdir="src" destdir="bin" debug="true" deprecation="on"> <classpath> <path refid="ivy.path" /> </classpath> </javac> </target> <target name="jar" depends="compile" description="Create one big jarfile."> <jar jarfile="${dist}/deps.jar"> <zipgroupfileset dir="lib"> <include name="**/*.jar" /> </zipgroupfileset> </jar> <sleep seconds="1" /> <jar jarfile="${dist}/myjar.jar" basedir="bin"> <zipfileset src="${dist}/deps.jar" excludes="META-INF/*.SF" /> <manifest> <attribute name="Main-Class" value="mymainclassishere" /> </manifest> </jar> </target> 

So does anyone have any suggestions? I am interested in hearing people's thoughts.

Edit: In particular .... WHY the Eclipse Runnable Jar Export Wizard can export my Jar in less than 30 s, but the build time is> 30 minutes.

+6
source share
1 answer

I know that answering your question can be confusing, but yesterday I managed to solve this problem. Of all the options I tried, I found that the fastest solution was to replicate the Eclipse Runnable Jar Export Wizard. This requires jar-in-jar-loader.zip , which can be obtained using the Export Executable Boxes Wizard ... or you can find it through the Google / Eclipse installation.

I found that this solution was quick to build (30 seconds) and much faster to run (5 second boot). He also left the can structure very neat, with no cans exploded.

 <target name="compile" depends="resolve"> <mkdir dir="bin"/> <!-- Copy all non java resources since jar javac will exclude them by default. Needed for xmls, properties etc ---> <copy todir="bin"> <fileset dir="src" excludes="**/*.java" /> </copy> <javac srcdir="src" destdir="bin" debug="true" deprecation="on"> <classpath> <path refid="ivy.path" /> </classpath> </javac> </target> <!-- Creates the runnable jar. Copies the dependencies as jar files, into the top level of a new jar. This means nothing without a custom classloader and manifest with a jar listing --> <target name="jar" depends="compile" description="Create one big jarfile."> <path id="dependencies.path"> <fileset dir="lib"> <include name="**/*.jar" /> </fileset> </path> <pathconvert property="manifest.classpath" pathsep=" "> <path refid="dependencies.path" /> <mapper> <chainedmapper> <flattenmapper /> <globmapper from="*.jar" to="*.jar" /> </chainedmapper> </mapper> </pathconvert> <mkdir dir="${dist}"/> <jar destfile="${dist}/jar/Runnable.jar" filesetmanifest="mergewithoutmain"> <manifest> <attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader" /> <attribute name="Rsrc-Main-Class" value="mymainclass" /> <attribute name="Class-Path" value="." /> <attribute name="Rsrc-Class-Path" value="./ ${manifest.classpath}" /> </manifest> <fileset dir="bin" /> <zipfileset src="jar-in-jar-loader.zip" /> <zipfileset dir="lib" includes="**/*.jar*" /> </jar> </target> 
+3
source

Source: https://habr.com/ru/post/913664/


All Articles