Maven WEB 项目使用ProGuard进行混淆,最佳解决方案&详解

本文中介绍的项目使用了SSM框架。

项目包结构:
Maven WEB 项目使用ProGuard进行混淆,最佳解决方案&详解_第1张图片

该项目是典型的Maven WEB项目

主要混淆对象 对类的名称、属性、方法名都进行混淆
次要混淆对象 对类的名称不混淆,类的属性、方法名选择性混淆
非混淆对象 不进行混淆,混淆后可能出现异常

Maven 配置(pom.xml)

使用Maven集成的ProGuard插件,混淆配置不用单独建立文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.testgroupId>
    <artifactId>progartifactId>
    <packaging>warpackaging>
    <version>hxversion>
    <name>prog Maven Webappname>
    <url>http://maven.apache.orgurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <mybatis.generator.configurationFile>src/test/resources/mybatisGenerator.xmlmybatis.generator.configurationFile>
    properties>

    <dependencies>
        <dependency>
            <groupId>javax.persistencegroupId>
            <artifactId>persistence-apiartifactId>
            <version>1.0version>
        dependency>
        <dependency>
            <groupId>com.googlecode.sli4jgroupId>
            <artifactId>sli4j-slf4j-log4jartifactId>
            <version>2.0version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>net.sf.ehcachegroupId>
            <artifactId>ehcache-coreartifactId>
            <version>2.6.5version>
        dependency>
        <dependency>
            <groupId>org.codehaus.jacksongroupId>
            <artifactId>jackson-mapper-aslartifactId>
            <version>1.9.13version>
        dependency>
        <dependency>
            <groupId>net.sf.json-libgroupId>
            <artifactId>json-libartifactId>
            <version>2.4version>
            <classifier>jdk15classifier>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>3.2.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>3.2.4.RELEASEversion>
            <type>jartype>
            <scope>compilescope>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.2.1version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-ehcacheartifactId>
            <version>1.0.0version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>3.2.5.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.2version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
            <version>3.8version>
        dependency>
        <dependency>
            <groupId>googlegroupId>
            <artifactId>google-connectionsartifactId>
            <version>1.0version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>3.2.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>commons-httpclientgroupId>
            <artifactId>commons-httpclientartifactId>
            <version>3.1version>
        dependency>
        
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-frontend-jaxwsartifactId>
            <version>2.7.6version>
        dependency>
        <dependency>
            <groupId>org.apachegroupId>
            <artifactId>neethiartifactId>
            <version>3.0.2version>
        dependency>
        <dependency>
            <groupId>wsdl4jgroupId>
            <artifactId>wsdl4jartifactId>
            <version>1.6.3version>
            <type>jartype>
            <scope>compilescope>
        dependency>
        <dependency>
            <groupId>org.apache.wsgroupId>
            <artifactId>commons.schemaartifactId>
            <version>2.0.3version>
        dependency>
        <dependency>
            <groupId>com.ctcgroupId>
            <artifactId>wstxartifactId>
            <version>4.2.0version>
        dependency>
        <dependency>
            <groupId>org.codehausgroupId>
            <artifactId>stax2artifactId>
            <version>3.1.1version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>3.2.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>3.2.5.RELEASEversion>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.7.4version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.12version>
        dependency>
        <dependency>
            <groupId>net.sourceforge.jtdsgroupId>
            <artifactId>jtdsartifactId>
            <version>1.3.0version> 
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.1.1version>
        dependency>
        <dependency>
            <groupId>javaxgroupId>
            <artifactId>javaee-apiartifactId>
            <version>6.0version>
            <scope>providedscope>
            
        dependency>

        <dependency>
            <groupId>javax.mailgroupId>
            <artifactId>mailartifactId>
            <version>1.4.7version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.3version>
        dependency>
        <dependency>
            <groupId>org.apache.clerezza.extgroupId>
            <artifactId>org.json.simpleartifactId>
            <version>0.3-incubatingversion>
            <scope>compilescope>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.1.41version>
        dependency>
        <dependency>
            <groupId>dom4jgroupId>
            <artifactId>dom4jartifactId>
            <version>1.6version>
        dependency>
        
        <dependency> 
            <groupId>com.google.zxinggroupId>
            <artifactId>coreartifactId>
            <version>3.0.1version>
        dependency>
        
        <dependency>
            <groupId>net.coobirdgroupId> 
            <artifactId>thumbnailatorartifactId>
            <version>0.4.8version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>3.2.5.RELEASEversion>
        dependency>
         <dependency>
            <groupId>org.unitilsgroupId>
            <artifactId>unitils-coreartifactId>
            <version>3.1version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.unitilsgroupId>
            <artifactId>unitils-databaseartifactId>
            <version>3.1version>
            <scope>testscope>
        dependency> 
        <dependency>
            <groupId>org.unitilsgroupId>
            <artifactId>unitils-dbmaintainerartifactId>
            <version>3.1version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.unitilsgroupId>
            <artifactId>unitils-dbunitartifactId>
            <version>3.1version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.unitilsgroupId>
            <artifactId>unitils-springartifactId>
            <version>3.1version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.unitilsgroupId>
            <artifactId>unitils-ormartifactId>
            <version>3.1version>
            <scope>testscope>
        dependency> 
        <dependency>
            <groupId>org.bouncycastlegroupId>
            <artifactId>bcprov-jdk14artifactId>
            <version>1.45version>
        dependency>
        <dependency>
            <groupId>sun.miscgroupId>
            <artifactId>BASE64DecoderartifactId>
            <version>1.2version>
        dependency>
        <dependency>
            <groupId>org.quartz-schedulergroupId>
            <artifactId>quartzartifactId>
            <version>1.8.5version>
        dependency>  
    dependencies>
    <build>
        
        <plugins>

            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>2.3.2version>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>

            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.1version>
                <configuration>
                    <overwrite>falseoverwrite>
                configuration>
            plugin>

            <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <version>2.1version>
                <configuration>
                    <uriEncoding>utf-8uriEncoding>
                    <port>8888port>
                    <path>/path>
                    <contextReloadable>falsecontextReloadable>
                    <useTestClasspath>falseuseTestClasspath>
                configuration>
            plugin>

            <plugin>
                <groupId>org.codehaus.mojogroupId>
                <artifactId>tomcat-maven-pluginartifactId>
                <version>1.1version>
                <configuration>
                    <url>http://192.168.200.121:8080/manager/texturl>
                    <username>tomcatusername>
                    <password>passwordpassword>
                    <path>/prog11path>
                configuration>
            plugin>

            
            <plugin>
                <groupId>com.github.wvengengroupId>
                <artifactId>proguard-maven-pluginartifactId>
                <version>2.0.11version>
                <executions>
                    <execution>
                        
                        <phase>packagephase>
                        <goals>
                            
                            <goal>proguardgoal>
                        goals>
                    execution>
                executions>
                <configuration>
                    
                    <attach>trueattach>
                    
                    <obfuscate>trueobfuscate>
                    
                    <attachArtifactClassifier>pgattachArtifactClassifier>
                    <options>
                        
                        <option>-target 1.7option>
                        
                        <option>-dontshrinkoption>
                        
                        <option>-dontoptimizeoption>
                        
                        <option>-dontskipnonpubliclibraryclassesoption>
                        <option>-dontskipnonpubliclibraryclassmembersoption>
                        
                        <option>-allowaccessmodificationoption>
                        
                        <option>-useuniqueclassmembernamesoption>
                        
                        <option>-keeppackagenamesoption>
                        
                        <option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethodoption> 
                        
                        <option>-keepclassmembers public class * {void set*(***);*** get*();}option>

                        
                        
                        
                        
                        
                        

                        
                        <option>-keep class com.test.prog.util.finals.Const{ *; }option>

                        
                        <option>-keep class com.test.prog.util.SecCodeoption>
                        <option>-keep class com.test.prog.util.exception.HihSoftHandlerExceptionoption>

                        
                        <option>-keep class com.test.prog.controller.** { <methods>; }option> 
                    options>
                    <outjar>${project.build.finalName}-pg.jaroutjar>
                    
                    <libs>
                        <lib>${java.home}/lib/rt.jarlib>
                        <lib>${java.home}/lib/jce.jarlib>    
                        <lib>${java.home}/lib/security/local_policy.jarlib>  
                        <lib>${java.home}/lib/security/US_export_policy.jarlib>
                        <lib>${java.home}/lib/ext/sunjce_provider.jarlib>        
                    libs>
                    
                    <inFilter>com/test/prog/**inFilter>
                    
                    <injar>classesinjar>
                    
                    <outputDirectory>${project.build.directory}outputDirectory>
                configuration>
            plugin>
        plugins>
        
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
            resource>
            <resource>
                <directory>src/main/javadirectory>
                <excludes>
                    <exclude>**/*.javaexclude>
                    <exclude>**/.svn/*exclude>
                excludes>
            resource>
        resources>
    build>
project>

在项目location执行命令

mvn clean package -DskipTests -X

执行命令后,会在target下生成以下文件:
Maven WEB 项目使用ProGuard进行混淆,最佳解决方案&详解_第2张图片

  • classes-pg.jar 混淆后的classes文件,里面包含完整的项目结构
  • proguard_map.txt 混淆内容的映射
  • proguard_seed.txt 参与混淆的类
    混淆完成后,将classes-pg.jar解压到应用服务器覆盖原有的classes文件,通常目录为

E:\tomcat7\webapps\项目名\WEB-INF\classes

运行服务,项目运行成功
查看目录下面文件(下面是包括包名的混淆)

Maven WEB 项目使用ProGuard进行混淆,最佳解决方案&详解_第3张图片

通过反编译工具查看混淆代码

Maven WEB 项目使用ProGuard进行混淆,最佳解决方案&详解_第4张图片

可以看出,混淆成功了!

下面是测试中遇到的部分问题及相关的解决方式:

Maven WEB 项目使用ProGuard进行混淆,最佳解决方案&详解_第5张图片

解析:-target 1.7 jdk目标版本的问题

Maven WEB 项目使用ProGuard进行混淆,最佳解决方案&详解_第6张图片

解析:-dontskipnonpubliclibraryclasses 不路过非公用类文件及成员

Maven WEB 项目使用ProGuard进行混淆,最佳解决方案&详解_第7张图片

解析:-dontskipnonpubliclibraryclasses -dontskipnonpubliclibraryclassmembers 不路过非公用类文件及成员造成的bug

Maven WEB 项目使用ProGuard进行混淆,最佳解决方案&详解_第8张图片

解析:-dontoptimize 变更代码逻辑引起的bug

还有一些其他的关于项目不能混淆的bug,这个需要根据项目而定。

你可能感兴趣的:(Mybatis)