You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Maven项目中导出依赖JAR包与自定义资源压缩包?

解决方案:导出依赖JAR并保留资源Zip包

要实现构建时同时生成appToReutilize-2.0.jar和resources.zip,只需在现有pom.xml中添加maven-dependency-plugin,通过它将指定依赖复制到构建输出目录(target)即可,无需修改原有的assembly配置。

修改后的pom.xml配置

<groupId>com.me</groupId>
<artifactId>myApp</artifactId>
<version>1.0</version>

<dependencies>
  <dependency>
    <groupId>com.someone</groupId>
    <artifactId>appToReutilize</artifactId>
    <version>2.0</version>
  </dependency>
</dependencies>

<build>
    <plugins>
        <!-- 原有的assembly插件,保持不变 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <descriptors>
                    <descriptor>assembly.xml</descriptor>
                </descriptors>
                <finalName>resources</finalName>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <id>properties-config</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- 新增:复制指定依赖JAR到target目录 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.6.0</version>
            <executions>
                <execution>
                    <id>copy-dependency-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.someone</groupId>
                                <artifactId>appToReutilize</artifactId>
                                <version>2.0</version>
                                <!-- 保留原JAR文件名,不重命名 -->
                                <outputDirectory>${project.build.directory}</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

配置说明

  • maven-dependency-plugin的copy目标专门用于从本地仓库复制指定依赖到指定路径
  • <artifactItem>精准匹配你需要导出的appToReutilize依赖,确保只复制目标JAR
  • <outputDirectory>设置为${project.build.directory}(即target目录),让导出的JAR和resources.zip处于同一目录下

验证效果

执行命令:

mvn clean package

完成后,在target目录下会同时生成两个文件:

  • appToReutilize-2.0.jar(从本地仓库复制的依赖JAR)
  • resources.zip(原assembly插件打包的资源文件)

内容的提问来源于stack exchange,提问作者Alexandre Tavares

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 16:40:57