Five Methods of Slimming jar Package in maven Project

  • 2021-08-16 23:48:51
  • OfStack

maven tools are often used in java projects for project management, but one of the problems often encountered is that the generated jar packages are getting bigger and bigger, and the compilation of the first project is getting slower and slower. How to effectively remove redundant dependence and slim down jar package is a necessary skill. Here are five ways to slim down the jar package in the maven project:

1. Set scope to provided for dependency packages already included in the environment

Some of the dependent packages in pom may already be included in your program runtime environment, so you should set scope of the dependent package to provided at this time. If the protobuf package is included in the environment, it should be set to:


<dependency>
  <groupId>com.google.protobuf</groupId>
  <artifactId>protobuf-java</artifactId>
  <version>${protobuf.version}</version>
  <scope>provided</scope>
</dependency>

2. Remove unused dependencies

Some dependencies that may be added during pom file configuration are not really used, so is there an effective way to find dependencies that are not used? The answer is Apache Maven Dependency Plugin.

Install the Apache Maven Dependency Plugin plug-in and run the mvn dependency: analyze command to analyze project dependencies and determine which dependencies are used and declared, used and undeclared, and unused and declared. Remove unused and declared dependencies from the pom file.

If you are using Spring Boot, you can add this plug-in directly


<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
    </plugin>
  </plugins>
</build>

3. Remove duplicate dependencies

Some dependencies in the pom file may be found in the <dependencies/> Or <dependencyManagement/> Or the parent-child project configuration may be duplicated with 1 dependency package. Available through Apache Maven Dependency Plugin Plug-in and run mvn dependency:analyze-duplicate Command to check the duplicate dependencies of the project, and then delete the duplicate dependencies.

4. Resolve dependency conflicts

Different dependency packages in a project can depend on another package at the same time, and this nested dependency package may not have a version, which may cause the program to fail to run normally, or cause some strange problems during the running process.

How can all dependency conflicts be easily found and resolved? Installing the maven helper plug-in in intellij can quickly identify conflicting packages.

After installing maven helper, open pom file, switch from Text mode to Dependency Analyzer mode, select Conflicts button, and you can clearly see that all conflicting dependency packages are marked red.

Select the red nested package and right-click to select Exclue. The actual effect is to get the nested package exclusion out under the corresponding dependency in pom file.


<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>${hibernate.manager}</version>
  <exclusions>
    <exclusion>
      <artifactId>jboss-logging</artifactId>
      <groupId>org.jboss.logging</groupId>
    </exclusion>
  </exclusions>
</dependency>

In some scenarios, different versions of exclusion conflicting dependencies can cause programs to fail to execute, possibly because the dependencies do not work properly under other versions of their nested packages. At this point, you may want to try to find a new version of the dependency package so that its dependent nested package can be compatible with other dependencies.

5. Remove the specified file

If you can't achieve the goal of effectively slimming the jar package after taking the above steps, you can only use the final killer: remove the unnecessary specified files or folders from the final jar package.

The reason why unnecessary files are hit to jar package is that 1 aspect may include 1 non-code engineering files, such as project documents; Another aspect may be that the jar package you rely on contains unnecessary redundant files.

Use maven-shade-plugin Plugins can remove files that match specific criteria from jar packages. As shown below, the Configure exclude entry removes files or folders (either configuration files or code files) that match specific criteria from the final jar package.


<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.4.3</version>
  <executions>
    <execution>
      <id>uber-jar</id>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <finalName>${project.artifactId}-${project.version}-jar-with-dependencies</finalName>
        <filters>
          <filter>
            <artifact>*:* </artifact>
            <excludes>
              <exclude>LICENSE</exclude>
              <exclude>META-INF/*.SF</exclude>
              <exclude>META-INF/*.DSA</exclude>
              <exclude>META-INF/*.RSA</exclude>
            </excludes>
          </filter>
        </filters>
      </configuration>
    </execution>
  </executions>
</plugin>

The above is the maven project in the jar package slimming 5 methods of the details, more about the maven project in the jar package slimming information please pay attention to other related articles on this site!


Related articles: