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

如何使用Maven将所有依赖源码打包至项目target目录归档?

当然可以实现!我给你分享两种实用的方案,都能帮你把所有依赖的源码打包到项目target目录的归档文件里:

方案一:用maven-dependency-plugin批量拷贝并打包源码

这个插件可以直接把所有依赖的源码Jar拷贝到指定目录,之后你可以选择单独打包这些源码,或者和项目产物合并。

在你的pom.xml中添加如下插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.6.0</version>
            <executions>
                <execution>
                    <id>copy-sources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <!-- 指定只拷贝源码Jar -->
                        <classifier>sources</classifier>
                        <!-- 把源码Jar拷贝到target目录下的sources子目录 -->
                        <outputDirectory>${project.build.directory}/sources</outputDirectory>
                        <!-- 是否包含传递依赖的源码,按需开启 -->
                        <includeScope>compile</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

执行mvn clean package后,所有依赖的源码Jar会被拷贝到target/sources目录。如果想把这些源码打包成单独的归档,可以再配合maven-jar-plugin或者直接用压缩工具打包这个目录。

方案二:用maven-assembly-plugin自定义归档(推荐)

如果想把项目自身的编译产物和依赖源码一起打包成一个完整的归档,这个方案更灵活。

第一步,在项目根目录创建src/main/assembly/sources-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 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
    <id>with-sources</id>
    <formats>
        <!-- 可以选择zip、tar.gz等格式 -->
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <!-- 包含项目自身的编译产物 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <includes>
                <include>${project.build.finalName}.jar</include>
            </includes>
        </fileSet>
        <!-- 包含依赖的源码Jar -->
        <fileSet>
            <directory>${project.build.directory}/sources</directory>
            <includes>
                <include>**/*.jar</include>
            </includes>
            <outputDirectory>dependencies-sources</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

第二步,在pom.xml中配置assembly插件,同时整合dependency插件的拷贝逻辑:

<build>
    <plugins>
        <!-- 先拷贝依赖源码到指定目录 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.6.0</version>
            <executions>
                <execution>
                    <id>copy-sources</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <classifier>sources</classifier>
                        <outputDirectory>${project.build.directory}/sources</outputDirectory>
                        <includeScope>compile</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- 用assembly打包成归档 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/sources-assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

执行mvn clean package后,你会在target目录得到一个类似your-project-name-with-sources.zip的归档文件,里面包含了项目Jar和所有依赖的源码Jar。

小提示

  • 记得替换插件版本为你当前环境兼容的稳定版
  • 如果不需要包含传递依赖,或者想过滤某些特定依赖,可以在dependency插件的配置里添加<excludes>参数,或者调整<includeScope>来缩小范围

内容的提问来源于stack exchange,提问作者Nelson G.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:09:09