Maven包括Proguard与jar-with-dependencies

时间:2017-12-25 17:25:33

标签: java maven proguard

我正在使用Maven来打包我的代码,我想用Proguard来混淆我的代码。所以我偶然发现了这个example并将其添加到我的Pom文件中。但是,当我打包代码(它被混淆),但它没有运行。错误是:

    Exception in thread "main" java.lang.ExceptionInInitializerError
        at e.a.b.i.c.L.h(Unknown Source)
        at e.a.b.i.c.L.<init>(Unknown Source)
        at a.b.a.a.c.b.b(Unknown Source)
        at a.b.a.a.c.b.<clinit>(Unknown Source)
        at a.b.a.a.b.a(Unknown Source)
        at a.b.a.a.b.a(Unknown Source)
        at a.b.a.b.a.d(Unknown Source)
        at d.b.d.a(Unknown Source)
        at d.b.b.a(Unknown Source)
        at d.b.a(Unknown Source)
        at b.c.a(Unknown Source)
        at SuperProgramMain.main(Unknown Source)
Caused by: e.a.a.b.b: java.lang.ClassNotFoundException: org.apache.commons.logging.impl.LogFactoryImpl (Caused by java.lang.ClassNotFoundException: org.apache.commons.logging.impl.LogFactoryImpl)
        at e.a.a.b.c.b(Unknown Source)
        at e.a.a.b.e.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at e.a.a.b.c.a(Unknown Source)
        at e.a.a.b.c.c(Unknown Source)
        at e.a.a.b.c.b(Unknown Source)
        at e.a.b.f.e.a.<init>(Unknown Source)
        at e.a.b.f.e.b.<init>(Unknown Source)
        at e.a.b.f.e.f.<clinit>(Unknown Source)
        ... 12 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.impl.LogFactoryImpl
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 21 more
Exception in thread "Shutdown-thread" java.lang.NullPointerException
        at a.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

这是我目前的Pom文件:

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.SuperProgram</groupId>
    <artifactId>SuperProgramMainValue</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>SuperProgram</name>
    <repositories>
        <repository>
            <id>spongepowered</id>
        <url>https://repo.spongepowered.org/maven</url>
    </repository>
    <repository>
        <id>restlet</id>
        <url>http://maven.restlet.com</url>
    </repository>
</repositories>

<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>templating-maven-plugin</artifactId>
            <version>1.0-alpha-3</version>
            <executions>
                <execution>
                    <id>filter-src</id>
                    <goals>
                        <goal>filter-sources</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>SuperProgramMain</mainClass>
                <cleanupDaemonThreads>false</cleanupDaemonThreads>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>assembly</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>SuperProgramMain</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.github.wvengen</groupId>
            <artifactId>proguard-maven-plugin</artifactId>
            <version>2.0.8</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>proguard</goal></goals>
                    <configuration>
                        <injar>${project.build.finalName}-jar-with-dependencies.jar</injar> <!-- make sure to obfuscate the jar with dependencies -->
                        <proguardVersion>5.3.3</proguardVersion>
                        <options>
                            <option>-allowaccessmodification</option>
                            <option>-dontoptimize</option>
                            <option>-dontshrink</option>
                            <option>-dontnote</option>
                            <option>-dontwarn</option> <!-- added option to ignore com.sun missing classes -->
                            <option>-keepattributes Signature</option>
                            <option>-keep class SuperProgramMain { *; }</option>
                        </options>
                        <libs>
                            <lib>${java.home}/lib/rt.jar</lib>
                        </libs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>net.sf.proguard</groupId>
        <artifactId>proguard-base</artifactId>
        <version>5.3.3</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>net.sourceforge.cssparser</groupId>
        <artifactId>cssparser</artifactId>
        <version>0.9.24</version>
    </dependency>

    <dependency>
        <groupId>com.mashape.unirest</groupId>
        <artifactId>unirest-java</artifactId>
        <version>1.4.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20140107</version>
    </dependency>

    <dependency>
        <groupId>org.jfree</groupId>
        <artifactId>jfreechart</artifactId>
        <version>1.5.0</version>
    </dependency>

    <dependency>
        <groupId>org.twitter4j</groupId>
        <artifactId>twitter4j-core</artifactId>
        <version>[4.0,)</version>
    </dependency>
</dependencies>

公共日志记录依赖项 NOT 包含在模糊的Jar版本中。只在基础版本中..所以我一直得到ClassNotFoundError!

1 个答案:

答案 0 :(得分:0)

我现在通过专门告诉Proguard保留该目录来解决这个问题。我在Proguard选项中指定了这个:

<option>-keep class org.apache.commons.** { *; }</option>

之后,包含了日志记录目录,然后程序正常运行。