java代码混淆生成可执行jar包并添加第三方依赖

java代码混淆打包可执行jar包,并添加第三方依赖

先看疗效

  1. 执行mvn clean packagemaven命令即可生成两个插件对应的*jar-with-dependencies.jar和 代码混淆*jar-with-dependencies-pg.jar 包.

  2. 生成的*-jar-with-dependencies-pg.jar包指定了main方法,可通过java -jar 执行.

java代码混淆生成可执行jar包并添加第三方依赖_第1张图片

Maven 配置

pom.xml maven-assembly-plugin打包插件和proguard-maven-plugin代码混淆插件具体配置如下:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-pluginartifactId>
            <configuration>
                <archive>
                    <manifest>
                        
                        <mainClass>pub.qingyun.AppMainmainClass>
                    manifest>
                archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependenciesdescriptorRef>
                descriptorRefs>
            configuration>
            <executions>
                <execution>
                    <id>make-assemblyid> 
                    <phase>packagephase> 
                    <goals>
                        <goal>singlegoal>
                    goals>
                execution>
            executions>
        plugin>
        <plugin>
            <groupId>com.github.wvengengroupId>
            <artifactId>proguard-maven-pluginartifactId>
            <version>2.6.0version>
            <executions>
                <execution>
                    <phase>packagephase>
                    <goals>
                        <goal>proguardgoal>
                    goals>
                execution>
            executions>
            <configuration>
                <attach>trueattach>
                <attachArtifactClassifier>pgattachArtifactClassifier>
                
                <options> 
                    <option>-ignorewarningsoption> 
                    <option>-dontshrinkoption>   
                    <option>-dontoptimizeoption> 
                    <option>-dontskipnonpubliclibraryclassesoption>
                    <option>-dontskipnonpubliclibraryclassmembersoption>
                    <option>-dontnoteoption>
                    <option>-keepattributes Signatureoption>
                    
                    <option>-keepattributes Exceptions,InnerClasses,*Annotation*,Signature,LineNumberTableoption>
                    
                    <option>-keep class pub.qingyun.model.**option>
                    <option>-keepclassmembers class pub.qingyun.model.** { *;}option>
                    
                    <option>-keep class pub.qingyun.core.**option>
                    <option>-keepclassmembers class pub.qingyun.core.** { *;}option>
                    <option>-keep class pub.qingyun.exception.**option>
                    <option>-keepclassmembers class pub.qingyun.exception.** { *;}option>
                    
                    <option>-keepclassmembers class com.hopechart.dcroute.** { *;}option>
                    <option>-dontwarn com.hopechart.dcroute.**option>
                    <option>-keep class com.hopechart.dcroute.** {*;}option>
                    <option>-keepclassmembers class com.hopechart.cloud.** { *;}option>
                    <option>-dontwarn com.hopechart.cloud.**option>
                    <option>-keep class com.hopechart.cloud.** {*;}option>
                    <option>-keepclassmembers class com.hopechart.jcc.** { *;}option>
                    <option>-dontwarn com.hopechart.jcc.**option>
                    <option>-keep class com.hopechart.jcc.** {*;}option>
                    <option>-keepclassmembers class oracle.** { *;}option>
                    <option>-dontwarn oracle.**option>
                    <option>-keep class oracle.**option>
                    <option>-keepclassmembers class org.aopalliance.** { *;}option>
                    <option>-dontwarn org.aopalliance.**option>
                    <option>-keep class org.aopalliance.**option>
                    <option>-keepclassmembers class org.apache.commons.** { *;}option>
                    <option>-dontwarn org.apache.commons.**option>
                    <option>-keep class org.apache.commons.**option>
                    <option>-keepclassmembers class org.apache.log4j.** { *;}option>
                    <option>-dontwarn org.apache.log4j.**option>
                    <option>-keep class org.apache.log4j.**option>
                    <option>-keepclassmembers class org.slf4j.** { *;}option>
                    <option>-dontwarn org.slf4j.**option>
                    <option>-keep class org.slf4j.**option>
                    <option>-keepclassmembers class org.springframework.** { *;}option>
                    <option>-dontwarn org.springframework.**option>
                    <option>-keep class org.springframework.**option>
                    
                    <option>-keep public class pub.qingyun.AppMain {
                        public static void main(java.lang.String[]);
                        }
                    option>
                options>
                
                <injar>code-confusion-demo-1.0-SNAPSHOT-jar-with-dependencies.jarinjar>
                
                <outjar>${project.build.finalName}-pg.jaroutjar>
                <obfuscate>trueobfuscate>
                
                <libs>
                    
                    
                    
                    <lib>C:/Program Files/Java/corretto-1.8.0_332/jre/lib/rt.jarlib>
                    <lib>C:/Program Files/Java/corretto-1.8.0_332/jre/lib/jce.jarlib>
                    
                    
                libs>
            configuration>
        plugin>
    plugins>
build>

proguard-maven-plugin代码混淆插件也可通过配置文件配置.

<plugin>
    <groupId>com.github.wvengengroupId>
    <artifactId>proguard-maven-pluginartifactId>
    <version>2.6.0version>
    <executions>
        <execution>
            <phase>packagephase>
            <goals>
                <goal>proguardgoal>
            goals>
        execution>
    executions>
    <configuration>
        ...
        <proguardInclude>${project.basedir}/proguard.cfgproguardInclude>
        ...
    configuration>
plugin>

proguard.cfg配置文件

# 指定Java的版本
-target 11
# proguard会对代码进行优化压缩,他会删除从未使用的类或者类成员变量等
-dontshrink
# 是否关闭字节码级别的优化,如果不开启则设置如下配置
-dontoptimize
# 混淆时不生成大小写混合的类名,默认是可以大小写混合
-dontusemixedcaseclassnames
# 对于类成员的命名的混淆采取唯一策略
-useuniqueclassmembernames
# 混淆时不生成大小写混合的类名,默认是可以大小写混合
-dontusemixedcaseclassnames
# 混淆类名之后,对使用Class.forName('className')之类的地方进行相应替代
-adaptclassstrings

# 对异常、注解信息予以保留
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
# 此选项将保存接口中的所有原始名称(不混淆)-->
-keepnames interface ** { *; }
# 此选项将保存所有软件包中的所有原始接口文件(不进行混淆)
#-keep interface * extends * { *; }
# 保留参数名,因为控制器,或者Mybatis等接口的参数如果混淆会导致无法接受参数,xml文件找不到参数
-keepparameternames
# 保留枚举成员及方法
-keepclassmembers enum * { *; }
# 不混淆所有类,保存原始定义的注释-
#-keepclassmembers class * {
#                        @org.springframework.context.annotation.Bean *;
#                        @org.springframework.beans.factory.annotation.Autowired *;
#                        @org.springframework.beans.factory.annotation.Value *;
#                        @org.springframework.stereotype.Service *;
#                        @org.springframework.stereotype.Component *;
#                        }

# 忽略warn消息
-ignorewarnings
# 忽略note消息
-dontnote
# 打印配置信息
-printconfiguration
-keep public class pub.qingyun.AppMain {
        public static void main(java.lang.String[]);
    }

你可能感兴趣的:(java,代码混淆,java,jar)