0

I'm trying to build and obfuscate a multi module project using maven. I use the shade plugin to create a fat jar containing all of my own class files(every module) so that I could obfuscate the fat jar using proguard-maven-plugin and then create executable build output using appassembler plugin. everything works except that the other module dependencies also appear in the appassembler repo dir, which is wrong because the obfuscated classes already exist in the shaded jar.

I've tried defining the other module dependencies as provided and then adding the dependencies for the shade plugin, but the shade plugin seems to ignore them.

this is the relevant part of 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>

Any ideas are welcome.

raven
  • 775
  • 6
  • 22
  • Where is your pom.xml? What command did you use? Can you provide the maven output? – Adonis Feb 28 '17 at 10:53
  • @Asettouf mvn clean install. build was successful but the shaded jar does not contain module-a and module-b classes – raven Feb 28 '17 at 11:26

2 Answers2

1

I found a solution which is not as convenient as I'd like but its better than removing the other module jars manually. I used assembly plugin to exclude the jars from the build distribution 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>

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>

raven
  • 775
  • 6
  • 22
0

I think your issue comes from the fact that the shaded jar and the appassembler are ran during the same phase, package.

I think you should try to modify the phase of the appassembler plugin to:

<phase>post-package</phase>
Adonis
  • 4,670
  • 3
  • 37
  • 57
  • I don't think that maven has post-package phase. but I don't think that's the problem since the ${project.build.finalName}-shaded.${project.packaging} file in my target also does not contain module-a and module-b classes. AFAIK maven executes same phase executions in the order they are seen in the pom – raven Feb 28 '17 at 14:34
  • Please see: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html. For every phase X, you have a pre-X, post-X and process-X phases. If you don't want to use post-package, you can use the install phase though – Adonis Feb 28 '17 at 14:37
  • I did not find what you mentioned in that link. see http://stackoverflow.com/questions/26607834/maven-lifecycle-vs-phase-vs-plugin-vs-goal. but nevertheless building the project in post-package or install phase for appassembler(or any other random word) strangely did not result in any error but still resulted in the same output. – raven Feb 28 '17 at 14:51
  • What command did you use to run maven? What is the output of the command? – Adonis Feb 28 '17 at 15:03
  • `mvn clean install`. though the output is quite huge considering its a big project and proguard. but the build is a success and the shade plugin output consists of lines like `[INFO] Excluding org.springframework.boot:spring-boot-starter-actuator:jar:1.5.1.RELEASE from the shaded jar.` that are justified by my `${project.groupId}:*` config. – raven Mar 01 '17 at 05:56