maven extracts dependencies outside the jar package---assembly

Scenes

The jar file generated by the project is too large, mainly because there are too many dependencies. Every time the code is updated, it takes a long time to upload these dependencies repeatedly when packaging and deploying. Therefore, consider extracting the dependencies outside the jar package. In this way, dependencies and source code are uploaded separately. For each update iteration, as long as the dependencies remain unchanged, you only need to upload the jar package compiled from the source code during deployment.

pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <!-- 配置启动类 -->
                            <mainClass>
                                com.example.TestApplication
                            </mainClass>
                            <!-- 添加依赖路径 -->
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
<!--                        <manifestEntries>-->
<!--                            <Class-Path>./</Class-Path>-->
<!--                        </manifestEntries>-->
                    </archive>
<!--                    <excludes>-->
<!--                        <exclude>config/**</exclude>-->
<!--                    </excludes>-->
                </configuration>
            </plugin>
            <!-- Maven Assembly Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <!-- not append assembly id in release file name -->
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <!--打包的详细描述,需要配置额外文件-->
                        <descriptor>assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <!-- 配置执行器 -->
                    <execution>
                        <id>make-assembly</id>
                        <!-- 绑定到package命令的生命周期上 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 只运行一次 -->
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

assembly/assembly.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 https://maven.apache.org/xsd/assembly-2.1.0.xsd">
    <id>package</id>
    <formats>
        <!--压缩文件的类型-->
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <!--需要包含的文件与输出的路径-->
<!--        <fileSet>-->
<!--            <directory>src/main/bin</directory>-->
<!--            <outputDirectory>bin/</outputDirectory>-->
<!--        </fileSet>-->
<!--        <fileSet>-->
<!--            <directory>src/main/resources</directory>-->
<!--            <outputDirectory>/</outputDirectory>-->
<!--        </fileSet>-->
        <fileSet>
            <!-- 提取jar文件到外部 -->
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <!-- 提前依赖到lib文件夹 -->
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <excludes>
                <exclude>${groupId}:${artifactId}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

Pack

mvn clean package

Generate files
Insert image description here

run

Unzip test-0.0.1.tar.gz
Insert image description here
and execute

java -jar test.0.0.1.jar

Guess you like

Origin blog.csdn.net/weixin_43932590/article/details/128562870