spring boot proguard 警告:错误命名的文件中有 184 个类

Ndz*_*una 5 java obfuscation spring proguard spring-boot

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

Spring boot 似乎将我的类文件编译到一个名为BOOT-INF/classes的文件夹中,该文件夹 与原始目录结构不同。解决此问题的最佳方法是什么?

Hel*_*rld 1

我遇到了同样的问题,这个问题是第一个结果。因此,可能值得链接到原始解决方案。此处解释了该行为的原因:

ProGuard 要求 jar 文件中的类文件名称直接对应于类的名称,而不需要像“BOOT-INF/classes”这样的前缀。

解决这个问题的一种方法是提取 jar,在其上应用 Proguard,然后将混淆的类打包到新的 jar 中。

同一篇文章继续:

Spring Boot 基本上需要三个目录,BOOT-INF、META-INF 和 org。混淆您自己的类并重新打包到 fat jar 可以大致通过以下方式完成:

# Extract the unobfuscated fat jar from Spring Boot
jar xvf input.jar

# Execute ProGuard, as input for ProGuard use
# -injars BOOT-INF/classes/
java -jar proguard.jar @ proguard.conf

# If you also want to obfuscate the libs, add
# -injars BOOT-INF/lib
# Be aware, probably needs filtering, if some but not all libs should be obfuscated. I have not tested, but I believe the Spring Framwork libraries should not be obfuscated because they would require a lot of keep exceptions in the ProGuard configuration.
# If some of the libs should be used for obfuscation in the copy step below **remove** the libs before copying, otherwise the old files remain in the fat jar.

# Copy the old files excluding the classes to a new directory.
# First, the classes are stored here:
mkdir obfuscated/BOOT-INF
mkdir obfuscated/BOOT-INF/classes

# I assume the output is obfuscated-classes.jar
# It has to be extracted in the new output
cp obfuscated-classes.jar obfuscated/BOOT-INF/classes
pushd obfuscated/BOOT-INF/classes
jar xvf obfuscated-classes.jar
rm obfuscated-classes.jar
popd

# Copy the remaining files
boot_directories=(BOOT-INF/lib META-INF org)
for boot_directory in ${boot_directories[@]}; do
    mkdir -p "./obfuscated/$boot_directory"

    copy_command="cp -R ./$boot_directory/* ./obfuscated/$boot_directory/"
    eval $copy_command
done

# Finally, create a new jar
pushd obfuscated
# Be aware: do not use * as selector, the order of the entries in the resulting jar is important!
jar c0mf ./META-INF/MANIFEST.MF input-obfuscated.jar BOOT-INF/classes/ BOOT-INF/lib/ META-INF/ org/
popd

# Now, there is a jar obfuscated/input-obfuscated.jar that is a Spring Boot fat jar whose BOOT-INF/classes directory is obfuscated.
Run Code Online (Sandbox Code Playgroud)