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

如何使用Maven发布target目录下的所有构件到指定仓库

Maven 发布时上传额外构件问题解决方案

我目前使用如下命令执行Maven发布操作:

mvn release:prepare release:perform

该命令可正常运行,但仅会部署项目的主jar包,无法上传target目录下的其他构件。

尝试过的解决方案

我尝试使用deploy插件的deploy-file目标上传构件,但是构件被错误上传到了jetbrains仓库,所用命令如下:

mvn org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy-file -Dfile="target/foo-bar-jar-with-dependencies.jar" -Durl="<my-repo-url>" -Darguments=-DskipTests -DrepositoryId="repo-id"

命令输出如下:

Uploading to repo-id: <my-repo-url>/org/jetbrains/annotations/13.0/annotations-13.0.jar

出于隐私考虑,我将部分参数用<>占位隐藏。

我的target目录下的构件结构如下:

src/
target/
   |______ foo-bar.jar <<< 这是主jar包
   |______ foo-bar-distribution.zip
   |______ foo-bar-jar-with-dependencies.jar
pom.xml

问题

我该如何使用Maven上传这些额外的构件?

问题排查与解决方案

默认情况下deploy插件会读取其他配置(我通过Maven的-X参数查看调试日志发现了该问题),因此我需要显式指定pom文件的路径(哪怕pom文件就在当前工作目录下),该方案可以解决问题。

我在调试日志中看到的内容如下:

[DEBUG] Using META-INF/maven/org.jetbrains/annotations/pom.xml as pomFile

在deploy插件的命令中添加pomFile参数即可解决问题,更新后的命令如下:

mvn org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy-file -Dfile="target/foo-bar-jar-with-dependencies.jar" -Durl="<my-repo-url>" -Darguments=-DskipTests -DrepositoryId="repo-id" -DpomFile="pom.xml"

附:pom文件build配置段

<build>
    <sourceDirectory>src/main/kotlin</sourceDirectory>
    <testSourceDirectory>src/test/kotlin</testSourceDirectory>
    <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <classifier>jar-with-dependencies</classifier>
                        <includes>
                            <include>${basedir}/target/classes/**</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <descriptors>
                    <descriptor>assembly/jar.xml</descriptor>
                    <descriptor>assembly/zip.xml</descriptor>
                </descriptors>
                <archive>
                    <manifest>
                        <mainClass>com.foo.bar.MainKt</mainClass>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-fat-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
                <execution>
                    <id>make-zip</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>3.0.0-M4</version>
            <configuration>
                <!-- this is done to avoid infinite loop of builds -->
                <scmCommentPrefix>[skip ci]</scmCommentPrefix>
                <tagNameFormat>v@{project.version}</tagNameFormat>
            </configuration>
        </plugin>
    </plugins>
</build>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 23:36:04