使用 Maven Assembly Plugin 打包包含 System Path 依赖、普通依赖和 Classpath 下类的 JAR 包

要在 Maven 的 pom.xml 文件中使用 maven-assembly-plugin 插件打包一个包含 System Path 依赖、普通依赖和 Classpath 下类的 JAR 包,可以按照以下步骤进行配置:

  1. pom.xml 文件的 <build> 标签下添加 <plugins> 标签,用于配置插件:
<build>
    <plugins>
        <!-- 其他插件配置 -->
        
        <!-- maven-assembly-plugin 插件配置 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <descriptorRefs>
                    <!-- 使用 jar-with-dependencies 描述符 -->
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <!-- 设置打包文件名 -->
                    <manifest>
                        <mainClass>com.example.MainClass</mainClass>
                    </manifest>
                </archive>
                <appendAssemblyId>false</appendAssemblyId>
                <finalName>my-jar-with-dependencies</finalName>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. <dependencies> 标签下添加需要的依赖项,包括 System Path 依赖和普通依赖:
<dependencies>
    <!-- System Path 依赖 -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>example</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/example.jar</systemPath>
    </dependency>
    
    <!-- 普通依赖 -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>example</artifactId>
        <version>1.0</version>
    </dependency>
    
    <!-- 其他依赖 -->
</dependencies>
  1. 确保 Classpath 下的类在 <build> 标签内的 <resources> 标签中配置正确的目录:
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <!-- 其他资源目录配置 -->
    </resources>
    <!-- 其他配置 -->
</build>
  1. 执行 mvn package 命令进行打包,生成的 JAR 包将包含 System Path 依赖、普通依赖和 Classpath 下的类。

注意:

  • 在上述步骤中,需要将 com.example.MainClass 替换为实际的主类名,并将 com.example 替换为实际的依赖项的 groupId 和 artifactId。
  • 此外,finalName 用于指定生成的 JAR 包的文件名,可以根据需要进行修改。
Maven Assembly Plugin: 打包包含 System Path 依赖、普通依赖和 Classpath 下类的 JAR 包

原文地址: https://www.cveoy.top/t/topic/zY2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录