当前位置: 首页 > 工具软件 > LibreSource > 使用案例 >

Maven 分离lib和resource的打包配置

慕容聪
2023-12-01

使用Maven打包并分离resource,只需两步:

1、修改项目根目录下的pom.xml

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <!-- 读取assembly.xml文件 -->
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                    <!-- 输出在target/dist/目录下 -->
                    <outputDirectory>${project.build.directory}/dist/</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- 打包成jar包,并指定lib和resources文件夹位置 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <!-- 入口Main Class -->
                            <mainClass>xxx.xxx.Main</mainClass>
                            <!-- 依赖的lib -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <addClasspath>true</addClasspath>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>resources/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

2、添加assembly.xml在项目根目录下

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>distribution</id>

    <!--最终输出生成zip文件 -->
    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <!--设置需要分离的文件-->
    <fileSets>
        <fileSet>
            <directory>src/main/resources/</directory>
            <!--去除resources下的mybatis文件夹-->
            <excludes>
                <exclude>mybatis/**<exclude>
            </excludes>
            <outputDirectory>/resources</outputDirectory>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <!--依赖包的输出目录-->
            <outputDirectory>/lib</outputDirectory>
            <scope>runtime</scope>
            <excludes>
                <exclude>${project.groupId}:${project.artifactId}</exclude>
            </excludes>
        </dependencySet>
        <dependencySet>
            <!--jar包的输出目录-->
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>${project.groupId}:${project.artifactId}</include>
            </includes>
        </dependencySet>
    </dependencySets>
</assembly>

 类似资料: