SoFunction
Updated on 2025-03-03

Reduce the volume of JAR packages generated by Maven projects to improve operation and maintenance efficiency

When building Java projects using Maven, it is sometimes necessary to reduce the volume of generated JAR packages to increase deployment efficiency or reduce resource consumption.

Here are some effective ways to reduce the volume of a JAR package:

  1. Exclude unnecessary dependencies
  2. When packaging, dependency jar package is independent of application jar package

1. Exclude unnecessary dependencies

By excluding unwanted dependencies in the project, the volume of the JAR package can be significantly reduced.

In , you can use the tag in the tag to exclude specific dependencies.

<dependencies>
    <dependency>
        <groupId></groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.5.4</version>
        <exclusions>
            <exclusion>
                <groupId></groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Other dependencies -->
</dependencies>

2. Depend on jar packages independent of application jar packages

Generally speaking, dependencies do not change frequently, so it is a good idea to package dependencies outside the application and can effectively reduce application packages.

The configuration is as follows:

<!-- When packing Relying on project independentjarBag-->
<plugin>
      <groupId></groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.2.0</version>
      <configuration>
          <archive>
              <manifest>
                  <addClasspath>true</addClasspath>
                  <classpathPrefix>lib/</classpathPrefix>
                  <mainClass></mainClass>
              </manifest>
          </archive>
      </configuration>
  </plugin>
  <plugin>
      <groupId></groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>3.1.2</version>
      <executions>
          <execution>
              <id>copy-dependencies</id>
              <phase>package</phase>
              <goals>
                  <goal>copy-dependencies</goal>
              </goals>
              <configuration>
                  <outputDirectory>${}/lib</outputDirectory>
                  <includeScope>runtime</includeScope>
              </configuration>
          </execution>
      </executions>
  </plugin>

Notice:

  • When the first deployment application or dependency is updated
  • You need to upload the dependency (lib) to the service as well.

The effects are as follows:

├── target
│   ├── classes
│   ├── generated-sources
│   ├── lib
│   ├── maven-archiver
│   ├── maven-status
│   └── web-0.0.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.