1

在我的 Web 应用程序中,为了混淆源代码,我使用了yGuard并获得了一个经过混淆的 jar 文件。现在我将此 jar 文件放入WEB-INF/lib文件夹中(因为我的 jsp 文件引用了 java 文件)并使用以下 ant 脚本打包了 war 文件:

<?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>

但是当我将此war文件部署到tomcat并访问index.jsp时,它会在浏览器上引发以下异常:

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

现在我的问题是:

  • 混淆后为什么jsp文件无法获取class文件的引用?
  • 我应该怎么做才能将我的混淆 jar 文件包含到 war 文件中才能完美地工作(或解决上述问题)?
4

1 回答 1

0

YGaurd 应该读取您的 jar 以获取 Main-Class 属性...您生成清单的 ant 看起来正确,清单出来了吗?

您还可以在重命名元素中指定主类: <rename logfile="renamelog.xml" mainclass="${main-class}>

这两种方法都应该防止你的主类被混淆。

于 2012-02-09T19:40:32.343 回答