Use Maven war package to package and remove jar package to reduce weight

Time:2022-5-4

1. POM file configuration

1.1 add Maven dependency plugin to copy the referenced jar package to the specified path

It is convenient to specify the dependent package path for subsequent Tomcat startup

<!-- Copy the dependency to the Lib directory outside the jar -- >
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>                <!-- Specified dependency path -- >
                <outputDirectory>
                    ${project.build.directory}/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

After using this plug-in to build the directory structure, there is an additional lib directory (i.e. the path specified in the outputdirectory configured above), which is the dependent jar package:

1.2 add Maven war plugin

<!--  War package -- >
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <packagingExcludes>
            <!-- Exclude specific jar packages -- >
            <!--WEB-INF/lib/commons-io-2.5.jar,-->
            <!--WEB-INF/lib/commons-lang3-3.6.jar,-->
            <!-- Exclude all jar packages that do not start with Commons -- >
            <!--%regex[WEB-INF/lib/(?!commons-).*.*.jar]-->
            <!-- Exclude all jar packages starting with Commons -- >
            %regex[WEB-INF/lib/(commons-).*.*.jar]
        </packagingExcludes>
    </configuration>
</plugin>

The war package printed after the above configuration will remove the corresponding jar package under / WEB-INF / lib / in the war package, so as to reduce the size of the release package and speed up the upload server. As shown in the figure below, pressurize the war package and check the WEB-INF / lib directory. There is no jar package beginning with commons, which indicates that it has been removed:

The original war package contains many jars starting with commons, as shown in the following figure:

According to the above ideas, we can remove all jars under lib. After removing the dependent jars, we can configure themTomcat refers to the external lib pathTo deploy the project

note appended:

If there are different version dependencies: for example, project a depends on version 1.0 of Library y, and project B depends on version 2.0 of Library y, version dependency conflicts may occur (when the two versions are incompatible)

Solution:

1. Keep the same version if you can keep the same version, and try to ensure that multiple projects use the same version of the same library

2. Let each project depend on the required version and put it into the war package. Put other jar packages of the same version under the same shared package

The test found that the dependency is found from top to bottom when searching, and the first one is used when matching

As shown in the figure below, comm-0.0.1 will be used Jar version

Spring boot packaging plug-in removes jar package and slims down

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer.

Recommended Today

Implementation example of adding permissions with laravel and Vue

catalogue 1、 Add (permission) node 1.1 introduction of Vue 1.2. Add templates (Vue, jQuery) 1.3. Add node controller logic 1.4. Define modifiers in the model 1.5. Effect: 1、 Add (permission) node 1.1 introduction of Vue usevueManage the interface and realize the addition functionWe put it in this directoryvue.js: 1.2. Add templates (Vue, jQuery) resources/admin/node/create.blade.php: <!–_ […]