java - 将 jar Artifact 部署到 Nexus 会导致内容类型不匹配

标签 java spring maven spring-boot nexus

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

[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 ) 的版本有关,但即使我将版本设置为 1.6.8(最新),我得到了同样的效果。这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上传完成(实际上是上传完成了,但是因为内容类型不匹配,所以失败了)。

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

最佳答案

在相关存储库的设置中(错误消息中的 URL 提到“快照”存储库),存储 部分:禁用严格内容类型验证环境。该设置的描述是:验证上传到此存储库的所有内容是否属于适合存储库格式的 MIME 类型

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

例子:

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

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

关于java - 将 jar Artifact 部署到 Nexus 会导致内容类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50182301/

相关文章:

java - Jgroups UDP bind_addr 创建两个集群而不是一个

java - 当我运行我的程序时没有任何显示

java - hibernate 和线程安全 : Is an injected SessionFactory thread safe?

java - Spring JPA Query 返回 Null 而不是 List

maven - Maven无法解析org.junit.test

java - 在 UI 线程上获取重复回调的正确方法

java - 在 android eclipse 中创建第二个计数器

ruby-on-rails - spring/application.rb :161 undefined method `reject!' for nil:NilClass (NoMethodError)

java - 创建 Maven Web 项目时出现重复的源文件夹

java - Caused by : org. hibernate.MappingException: 实体映射中的重复列