首页 > 解决方案 > 将 jar 工件部署到 Nexus 会导致内容类型不匹配

问题描述

我正在使用 Nexus Repository Manager v3.1.0-04。当我尝试将mvn deployjar 工件添加到我的存储库时,我遇到了以下问题。

[错误] 无法在项目 rest-service 上执行目标 org.sonatype.plugins:nexus-staging-maven-plugin:1.5.1:deploy (injected-nexus-deploy):无法部署工件:无法传输工件 com。 xyz:rest-service:jar:0.0.1-20180504.193415-6 from/to nexus ( http://nexus.mydomain.io/repository/snapshots/ ): 传输文件失败: http://nexus.mydomain.io /repository/snapshots/com/xyz/rest-service/0.0.1-SNAPSHOT/rest-service-0.0.1-20180504.193415-6.jar。返回码为:400,ReasonPhrase:检测到内容类型 [application/x-sh],但预期 [application/java-archive]:com/xyz/rest-service/0.0.1-SNAPSHOT/rest-service-0.0.1 -20180504.193415-6.jar。-> [帮助 1]

我认为这可能与nexus-staging-maven-plugin链接)的版本有关,但即使我将版本设置为1.6.8(最新),我也会得到相同的效果。这篇文章建议使用build-helper-maven-plugin,所以我修改了我pom.xml的如下。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
        <id>attach-artifacts</id>
        <phase>package</phase>
        <goals>
            <goal>attach-artifact</goal>
        </goals>
        <configuration>
            <artifacts>
            <artifact>
                <file>target/${artifactId}-${version}.jar</file>
                <type>jar</type>
            </artifact>
            </artifacts>
        </configuration>
        </execution>
    </executions>
</plugin>

但是,我现在看到了一个不同的问题。

[错误] 无法在项目 rest-service 上执行目标 org.codehaus.mojo:build-helper-maven-plugin:3.0.0:attach-artifact (attach-artifacts):执行目标 org.codehaus.mojo 的附加工件:build-helper-maven-plugin:3.0.0:attach-artifact 失败:对于工件 {com.xyz:rest-service:0.0.1-SNAPSHOT:jar}:附加工件的 ID 必须与其对应的主 ID 不同人工制品。-> [帮助 1]

请注意,Maven 项目是由 Spring Initializer 通过 IntelliJ 生成的,是一个 Spring Boot 项目。在不使用Builder Helper插件的情况下,我可以看到所有文件都成功上传到 Nexus,直到 jar 完成上传(它实际上完成了上传,但由于内容类型不匹配,它失败了)。

关于如何解决这个问题的任何想法?我提到的帖子说“一些 maven 存储库检查文件内容”,那么,我将如何禁用 Nexus(我可以控制)检查文件内容?但真正的问题是,为什么是内容类型application/x-sh而不是application/java-archive?

标签: javaspringmavenspring-bootnexus

解决方案


在相关存储库的设置中(错误消息中的 URL 提到“快照”存储库),存储部分:禁用严格内容类型验证设置。该设置的描述是:Validate that all content uploaded to this repository is of a MIME type appropriate for the repository format.

要回答第二个问题,为什么:在编辑器中加载 JAR 文件。您可能会看到一个 shell 脚本标头 (Bash)。在这种情况下,JAR 文件是“可执行 JAR”,而 shell 脚本头是 Spring Boot 的启动脚本。因此,Nexus 错误地将文件检测为 shell 脚本。

例子:

#!/bin/bash
#
#    .   ____          _            __ _ _
#   /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
#  ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
#   \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
#    '  |____| .__|_| |_|_| |_\__, | / / / /
#   =========|_|==============|___/=/_/_/_/
#   :: Spring Boot Startup Script ::
#
# ... etc

这是在 Sublime Text 中打开的此类文件的屏幕截图: 在此处输入图像描述


推荐阅读