将 jar 工件部署到 Nexus 会导致内容类型不匹配

Deploying a jar artifact to Nexus results in a content type mismatch

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

[ERROR] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.5.1:deploy (injected-nexus-deploy) on project rest-service: Failed to deploy artifacts: Could not transfer artifact com.xyz:rest-service:jar:0.0.1-20180504.193415-6 from/to nexus (http://nexus.mydomain.io/repository/snapshots/): Failed to transfer file: http://nexus.mydomain.io/repository/snapshots/com/xyz/rest-service/0.0.1-SNAPSHOT/rest-service-0.0.1-20180504.193415-6.jar. Return code is: 400, ReasonPhrase: Detected content type [application/x-sh], but expected [application/java-archive]: com/xyz/rest-service/0.0.1-SNAPSHOT/rest-service-0.0.1-20180504.193415-6.jar. -> [Help 1]

我认为这可能与nexus-staging-maven-plugin的版本有关(link) but even if I set the version to 1.6.8 (latest), I get the same effect. This post建议使用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>

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

[ERROR] Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:3.0.0:attach-artifact (attach-artifacts) on project rest-service: Execution attach-artifacts of goal org.codehaus.mojo:build-helper-maven-plugin:3.0.0:attach-artifact failed: For artifact {com.xyz:rest-service:0.0.1-SNAPSHOT:jar}: An attached artifact must have a different ID than its corresponding main artifact. -> [Help 1]

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

关于如何解决这个问题有什么想法吗?我提到的 post 说 "some maven repositories check the file content," 等等,我将如何在检查文件内容时禁用 Nexus(我可以控制)?但真正的问题是,为什么内容类型是 application/x-sh 而不是 application/java-archive

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

回答第二个问题,为什么:在编辑器中加载 JAR 文件。您可能会看到 shell 脚本 header (Bash)。在这种情况下,JAR 文件是 "executable JAR",shell 脚本 header 是来自 Spring Boot.xml 的启动脚本。因此,Nexus 错误地将文件检测为 shell 脚本。

示例:

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

这是在 Sublime Text 中打开的此类文件的屏幕截图: