账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    我用proguard混淆一个Spring mvc+mybatis项目后,war文件里没有应有的class文件是怎么回事?
    96
    0

    这是一个IDEA的maven项目。

    我把项目名target项目名-1.0-small.war文件解压缩后,WEB-INFclasses包名下除了xml文件,都是空的。

    build信息:
    [proguard] Note: there were 84 unkept descriptor classes in kept class members.
    [proguard] You should consider explicitly keeping the mentioned classes
    [proguard] (using '-keep').
    [proguard] (http://proguard.sourceforge.n...
    [proguard] Note: there were 9 unresolved dynamic references to classes or interfaces.
    [proguard] You should check if you need to specify additional program jars.
    [proguard] (http://proguard.sourceforge.n...
    [proguard] Note: there were 5 class casts of dynamically created class instances.
    [proguard] You might consider explicitly keeping the mentioned classes and/or
    [proguard] their implementations (using '-keep').
    [proguard] (http://proguard.sourceforge.n...
    [proguard] Note: there were 65 accesses to class members by means of introspection.
    [proguard] You should consider explicitly keeping the mentioned class members
    [proguard] (using '-keep' or '-keepclassmembers').
    [proguard] (http://proguard.sourceforge.n...
    [proguard] Note: you're ignoring all warnings!
    [proguard] Ignoring unused library classes...
    [proguard] Original number of library classes: 19879
    [proguard] Final number of library classes: 19879
    [proguard] Setting target versions...
    [proguard] Printing kept classes, fields, and methods...
    [proguard] Warning: there were 10914 unresolved references to classes or interfaces.
    [proguard] You may need to add missing library jars or update their versions.
    [proguard] If your code works fine without the missing classes, you can suppress
    [proguard] the warnings with '-dontwarn' options.
    [proguard] (http://proguard.sourceforge.n...
    [proguard] Warning: there were 203 unresolved references to program class members.
    [proguard] Your input classes appear to be inconsistent.
    [proguard] You may need to recompile the code.
    [proguard] (http://proguard.sourceforge.n...
    [proguard] Warning: there were 26 unresolved references to library class members.
    [proguard] You probably need to update the library versions.
    [proguard] (http://proguard.sourceforge.n...
    [proguard] Inlining subroutines...
    [proguard] Obfuscating...
    [proguard] Printing mapping to [C:UserswinIdeaProjects项目名targetproguard_map.txt]...
    [proguard] Preverifying...
    [proguard] Writing output...
    [proguard] Preparing output war [C:UserswinIdeaProjects项目名target项目名-1.0-small.war]
    [proguard] Copying resources from program war [C:UserswinIdeaProjects项目名target项目名-1.0.war] (filtered)
    [INFO]
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ 项目名 ---
    [INFO] Installing C:UserswinIdeaProjects项目名target项目名-1.0.war to C:Userswin.m2repositorycomsmart项目名1.0项目名-1.0.war
    [INFO] Installing C:UserswinIdeaProjects项目名pom.xml to C:Userswin.m2repositorycomsmart项目名1.0项目名-1.0.pom
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------

    proguard.conf文件的内容:

    忽略警告

    -ignorewarnings

    打印处理信息,失败时会打印堆栈信息

    -verbose

    保持目录结构

    -keepdirectories

    不能混淆泛型、抛出的异常、注解默认值、原始行号等

    -keepattributes Signature,Exceptions,Annotation,InnerClasses,Deprecated,EnclosingMethod

    对于包名、类名不进行混淆

    -keeppackagenames com.company.appname.**

    保留public、protected方法不被混淆

    -keep public class * {

      public protected *;

    }

    保留注解不被混淆

    -keep public @interface * {

    ** default (*);

    }

    保留枚举类不被混淆

    -keepclassmembers enum * {

    public static **[] values();
    public static ** valueOf(java.lang.String);

    }

    保持依赖注入不被混淆

    -keepclassmembers class * {

    @org.springframework.beans.factory.annotation.Autowired *;
    @javax.annotation.Resource *;

    }

    保持RMI调用不被混淆

    -keep class * implements java.rmi.Remote {

    <init>(java.rmi.activation.ActivationID, java.rmi.MarshalledObject);

    }

    保留JavaBean不被混淆

    -keepclassmembers class * implements java.io.Serializable {

    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();

    }

    避免类名被标记为final

    -optimizations !class/marking/final

    -target 1.8 ##指定java版本号
    -dontshrink ##默认是开启的,这里关闭shrink,即不删除没有使用的类/成员
    -dontoptimize ##默认是开启的,这里关闭字节码级别的优化
    -useuniqueclassmembernames ##对于类成员的命名的混淆采取唯一策略
    -adaptclassstrings ## 混淆类名之后,对使用Class.forName('className')之类的地方进行相应替代
    -dontusemixedcaseclassnames ## 混淆时不生成大小写混合的类名,默认是可以大小写混合

    pom.xml文件:

        <plugin>
            <groupId>com.github.wvengen</groupId>
            <artifactId>proguard-maven-plugin</artifactId>
            <version>2.0.14</version>
            <dependencies>
                <dependency>
                    <groupId>net.sf.proguard</groupId>
                    <artifactId>proguard-base</artifactId>
                    <version>5.3.3</version>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>proguard</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <proguardVersion>5.3.3</proguardVersion>
                <obfuscate>true</obfuscate>
                <injar>项目名-1.0.war</injar>
                <outjar>项目名-1.0-small.war</outjar>
                <outputDirectory>${project.build.directory}</outputDirectory>
                <proguardInclude>${project.basedir}/proguard.conf</proguardInclude>
                <libs>
                    <!-- Include main JAVA library required.-->
                    <lib>${java.home}/lib/rt.jar</lib>
                    <!-- Include crypto JAVA library if necessary.-->
                    <lib>${java.home}/lib/jce.jar</lib>
                </libs>
            </configuration>
        </plugin>
    
    2
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 葡萄要吐皮 普通会员 1楼

      在Proguard配置文件中,只有在目标类文件(.class)上使用"proguard-risk保留"指令,Spring mvc+Mybatis项目才会生成对应的.class文件。否则,不会生成.class文件。

      例如,你的配置文件可能如下所示:

      xml -keep class com.example.demo.MyController -keep class com.example.demo.MyService

      如果你的代码中使用了Spring mvc和Mybatis的API,那么.class文件就会生成。如果你只是在项目中使用了Mybatis的工具类,那么.class文件就不会生成。

      如果你在编译过程中发现没有.class文件生成,那么可能是你的代码中存在一些遗漏。你可以检查一下你的代码,看看是否在注释中指定了.class文件的位置。或者,你可以尝试在编译后查看.class文件的内容,看是否包含你需要的类。

    更多回答
    网站公告
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部