ProGuard + Spring Boot + Maven插件

时间:2016-11-11 17:53:45

标签: maven spring-boot proguard

伙计们,我正在尝试使用proguard-maven-plugin来混淆.jar应用程序。
当我尝试执行混淆过程时,我收到错误消息,指出存在意外的类。

我正在使用Spring Boot 1.4.1.RELEASE和Proguard Maven插件2.0.13。

这是我的 proguard.conf

-injars /workspace/base/target/test-1.0.0.jar

-libraryjars /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/rt.jar

-dontshrink
-dontoptimize
-dontobfuscate
-dontusemixedcaseclassnames
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod

-adaptresourcefilenames **.properties
-adaptresourcefilecontents **.properties,META-INF/MANIFEST.MF

-dontpreverify
-verbose


-keepclasseswithmembers public class * {
    public static void main(java.lang.String[]);
}

-keepclassmembers enum  * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * extends java.beans.BeanInfo

-keep class * {
    void set*(***);
    void set*(int,***);
    boolean is*();
    boolean is*(int);
    *** get*();
    *** get*(int);
}

-assumenosideeffects public class java.lang.System {
    public static long currentTimeMillis();
    static java.lang.Class getCallerClass();
    public static int identityHashCode(java.lang.Object);
    public static java.lang.SecurityManager getSecurityManager();
    public static java.util.Properties getProperties();
    public static java.lang.String getProperty(java.lang.String);
    public static java.lang.String getenv(java.lang.String);
    public static java.lang.String mapLibraryName(java.lang.String);
    public static java.lang.String getProperty(java.lang.String,java.lang.String);
}

pom.xml 文件。我只是通过插件通知配置。

<plugin>
    <groupId>com.github.wvengen</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <version>2.0.13</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <obfuscate>false</obfuscate>
        <outFilter>**/BOOT-INF/classes/ **.class</outFilter>

        <proguardInclude>${basedir}/proguard.conf</proguardInclude>
        <outputDirectory>${project.build.directory}</outputDirectory>

        <injar>${project.build.finalName}.jar</injar>
        <outjar>${project.build.finalName}-min.jar</outjar>
    </configuration>
</plugin>

但是,在执行过程中,我得到了应用程序中所有类的以下返回值。

Warning: class [BOOT-INF/classes/br/com/base/BaseApplication.class] unexpectedly contains class [br.com.base.BaseApplication]
Warning: class [BOOT-INF/classes/br/com/base/controller/CaixaController.class] unexpectedly contains class [br.com.base.controller.CaixaController]
[...]

ProGuard的最终输出。 PS:所有课程都在BOOT-INF/classes目录

Warning: there were 97 classes in incorrectly named files.
You should make sure all file names correspond to their class names.
The directory hierarchies must correspond to the package hierarchies.
     (http://proguard.sourceforge.net/manual/troubleshooting.html#unexpectedclass)
If you don't mind the mentioned classes not being written out,
you could try your luck using the '-ignorewarnings' option.
Please correct the above warnings first.

任何人都可以想象我可以尝试的其他选择吗? 感谢。

1 个答案:

答案 0 :(得分:4)

为了解决这个问题,我确保改变pom中插件的顺序。应首先使用proguard插件,然后使用spring boot插件。

此外,请确保在spring boot配置中指定了<goal>repackage</goal>。通过正确的顺序和指定的重新打包目标,将进行proguard混淆/优化/您配置的任何内容并生成一个jar。然后spring boot插件将该jar重新打包为可执行文件,一切都应该可以工作。

我在pom.xml中的插件配置:

<project ...>
....
<plugin>
    <groupId>com.github.wvengen</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <proguardInclude>${basedir}/proguard.conf</proguardInclude>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jce.jar</lib>
        </libs>
    </configuration>
</plugin>
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <start-class>org.springframework.boot.loader.JarLauncher</start-class>
            </configuration>
        </execution>
    </executions>
</plugin>
...