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?
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.
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 .
– 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:
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With