如何调用驻留在 Maven 本地存储库中的 JAR?

How to invoke a JAR resides in the maven local repository?

作为 CI/CD 过程的一部分,我从 Maven 存储库 (Sonatype nexus) 中提取一个 JAR

现在,我想简单地"java -jar"它。

最简单的方法是什么?

我应该只使用命令 java -jar ${MAVEN_HOME}/repository/com/company/path/to/my/x.jar 吗?

或者有更简单的方法吗?

您可以创建一个 sh 文件并将其用于下载 jar

wget --user USERNAME --password PASSWORD url of the nexus where the jar is uploaded

java -Djava.security.egd=file:/dev/./urandom -jar name of the jar.jar
exec "$@"

The Next step will help you to download jar to another project

在您的 pom.xml 中您需要添加 repository 标签

<repositories>
    <repository>
       <id>remote</id>
       <url>Url where you have hosted the jar</url>
     </repository>
</repositories>

此外,如果您的遥控器受密码保护,则需要添加 setting.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                  http://maven.apache.org/xsd/settings-1.0.0.xsd">
 <servers>
    <server>
      <id>remote</id>
      <username>***</username> //username
      <password>****</password> //password
    </server>
  </servers>
</settings>

在你的 pom.xml 中添加依赖后,它会将 jar 下载到你本地的 m2 文件夹

<dependency>
            <groupId>group id of project</groupId> 
            <artifactId>artifact of project</artifactId>    //artifact of your jar
            <version>version of your project</version>
        </dependency>

创建 jar 时需要添加的 groupIdartifactIdversion