我有一个工作项目,想要混淆我公司的jar。 该项目是基于maven(版本2.2.1)的webservice(JRE 1.6.0.45)。它运作得很好。
要使用proguard对其进行模糊处理,我将以下内容添加到我的POM文件中:
...
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
<jdk.version>1.6</jdk.version>
<slf4j.version>1.7.7</slf4j.version>
<logback.version>1.1.2</logback.version>
<maven.repo>C:\Java\Workspaces\Maven2-local-repository</maven.repo>
<java.home>C:\Java\Tools\jdk1.6.0_45\jre</java.home>
<proguard.needed.jars>C:\Java\Workspaces\myApp\ProGuardNeededFiles</proguard.needed.jars>
</properties>
...
...
...
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.8</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscate>true</obfuscate>
<includeDependency>false</includeDependency>
<injar>test.war</injar>
<outjar>test_mini.war</outjar>
<outputDirectory>${project.build.directory}</outputDirectory>
<maxMemory>256m</maxMemory>
<libs>
<!-- Java Runtime -->
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jsse.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
<!-- Dependency of a dependency library -->
<lib>${proguard.needed.jars}/groovy-all-1.5.3.jar</lib>
<lib>${proguard.needed.jars}/droovy-1.0-jar-with-dependencies.jar</lib>
<lib>${proguard.needed.jars}/jboss-j2ee-4.0.2.jar</lib>
<lib>${proguard.needed.jars}/log4j-1.2.11.jar</lib>
<lib>${proguard.needed.jars}/log4j-jboss-logmanager-1.1.1.Final.jar</lib>
<lib>${proguard.needed.jars}/classmate-1.1.0.jar</lib>
<lib>${proguard.needed.jars}/geronimo-validation_1.0_spec-1.0-CR5.jar</lib>
<lib>${proguard.needed.jars}/dom4j-1.4.jar</lib>
<lib>${proguard.needed.jars}/pull-parser-2.1.10.jar</lib>
<lib>${proguard.needed.jars}/xml-apis-2.0.2.jar</lib>
<lib>${proguard.needed.jars}/jboss-logmanager-1.2.1.GA.jar</lib>
<lib>${proguard.needed.jars}/xsdlib-2013.6.1.jar</lib>
<lib>${proguard.needed.jars}/joda-time-2.5.jar</lib>
<lib>${proguard.needed.jars}/logkit-1.3.jar</lib>
<lib>${proguard.needed.jars}/avalon-framework-api-4.3.1.jar</lib>
<lib>${proguard.needed.jars}/commons-jxpath-1.1.jar</lib>
<lib>${proguard.needed.jars}/commons-codec-1.1.jar</lib>
<lib>${proguard.needed.jars}/orai18n-1.0.jar</lib>
<lib>${proguard.needed.jars}/javax.mail-api-1.5.2.jar</lib>
<lib>${proguard.needed.jars}/relaxngDatatype-1.0.jar</lib>
<lib>${proguard.needed.jars}/javolution-5.4.5.jar</lib>
<lib>${proguard.needed.jars}/groovy-2.2.2.jar</lib>
<lib>${proguard.needed.jars}/sax-2.0.1.jar</lib>
<lib>${proguard.needed.jars}/dom-2.3.0-jaxb-1.0.6.jar</lib>
<lib>${proguard.needed.jars}/janino-2.7.6.jar</lib>
<lib>${proguard.needed.jars}/jaxp-api-1.4.5.jar</lib>
<lib>${proguard.needed.jars}/jdi.jar</lib>
<lib>${proguard.needed.jars}/errai-javax-enterprise-3.0.3.Final.jar</lib>
<lib>${proguard.needed.jars}/xdb.jar</lib>
<lib>${proguard.needed.jars}/ons.jar</lib>
<lib>${proguard.needed.jars}/oraclepki.jar</lib>
<lib>${proguard.needed.jars}/asm.jar</lib>
<lib>${proguard.needed.jars}/orai18n.jar</lib>
<lib>${proguard.needed.jars}/commons-beanutils-1.8.3.jar</lib>
<lib>${proguard.needed.jars}/org.apache.servicemix.bundles.xmlpull-1.1.3.4a_1.jar</lib>
<lib>${proguard.needed.jars}/org.apache.servicemix.bundles.jaxen-1.1.6_1.jar</lib>
<lib>${proguard.needed.jars}/ow2-cdi-1.0-spec-1.0.13.jar</lib>
<lib>${proguard.needed.jars}/xmlparserv2.jar</lib>
<lib>${proguard.needed.jars}/commons-compiler-2.6.1.jar</lib>
<lib>${proguard.needed.jars}/javax.interceptor-api-1.2.jar</lib>
<!-- Libraries added within the WAR file -->
<lib>${maven.repo}/antlr/antlr/2.7.7/antlr-2.7.7.jar</lib>
<lib>${maven.repo}/commons-beanutils/commons-beanutils/1.7.0/commons-beanutils-1.7.0.jar</lib>
<lib>${maven.repo}/commons-beanutils/commons-beanutils-core/1.8.0/commons-beanutils-core-1.8.0.jar</lib>
<lib>${maven.repo}/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar</lib>
<lib>${maven.repo}/commons-digester/commons-digester/1.8/commons-digester-1.8.jar</lib>
<lib>${maven.repo}/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar</lib>
<lib>${maven.repo}/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar</lib>
<lib>${maven.repo}/org/slf4j/slf4j-api/${slf4j.version}/slf4j-api-${slf4j.version}.jar</lib>
<lib>${maven.repo}/ch/qos/logback/logback-classic/${logback.version}/logback-classic-${logback.version}.jar</lib>
<lib>${maven.repo}/ch/qos/logback/logback-core/${logback.version}/logback-core-${logback.version}.jar</lib>
<lib>${maven.repo}/com/mycompany/application_A-1.0.0.jar</lib>
<lib>${maven.repo}/com/mycompany/application_B-2.0.0.jar</lib>
<lib>${maven.repo}/commons-configuration/commons-configuration/1.6/commons-configuration-1.6.jar</lib>
<lib>${maven.repo}/junit/junit/3.8.1/junit-3.8.1.jar</lib>
<lib>${maven.repo}/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar</lib>
<lib>${maven.repo}/commons-lang/commons-lang/2.6/commons-lang-2.6.jar</lib>
<lib>${maven.repo}/org/hibernate/hibernate-core/4.3.6.Final/hibernate-core-4.3.6.Final.jar</lib>
<lib>${maven.repo}/org/hibernate/common/hibernate-commons-annotations/4.0.5.Final/hibernate-commons-annotations-4.0.5.Final.jar</lib>
<lib>${maven.repo}/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar</lib>
<lib>${maven.repo}/org/javassist/javassist/3.18.2-GA/javassist-3.18.2-GA.jar</lib>
<lib>${maven.repo}/org/codehaus/jackson/jackson-mapper-asl/1.9.13/jackson-mapper-asl-1.9.13.jar</lib>
<lib>${maven.repo}/org/codehaus/jackson/jackson-core-asl/1.9.13/jackson-core-asl-1.9.13.jar</lib>
<lib>${maven.repo}/cn/guoyukun/jdbc/oracle-ojdbc6/11.2.0.3.0/oracle-ojdbc6-11.2.0.3.0.jar</lib>
<lib>${maven.repo}/com/microsoft/sqlserver/sqljdbc4/4.0/sqljdbc4-4.0.jar</lib>
<lib>${maven.repo}/org/jboss/jandex/1.1.0.Final/jandex-1.1.0.Final.jar</lib>
<lib>${maven.repo}/org/jboss/logging/jboss-logging/3.1.3.GA/jboss-logging-3.1.3.GA.jar</lib>
<lib>${maven.repo}/org/jboss/logging/jboss-logging-annotations/1.2.0.Beta1/jboss-logging-annotations-1.2.0.Beta1.jar</lib>
<lib>${maven.repo}/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.0.0.Final/jboss-transaction-api_1.2_spec-1.0.0.Final.jar</lib>
<lib>${maven.repo}/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2.jar</lib>
</libs>
<options>
<option>-allowaccessmodification</option>
<option>-dontskipnonpubliclibraryclasses</option>
<option>-dontskipnonpubliclibraryclassmembers</option>
<option>-keepattributes *Annotation*</option>
<option>-keepattributes Signature</option>
<option>-keepattributes InnerClasses</option>
<option>-keepattributes InnerClasses,EnclosingMethod</option>
<option>-keepattributes EnclosingMethod</option>
<option>-dontshrink</option>
<option>-dontoptimize</option>
<option>-dontnote</option>
<option>-dontwarn org.w3c.dom.**</option>
<option>-dontwarn javax.xml.transform.**</option>
<option>-dontwarn javax.transaction.xa.**</option>
<option>-dontwarn org.dom4j.io.**</option>
<option>-dontwarn org.xml.sax.**</option>
<option>-dontwarn oracle.sql.**</option>
<option>-dontwarn groovy.servlet.**</option>
<option>-dontwarn oracle.i18n.servlet.**</option>
<option>-dontwarn oracle.xdb.servlet.**</option>
<option>-dontwarn oracle.xml.jaxp.**</option>
<option>-dontwarn org.apache.xerces.jaxp.**</option>
<option>-dontwarn org.codehaus.groovy.antlr.**</option>
<option>-dontwarn com.sun.org.apache.xerces.internal.jaxp.**</option>
<!-- <option>-dontwarn com.microsoft.sqlserver.jdbc.**</option> -->
<option>-dontwarn oracle.jdbc.proxy.**</option>
<option>-dontwarn org.apache.commons.beanutils.**</option>
<option>-dontwarn org.apache.commons.configuration.tree.xpath.**</option>
<option>-dontwarn org.dom4j.xpath.XPathPattern</option>
<option>-dontwarn org.jboss.logging.Log4jLogger</option>
<option>-dontwarn oracle.xdb.XMLType</option>
<option>-printseeds</option>
<option>-printmap obfuscation.map</option>
<option>-printusage deadcode.txt</option>
<option>-verbose</option>
<option>-keep class !com.mycompany.** { *; }</option>
<option>-keep class com.mycompany.utility.log.** { *;} </option>
<option>-keep class com.mycompany.listeners.** {*;}</option>
<option>-keep class com.mycompany.controller.** {*;}</option>
<option>-keep @javax.persistence.* class * {*;}</option>
<option>-keep class * implements java.sql.Driver</option>
</options>
</configuration>
首先,我为微软课程添加了 dontwarn 选项。它成功构建,但在Tomcat中运行后,我访问数据库时遇到错误。
引起:java.lang.ClassNotFoundException:无法加载请求的类:com.microsoft.sqlserver.jdbc.SQLServerDriver
所以我删除了 dontwarn 选项,并在编译项目后找到了以下信息:
...
...
[proguard] Reading library jar [C:\Java\Tools\jdk1.6.0_45\jre\lib\rt.jar]
...
...
[proguard] Initializing...
[proguard] Warning: com.microsoft.sqlserver.jdbc.SQLServerSQLXML: can't find referenced method 'void setFeature(java.lang.String,boolean)' in program class javax.xml.parsers.DocumentBuilderFactory
[proguard] Warning: there were 1 unresolved references to program class members.
[proguard] Your input classes appear to be inconsistent.
[proguard] You may need to recompile the code.
[proguard] (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)
[proguard] java.io.IOException: Please correct the above warnings first.
[proguard] at proguard.Initializer.execute(Initializer.java:473)
[proguard] at proguard.ProGuard.initialize(ProGuard.java:222)
[proguard] at proguard.ProGuard.execute(ProGuard.java:97)
[proguard] at proguard.ProGuard.main(ProGuard.java:507)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
...
...
...
这很奇怪,因为方法 setFeature(java.lang.String,boolean)是rt.jar库中的默认值。
通过在$ {proguard.needed.jars}文件夹中添加库,我确实删除了所有其他错误。我还需要设置一个dontwarn设置列表,因为依赖类中缺少我的应用程序未使用的方法(想想ProGuard工具中的一个缺陷)。
有人遇到同样的问题并想出来了吗?
答案 0 :(得分:0)
您是否查阅了ProGuard手册?
Usage, -libraryjars
:&#34;指定的库jar应该至少包含由应用程序类文件扩展的类文件。仅存在的库类文件不存在,尽管它们的存在可以改善优化步骤的结果。&#34;
您不扩展您在&lt; lib&gt;中指定的所有广告,是吗?删除仅调用以首先减少可能出现问题的来源数量。
Usage, -dontwarn
:&#34;指定不要警告未解决的引用和其他重要问题。 [...]忽略警告可能很危险。 [...]如果您知道自己在做什么,请仅使用此选项!&#34; 。
在尝试压制它们之前,我试图摆脱有关未解决的引用和其他重要问题的警告。
答案 1 :(得分:0)
数据库驱动程序通过反射加载。 ProGuard无法知道,因此您需要明确保留它:
-keep class * implements java.sql.Driver
请参阅ProGuard手册&gt;例子&gt; Processing database drivers
请参阅ProGuard手册&gt;疑难解答&gt;运行时的问题&gt; ClassNotFoundException
关于方法的警告(“......在程序类中找不到......”)表明你的程序中有javax.xml.parsers.DocumentBuilderFactory的副本代码或jar(不仅仅是JDK运行时库jar),并且此副本不包含该方法。如果您不禁用注释,ProGuard会打印关于重复类的注释。
ProGuard必须在处理之前检查输入是否一致,因此它可能会抱怨结果未使用的类,字段和方法。处理不一致的输入可能会导致各种意外,如此处所示。