How to exclude unused classes when assembling a Maven p…

During Maven packaging, ProGuard can be used to remove unused classes. ProGuard is a tool for optimizing Java bytecode, which can remove unreferenced classes, methods, and fields to reduce the size of the packaged file.

To use ProGuard to remove unused classes, you can follow these steps:

  1. Integrate the ProGuard plugin into the project’s pom.xml file.
<build>
    <plugins>
        <plugin>
            <groupId>com.github.wvengen</groupId>
            <artifactId>proguard-maven-plugin</artifactId>
            <version>2.2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>proguard</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <proguardVersion>6.2.2</proguardVersion>
                <options>
                    <option>-dontshrink</option>
                    <option>-dontoptimize</option>
                </options>
                <injar>${project.build.finalName}.jar</injar>
                <outjar>${project.build.finalName}-proguard.jar</outjar>
            </configuration>
        </plugin>
    </plugins>
</build>
  1. Run Maven command for packaging:
mvn clean package
  1. The jar file created for the project will be named ${project.build.finalName}-proguard.jar.

Note: ProGuard can only remove unused classes in the compilation phase, so it is recommended to run the Maven command before each packaging to ensure that the generated JAR file is minimized.

bannerAds