在构建"maven-plugin"插件时如何使用Proguard混淆包裹? [英] How do I use Proguard obfuscation while building a "maven-plugin" package?

查看:73
本文介绍了在构建"maven-plugin"插件时如何使用Proguard混淆包裹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"maven-plugin"项目的生成输出JAR(使用 maven -plugin-plugin )被 Proguard 工具执行的混淆处理破坏了.尝试将混淆的JAR用作Maven插件会生成异常,例如

The generated output JAR of a "maven-plugin" project (using the maven-plugin-plugin) is broken by the obfuscation performed by the Proguard tool. Attempting to use the obfuscated JAR as a Maven plugin generates exceptions, such as a MojoExecutionException, which terminate the build with an error. What are the proper Proguard configuration options to allow the generation of a working "maven-plugin" JAR containing an automatically generated plugin descriptor?

推荐答案

Maven插件和Proguard Tool的基础

为了生成Maven插件(maven打包类型 "maven-plugin" ,它会生成一个包含插件特定配置资源的JAR),我们必须指示 maven-plugin-plugin 上我们Mojos的位置和名称.假设正确配置了maven-plugin-plugin执行,则

Basics of Maven Plugins and the Proguard Tool

In order to generate a Maven plugin (maven packaging type "maven-plugin", which generates a JAR containing plugin specific configuration resources) we must instruct the maven-plugin-plugin on the location and name of our Mojos. Assuming a properly configured maven-plugin-plugin execution, either using annotations or other configuration options, the generated JAR will contain a plugin.xml file within the META-INF directory at the root of the JAR. This plugin.xml file describes your plugin's goals and configurable parameters using static references to Java class and package names (you can find more information on this file here).

必须特别注意将混淆处理纳入"maven-plugin" JAR的构建中;在这里,我们说明使用 Proguard混淆库所采取的步骤.使用库混淆的默认Proguard配置时,生成的JAR将由于Proguard重命名,缩小,重新定位和混淆了Maven插件的重要文件,因此无法再正常工作.尝试使用您的插件可能会导致异常,该异常会终止构建,并产生与Maven运行时无法定位和处理插件的配置文件和类文件有关的错误.

Special care must be taken to incorporate obfuscation into a build of a "maven-plugin" JAR; here we explain the steps taken in using the Proguard obfuscation library. When using the default Proguard configuration for library obfuscation, the generated JAR will no longer work correctly because Proguard renames, shrinks, relocates and obfuscates important files for the Maven plugin. Attempting to use your plugin will likely result in an exception which terminates the build with an error related to the Maven runtime's inability to locate and process the plugin's configuration and class files.

但是,通过一些重新配置,我们可以指示Proguard正确维护"maven-plugin" JAR的生成的插件文件和目录结构.对Proguard选项的必要更改如下(请参阅有关以下链接的注释):

With a little reconfiguration, however, we can instruct Proguard to properly maintain the generated plugin files and directory structure of your "maven-plugin" JAR. The necessary changes to the Proguard options are as follows (please see the notes regarding the following links):

-keepdirectories

-keepdirectories

这指示Proguard维护输入的JAR目录结构,而不是将所有文件移动到根目录. Maven希望plugin.xml文件位于/META-INF/maven/目录中,该目录可通过此选项与所有其他目录一起保留.您可以通过指定目录过滤器来更具体地过滤保留的目录,但是我选择了不加选择地维护所有输入目录结构.

This instructs Proguard to maintain the input JAR directory structure instead of moving all files to the root directory. Maven expects the plugin.xml file to be located within the /META-INF/maven/ directory which is preserved alongside all other directories via this option. You can filter the kept directories more specifically by specifying a directory filter, however I have chosen to indiscriminately maintain all input directory structures.

-keeppackagenames

-keeppackagenames org.apache.maven.plugin.my.MyMojo

您应将占位符包替换为包含Mojo定义的包.如果您的Mojo定义不共享公共包,则应根据需要使用多个选项指定每个唯一的包.如果不确定需要保留哪些软件包,请在文本编辑器中打开生成的plugin.xml文件,然后检查每个"mojo"定义中的"implementation"元素. 实现"元素通过完全限定的名称指定一个类.这些完全限定的类名的每个包组件都是您应指定的包.例如,我的basedir插件包含一个Mojo实现元素值"com.github.emabrey.maven.plugins.basedir.RootDirectoryGoal",因此我将选项写为-keeppackagenames com.github.emabrey.maven.plugins.basedir.

You should replace the placeholder package with the package containing your Mojo definitions. If your Mojo definitions do not share a common package you should specify each unique package using multiple options as needed. If you are unsure which packages need to be kept, open your generated plugin.xml file in a text editor and examine the "implementation" elements within each "mojo" definition. The "implementation" element specifies a class via a fully qualified name. Each package component of those fully qualified class names are the packages you should be specifying. For example, my basedir-plugin contains a Mojo implementation element value of "com.github.emabrey.maven.plugins.basedir.RootDirectoryGoal", so I would write the option as -keeppackagenames com.github.emabrey.maven.plugins.basedir.

-keepnames

-keepnames class * implements org.apache.maven.plugin.AbstractMojo

此选项可防止Proguard重命名包含Maven插件Mojo的实现的类.如果将这些类重命名,则上述实现"元素将不再正确标识包含Mojo实现的类.

This option prevents Proguard from renaming the classes which contain the implementation of a Maven plugin Mojo. If these classes were renamed the aforementioned "implementation" elements would no longer correctly identify the classes containing a Mojo implementation.

-keepclassmembers

-keepclassmembers class * implements org.apache.maven.plugin.AbstractMojo {
    private <fields>;
    private <methods>;
}

此选项可防止Proguard重命名插件Mojo实现中的类级别方法和字段. Maven使用这些类字段/方法的名称来生成插件的配置元素.如果Proguard重命名字段,则Maven执行环境将无法使用用户配置正确填充Mojo实现.

This option prevents Proguard from renaming the class level methods and fields within a plugin Mojo implementation. Maven uses the names of these class fields/methods to generate the configuration elements for the plugin. If Proguard renames a field the Maven execution environment will be unable to correctly populate the Mojo implementation with the user configuration.

版本2.0.13的完整配置(

The full configuration for version 2.0.13 (more versions here; please see the notes regarding the plugin's version), including the default library configuration alongside the mentioned modifications, is provided here for your convenience (remember to specify the ${tool.proguard.version} property with the latest version of the proguard-base artifact and replace the placeholder package "org.apache.maven.plugin.my.MyMojo" with the appropriate value(s)):

<!-- Configures Proguard obfuscation tool to generate an
     obfuscated version of the JAR file that replaces the
     default unobfuscated JAR.
-->
<plugin>
    <groupId>com.github.wvengen</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <version>2.0.13</version>
    <executions>
        <execution>
            <id>obfuscation-packaging</id>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
            <configuration>
                <proguardVersion>${tool.proguard.version}</proguardVersion>
                <obfuscate>true</obfuscate>
                <attach>true</attach>
                <appendClassifier>false</appendClassifier>
                <addMavenDescriptor>true</addMavenDescriptor>
                <injar>${project.build.finalName}.jar</injar>
                <injarNotExistsSkip>true</injarNotExistsSkip>
                <libs>
                    <lib>${java.home}/lib/rt.jar</lib>
                </libs>

                <options>
                    <option>-keepdirectories</option>
                    <option>-keeppackagenames org.apache.maven.plugin.my.MyMojo</option>
                    <option>-keepnames class * implements org.apache.maven.plugin.AbstractMojo</option>
                    <option>-keepclassmembers class * implements org.apache.maven.plugin.AbstractMojo {
                        private <![CDATA[<fields>]]>;
                        private <![CDATA[<methods>]]>;
                    }
                    </option>
                    <option>-keepparameternames</option>
                    <option>-renamesourcefileattribute SourceFile</option>
                    <option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
                        SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
                    </option>
                    <option>-target 1.8</option>
                    <option>-keep public class * {
                        public protected *;
                        }
                    </option>
                    <option>-keepclassmembernames class * {
                        java.lang.Class class$(java.lang.String);
                        java.lang.Class class$(java.lang.String, boolean);
                        }
                    </option>
                    <option>-keepclasseswithmembernames,includedescriptorclasses class * {
                        native <![CDATA[<methods>]]>;
                        }
                    </option>
                    <option>-keepclassmembers,allowoptimization enum * {
                        public static **[] values();
                        public static ** valueOf(java.lang.String);
                        }
                    </option>
                    <option>-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();
                        }
                    </option>
                </options>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>net.sf.proguard</groupId>
            <artifactId>proguard-base</artifactId>
            <version>${tool.proguard.version}</version>
        </dependency>
    </dependencies>
</plugin>  


注释

链接问题(9-04-2019)


Notes

Link Issues (9-04-2019)

Proguard网站存在某种问题,这意味着我到程序选项的链接并不总是像应包含锚点的链接那样转到锚点的位置.如果您最初看到的选项没有显示在他们的网页上,只需稍微向上滚动即可.

The Proguard website has some sort of issue that means my links to the program options are not always going to the location of the anchor the way they should be for an anchor-containing link. Simply slightly scroll upwards if you don't see the option you clicked on being initially displayed on their webpage.

proguard-maven-plugin的版本问题(9-04-2019)

截至2017年3月,com.github.wvengen:proguard-maven-plugin的当前版本为2.0.14,直到进行此编辑为止.我将保留原始配置的版本号2.0.13,因为版本2.0.14包含潜在的重大更改.现在,它包含种子文件和映射文件,作为最终Proguard模糊处理工件的输出工件的一部分.大多数用例实际上不太可能在使用工件中的其他文件时出现任何问题,但是我不是忍者编辑配置以指向2.0.14,而是留了本说明并让您评估哪个版本适合您的项目. .也就是说,只需将版本更改为<version>2.0.14</version>就可以了,因为在版本2.0.14的提交历史记录中未记录任何插件配置更改.

The current version of the com.github.wvengen:proguard-maven-plugin is 2.0.14 as of March 2017 until the making of this edit. I will be leaving the original configuration intact with version number 2.0.13 because version 2.0.14 contains a potentially breaking change. It now includes the seed and map files as part of the output artifact of the final Proguard obfuscated artifact. It is unlikely that most use cases would actually have any issue consuming additional files in the artifact, but instead of ninja editing the configuration to point to 2.0.14, I am instead leaving this note and letting you evaluate which version is appropriate for your project. That said, simply altering the version to <version>2.0.14</version> should be okay, as no plugin configuration changes are noted in the commit history of version 2.0.14.

这篇关于在构建"maven-plugin"插件时如何使用Proguard混淆包裹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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