How to include obfuscated jar file into a war file

2019-06-07 06:17发布

问题:

In my web application, to obfuscate the source code, i used yGuard and got an obfuscated jar file. Now i placed this jar file into the WEB-INF/lib folder (as my jsp files have reference to the java files) and packaged the war file using below ant script :

<?xml version="1.0" encoding="UTF-8"?>    
   <project name="MyWebApp" default="war" basedir=".">
      <property file="build.properties" />  
      <property name="war-file-name" value="${project-name}.war" />
      <property name="source-directory" value="src" />
      <property name="classes-directory" value="WebRoot/WEB-INF/classes" />
      <property name="web-directory" value="WebRoot" />
      <property name="web-xml-file" value="WebRoot/WEB-INF/web.xml" />
      <property name="build-directory" value="build" />
      <property name="lib-dir" value="WebRoot/WEB-INF/lib" />
      <property name="main-class"  value="com.Main"/>

      <path id="compile.classpath">
         <fileset dir="${lib-dir}">
             <include name="*.jar"/>        
         </fileset>
      </path>

      <target name="clean">
         <delete dir="${classes-directory}" />      
         <delete dir="${build-directory}" />                
      </target>

      <!-- Creates the  build, docs and dist directory-->
      <target name="makedir">
         <mkdir dir="${classes-directory}" />       
         <mkdir dir="${build-directory}" />
      </target> 

      <target name="compile" depends="clean, makedir">
         <javac srcdir="${source-directory}" destdir="${classes-directory}" includeantruntime="false">
            <classpath refid="compile.classpath"/>
         </javac>
         <copy  todir="${classes-directory}">
            <fileset dir="src" includes="**/*.properties"/>
         </copy>
      </target>

      <target name="jar" depends="compile">
             <jar destfile="${build-directory}/${ant.project.name}.jar" basedir="${classes-directory}">
                  <manifest>
                       <attribute name="Main-Class" value="${main-class}"/>
                  </manifest>
             </jar>
       </target>

       <path id="external.lib.path">
         <fileset dir="${lib-dir}" includes="*.jar"/>
       </path>

       <target name="yguard" depends="jar">
         <taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" classpath="yguard.jar"/>
         <yguard>

             <inoutpair in="${build-directory}/${ant.project.name}.jar" out="${build-directory}/${ant.project.name}_obf.jar"/>

         <externalclasses refid="external.lib.path"/>

             <rename logfile="renamelog.xml">
                 <keep>
                <method name="void main(java.lang.String[])">
                  <patternset>
                     <include name="com.*"/>
                     <include name="misc.*"/>
                     <include name="client.*"/>
                  </patternset>
                </method>
                </keep> 
            </rename>
                </yguard>

        <copy file="${build-directory}/${ant.project.name}_obf.jar" todir="${lib-dir}"/>
     </target>

     <target name="war" depends="yguard">
        <war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}">
        <fileset dir="${web-directory}">
            <!-- Need to exclude it since webxml is an attribute of the war tag above --> 
            <exclude name="WEB-INF/web.xml" />
            <exclude name="WEB-INF/lib/servlet-api.jar" />
            <exclude name="WEB-INF/lib/jsp-api.jar" />
            <exclude name="WEB-INF/classes/**/" />
        </fileset>          
        </war>
     </target>
   </project>

But when i deployed this war file to tomcat and accessed the index.jsp it throws following exception on browser :

javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/Main

Now my questions are :

  • After obfuscation why the jsp file is not able to get the reference of class file ?
  • What should i do to include my obfuscated jar file into the war file to work it perfectly ( or resolve the above issue)?

回答1:

YGaurd is supposed to read your jar for a Main-Class attribute... Your ant to generate the manifest looks correct, does the manifest come out OK?

You can also specify a mainclass in your rename element: <rename logfile="renamelog.xml" mainclass="${main-class}>

Both methods should prevent your main class from being obfuscated.