如何使用spring-boot gradle插件进行维护 [英] how to proguard with spring-boot gradle plugin

查看:181
本文介绍了如何使用spring-boot gradle插件进行维护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Spring Boot 2和Gradle构建来设置Proguard混淆?

How to setup proguard obfuscation with spring boot 2 and gradle build?

你好.尝试使用gradle插件和Proguard gradle插件设置Spring Boot应用程序的代码混淆. Google大多提供了一些针对较早的spring-boot-gradle-plugin版本的方法(例如,最接近的使用不存在的bootRepackage任务),或使用maven插件(具有重新打包的目标).

Hello. Trying to setup code obfuscation of Spring Boot app with its gradle plugin and Proguard gradle plugin. Google mostly gives some approaches for older spring-boot-gradle-plugin version (i.e. this closest one using non-existing bootRepackage task), or using maven plugin (having repackage goal).

据我所知,想法是在jar打包之前混淆类,但在当前gradle插件版本中看不到任何入口点,并且希望避免手动提取和回退.

Idea is to obfuscate classes before jar packaging, as I understand, but I don't see any entry points in current gradle plugin version, and would like to avoid manual extraction and zipping back.

有人在使用那个组合吗? Spring Boot版本> = 2.0.3.

Is anyone using that combo at all? Spring Boot version >=2.0.3.

推荐答案

我认为今天是不可能的,您必须手动提取并压缩回来.

I think that today is not possible and you have to do it with manual extraction and zipping back.

一个手动提取和压缩后的示例可能会有所帮助:

An example of manual extraction and zipping back which could be helpful:

build.gradle

build.gradle

version = '0.1.0'

buildscript {
    dependencies {
        classpath 'net.sf.proguard:proguard-gradle:6.0.3'
        classpath 'net.sf.proguard:proguard-base:6.0.3'
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

task extractJar(type: Copy) {
    def zipFile = file("${buildDir}/libs/your_project_name-${version}.jar")
    def outputDir = file("${buildDir}/unpacked/")

    from zipTree(zipFile)
    into outputDir
}

task proguard(type: proguard.gradle.ProGuardTask) {
    doFirst {
        tasks.extractJar.execute();
    }
    configuration 'proguard.conf'
    injars  "${buildDir}/unpacked/BOOT-INF/classes"
    outjars "${buildDir}/obfClasses"

    libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
    libraryjars "${buildDir}/unpacked/BOOT-INF/lib"

    doLast {
        tasks.deleteClasses.execute();
    }
}

task deleteClasses(type: Delete) {
    delete "${buildDir}/unpacked/BOOT-INF/classes/"

    doLast {
        tasks.copyObfuscatedClasses.execute()
    }
}

task copyObfuscatedClasses(type: Copy) {
    from "${buildDir}/obfClasses"
    into "${buildDir}/unpacked/BOOT-INF/classes/"
    include 'com/**'
    include '*.properties'

    doLast {
        tasks.copyObfuscatedJars.execute()
    }
}

task copyObfuscatedJars(type: Copy) {
    from "${buildDir}/obfClasses"
    into "${buildDir}/unpacked/BOOT-INF/lib/"
    include '*.jar'

    doLast {
        tasks.deleteObfuscated.execute()
    }
}

task deleteObfuscated(type: Delete) {
    delete 'build/obfClasses'

    doLast {
        tasks.repackage.execute()
    }
}

task repackage(type: Zip) {
    from  "${buildDir}/unpacked"
    entryCompression ZipEntryCompression.STORED
    archiveName "your_project_name-${version}-obf.jar"
    destinationDir(file("${buildDir}/libs"))
}

proguard.conf

proguard.conf

-ignorewarnings
-keepdirectories

-keep interface com.your_package.** { *; }
-keep class com.your_package.main{ *; }
-keep class com.your_package.model.** { *; }

-keepparameternames
-keepclassmembers @org.springframework.** class * {
    *;
}

-keepclassmembers @org.springframework.** interface * {
    *;
}

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

-keep @org.springframework.** class *
-keepclassmembers @javax.** class * { *; }

-dontwarn org.springframework.**
-dontwarn javax.**
-dontwarn org.yaml.snakeyaml.**
-dontwarn okhttp3.**

这篇关于如何使用spring-boot gradle插件进行维护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆