See: Part 1: Shrink Solr Application Size
4. Use proguard to shrink jar size.
http://proguard.sourceforge.net/
proguard detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions.
we need use "Trial and error": run it, if report NoClassDefFoundError, or NoSuchMethodException, modify the configuration file like below:
-keep public class org.apache.solr.servlet.SolrDispatchFilter
-keepclassmembers public class org.apache.solr.servlet.SolrDispatchFilter {
*;
}
Then redo previous steps.
We can use ANT to do these tasks, the script will copy original jars to some places, use proguard to shrink them, copy shrinked jars to solr application, start the server, and run some tests.
<taskdef resource="proguard/ant/task.properties" classpath="<proguard4.8>\lib\proguard.jar" /> <target name="shrinkJetty" depends="postProcess"> <proguard configuration="conf-jetty.txt"/> </target> <target name="shrinkSolr" depends="postProcess"> <proguard configuration="conf-solr.txt"/> </target>
5. Use ANT to shrink XML files
First we need remove all unneeded configuration from solrconfig.xml, Solr.xml, web.xml, and xml under
<target name="shrinkRuntimeXMLs"> <path id="orignal.xmls.path"> <fileset dir="${runtime.solr.core.home}/conf/"> <include name="*.xml" /> </fileset> </path> <property name="orignal.xmls" refid="orignal.xmls.path" /> <echo message="orignal.xmls: ${orignal.xmls}" /> <ac:foreach list="${orignal.xmls};${runtime.solr.home}/solr.xml;${runtime.solr.war}/WEB-INF/web.xml" delimiter=";" param="original.xml" target="shrinkXml"/> </target> <target name="shrinkXml"> <basename property="original.xml.basename" file="${original.xml}"/> <dirname property="original.xml.dirname" file="${original.xml}"/> <echo message="original.xml: ${original.xml}"/> <xslt basedir="${original.xml.dirname}" includes="${original.xml.basename}" destdir="${shrinked.xml.output}" extension=".xml" style="shrink.xslt"/> <move file="${shrinked.xml.output}/${original.xml.basename}" tofile="${original.xml}"/> </target>
shrink.xslt:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="comment()"/> </xsl:stylesheet>6. Use ANT to shrink property files
We can also remove comment, empty lines from property files.
<target name="shrinkPropertyFiles"> <path id="orignal.properties.path"> <fileset dir="${runtime.solr.core.home}/conf/"> <include name="*.txt" /> </fileset> </path> <property name="orignal.properties" refid="orignal.properties.path" /> <ac:foreach list="${orignal.properties}" delimiter=";" param="original.propertyFile" target="shrinkPropertyFile"/> </target> <target name="shrinkPropertyFile"> <replaceregexp file="${original.propertyFile}" match="^#.*" replace="" byline="true"/> <copy file="${original.propertyFile}" toFile="${original.propertyFile}-tmp"> <filterchain> <ignoreblank/> </filterchain> </copy> <move file="${original.propertyFile}-tmp" tofile="${original.propertyFile}"/> </target>After all these steps, we remove the whole application from 16.6mb to 7.3 mb.
You can view all source code from github:
https://github.com/jefferyyuan/tools/tree/master/ant-scripts/shrink-solr