阴影,proguard和Appassembler Maven插件的组合



我正在尝试使用Maven构建和混淆多模块项目。我使用Shade插件来创建一个胖罐,其中包含我自己的所有类文件(每个模块),以便我可以使用Proguard-Maven-Plugin混淆胖罐,然后使用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>

欢迎任何想法。

我找到了一个解决方案,它不像我想要的那样方便,但比手动卸下其他模块罐要好。我使用汇编插件将罐子排除在构建分布zip中。

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>

描述符。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>

我认为您的问题来自以下事实:阴影罐和Appassembler在同一阶段运行, package

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

<phase>post-package</phase>

最新更新