阴影,proguard和appassembler maven插件的组合

时间:2017-02-28 10:13:43

标签: maven proguard maven-shade-plugin appassembler

我正在尝试使用maven构建和混淆多模块项目。我使用shade插件创建一个包含我自己的所有类文件(每个模块)的胖jar,这样我就可以使用proguard-maven-plugin对fat jar进行模糊处理,然后使用appassembler插件创建可执行的构建输出。一切正常,除了其他模块依赖项也出现在appassembler repo dir中,这是错误的,因为模糊的jar中已经存在混淆的类。

我已经尝试定义提供的其他模块依赖项,然后为shade插件添加依赖项,但是shade插件似乎忽略了它们。

这是pom.xml的相关部分:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <outputFile>${project.build.directory}/${project.build.finalName}-shaded.${project.packaging}</outputFile>
                        <artifactSet>
                            <includes>
                                <include>${project.groupId}:*</include>
                            </includes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>module-a</artifactId>
                    <version>${project.version}</version>
                </dependency>
                <dependency>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>module-b</artifactId>
                    <version>${project.version}</version>
                </dependency>
            </dependencies>
        </plugin>

        <plugin>
            <groupId>com.github.wvengen</groupId>
            <artifactId>proguard-maven-plugin</artifactId>
            <version>2.0.13</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>proguard</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <injar>${project.build.finalName}-shaded.${project.packaging}</injar>
                <outjar>${project.build.finalName}.${project.packaging}</outjar>
                <proguardInclude>proguard.pro</proguardInclude>
                <maxMemory>1024m</maxMemory>
                <libs>
                    <lib>${java.home}/lib/rt.jar</lib>
                    <lib>${java.home}/lib/jce.jar</lib>
                </libs>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>2.0.0</version>
            <executions>
                <execution>
                    <id>assemble</id>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                    <configuration>
                        <programs>
                            <program>
                                <mainClass>my.package.Application</mainClass>
                            </program>
                        </programs>
                        <useWildcardClassPath>true</useWildcardClassPath>
                        <repositoryLayout>flat</repositoryLayout>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

欢迎任何想法。

2 个答案:

答案 0 :(得分:1)

我找到了一个不像我想的那样方便的解决方案,但它比手动移除其他模块罐更好。我使用了程序集插件从构建分发zip中排除了jar。

的pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <outputFile>${project.build.directory}/${project.build.finalName}-shaded.${project.packaging}</outputFile>
                        <artifactSet>
                            <includes>
                                <include>${project.groupId}:*</include>
                            </includes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>com.github.wvengen</groupId>
            <artifactId>proguard-maven-plugin</artifactId>
            <version>2.0.13</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>proguard</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <injar>${project.build.finalName}-shaded.${project.packaging}</injar>
                <outjar>${project.build.finalName}.${project.packaging}</outjar>
                <proguardInclude>proguard.pro</proguardInclude>
                <maxMemory>1024m</maxMemory>
                <libs>
                    <lib>${java.home}/lib/rt.jar</lib>
                    <lib>${java.home}/lib/jce.jar</lib>
                </libs>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>2.0.0</version>
            <executions>
                <execution>
                    <id>assemble</id>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                    <configuration>
                        <programs>
                            <program>
                                <mainClass>my.package.Application</mainClass>
                            </program>
                        </programs>
                        <useWildcardClassPath>true</useWildcardClassPath>
                        <repositoryLayout>flat</repositoryLayout>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>descriptor.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

descriptor.xml:

<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
      xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>distribution</id>
<formats>
    <format>zip</format>
</formats>
<fileSets>
    <fileSet>
        <directory>${project.build.directory}/appassembler</directory>
        <excludes>
            <exclude>**/module-a-${project.version}.${project.packaging}</exclude>
            <exclude>**/module-b-${project.version}.${project.packaging}</exclude>
        </excludes>
        <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>

答案 1 :(得分:0)

我认为您的问题来自于阴影jar和appassembler在同一阶段 package 中运行。

我认为你应该尝试将appassembler插件的阶段修改为:

<phase>post-package</phase>