kezhenxu94/chatgpt-java

Q: how to use proguard maven plugin in multi-module project

Opened this issue · 5 comments

Q: how to use proguard maven plugin in multi-module project
______________        _____________________________
__  ____/__  /_______ __  /__  ____/__  __ \__  __/
_  /    __  __ \  __ `/  __/  / __ __  /_/ /_  /
/ /___  _  / / / /_/ // /_ / /_/ / _  ____/_  /
\____/  /_/ /_/\__,_/ \__/ \____/  /_/     /_/

2023-03-10T10:28:45.973Z WARN 1668 --- [ main] org.jline : Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)
You are now at conversation: issue-26
System message set to: ""
To use Proguard Maven plugin in a multi-module project, you will need to add it to the respective module's pom.xml file. Here are the steps to follow:

  1. Add the plugin in the plugins section of the pom.xml file of the desired module. Here is an example configuration:

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <version>2.0.14</version>
                <executions>
                    <execution>
                        <id>proguard</id>
                        <phase>package</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                        <configuration>
                            <injar>${project.build.finalName}.jar</injar>
                            <obfuscate>true</obfuscate>
                            <attach>true</attach>
                            <options>
                                <option>-dontoptimize</option>
                                <option>-keep public class com.example.module.** { *; }</option>
                                <!-- more options can be specified here -->
                            </options>
                            <libs>
                                <lib>${java.home}/lib/rt.jar</lib>
                            </libs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
  2. Specify the injar configuration to point to the input JAR file that should be processed by Proguard. In this example, the injar value is set to ${project.build.finalName}.jar.

  3. Specify the desired Proguard options in the options section. Here, we have set three options, including -dontoptimize that disables optimization, -keep public class com.example.module.** { *; } that keeps all public classes in the com.example.module package and sub-packages, and more can be added.

  4. Run the mvn package command to package the module with Proguard obfuscation.

Note: If you want to apply Proguard to multiple modules, you will need to add the plugin configuration to each of the module's pom.xml files.

Please rewrite the configuration, move the plugin configuration into pluginManagement in the root module, and so we can only add the plugin definition without duplicating the configurations in every sub module.

______________        _____________________________
__  ____/__  /_______ __  /__  ____/__  __ \__  __/
_  /    __  __ \  __ `/  __/  / __ __  /_/ /_  /
/ /___  _  / / / /_/ // /_ / /_/ / _  ____/_  /
\____/  /_/ /_/\__,_/ \__/ \____/  /_/     /_/

2023-03-10T10:31:11.489Z WARN 1688 --- [ main] org.jline : Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)
You are now at conversation: issue-26
Sure, here's how you can move the Proguard plugin configuration to pluginManagement in the parent/root module and avoid duplicating the configuration across all submodules:

  1. In the parent/root module's pom.xml, add the plugins section inside the build/pluginManagement element, like this:
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <version>2.0.14</version>
                <executions>
                    <execution>
                        <id>proguard</id>
                        <phase>package</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                        <configuration>
                            <injar>${project.build.finalName}.jar</injar>
                            <obfuscate>true</obfuscate>
                            <attach>true</attach>
                            <options>
                                <option>-dontoptimize</option>
                                <option>-keep public class com.example.module.** { *; }</option>
                                <!-- more options can be specified here -->
                            </options>
                            <libs>
                                <lib>${java.home}/lib/rt.jar</lib>
                            </libs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
  1. In the submodules that you want to apply Proguard to, add the following code to their build/plugins section:
<build>
    <plugins>
        <plugin>
            <groupId>com.github.wvengen</groupId>
            <artifactId>proguard-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>proguard</id>
                    <phase>package</phase>
                    <goals>
                        <goal>proguard</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Note that in the submodules, you only need to add the plugin definition with minimal configuration. That's because the configuration is already defined in the parent/root module's pluginManagement section.

With these changes, the Proguard plugin is now configured in the parent module and can be used in any submodule without duplicating the configuration.

Can we even omit the executions part in sub-modules as it is defined in parent module already?

______________        _____________________________
__  ____/__  /_______ __  /__  ____/__  __ \__  __/
_  /    __  __ \  __ `/  __/  / __ __  /_/ /_  /
/ /___  _  / / / /_/ // /_ / /_/ / _  ____/_  /
\____/  /_/ /_/\__,_/ \__/ \____/  /_/     /_/

2023-03-10T10:34:06.180Z WARN 1683 --- [ main] org.jline : Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)
You are now at conversation: issue-26
Yes, you can omit the executions part in the sub-modules since it's already defined in the parent module's pluginManagement section. Your sub-module's pom.xml file should only contain the following plugin configuration:

<build>
    <plugins>
        <plugin>
            <groupId>com.github.wvengen</groupId>
            <artifactId>proguard-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

This way, you don't need to repeat the execution configuration in every sub-module's pom.xml. The pluginManagement section in the parent module sets up the plugin configuration, including its executions, which can be inherited by the sub-modules.