Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate minimized jar with only used classes

I'm in need of creating the minimal jar of utils library for use in Android. I'm using some methods from apache commons libraries (such as IOUtils, StringUtils). However, each such usage makes me import the whole library (commons-lang, commons-io etc.) which is absolutely acceptable under Tomcat (war's are mamoot-sized anyway), but absolutely unacceptable for Android project.

So, my aim is, to pack all used classes from dependencies into one jar - but only that classes that are needed. I remember once being in touch with maven plugin that done that task, unfortunatelly I can't remember its name nor find it via Google.

So please, do you know maven plugin that will do such minimization of dependencies, or any stand-alone tool that will do the same?

like image 689
Danubian Sailor Avatar asked Mar 01 '12 14:03

Danubian Sailor


People also ask

How do I set the main class in a JAR file?

Setting the Main Class It's helpful for the jar file manifest to include the main class. The manifest is a special file in a jar located the META-INF directory and named MANIFEST.MF . The manifest file contains special meta information about files within the jar file.

Should the JAR file manifest include the main class?

It's helpful for the jar file manifest to include the main class. The manifest is a special file in a jar located the META-INF directory and named MANIFEST.MF .

How to extract class names from an example JAR file?

– We take each class jarEntry, and change the path of the class file into the qualified class name, for example change “package1/package2/SomeType.class” into “package1.package2.SomeType” Let's verify if the method can extract the class names from our example JAR file through a unit test method:

How do I create a JAR file in Linux?

Using the Defaults To create the jar file, we are going to use the jar command. To use the jar command to create a jar file, we need to use the c option to indicate that we're creating a file and the f option to specify the file: 3.2. Setting the Main Class It's helpful for the jar file manifest to include the main class.


1 Answers

The maven plugin you can't remember is probably Apache Maven Shade Plugin, there is minimizeJar option. As Andreas_D noticed, this won't include classes, loaded with Class.forName, so you will need to implicity say in configuration, that you need them. Here is how i made maven to include jdbc driver in my single jar:

<filter>
  <artifact>net.sourceforge.jtds:jtds</artifact>
    <includes>
      <include>**</include>
    </includes>
</filter>
like image 132
Kirill Avatar answered Oct 24 '22 05:10

Kirill