如何调用存储在Maven本地仓库中的JAR文件?

huangapple go评论64阅读模式
英文:

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吗?

还是有更简单的方法?

英文:

As part of a CI/CD process, I'm pulling a JAR from a maven repository (Sonatype nexus)

Now, I'd like to simply "java -jar" it.

What is the simplest way to do it?

Should I just use the command java -jar ${MAVEN_HOME}/repository/com/company/path/to/my/x.jar?

Or is there a simpler way?

答案1

得分: 1

你可以创建一个sh文件,并将以下内容添加到文件中以下载jar包:

wget --user USERNAME --password PASSWORD 上传jar包的Nexus URL

java -Djava.security.egd=file:/dev/./urandom -jar jar包的名称.jar
exec "$@"

> 下一步将帮助你将jar包下载到另一个项目中

在你的pom.xml中,你需要添加repository标签:

<repositories>
    <repository>
        <id>remote</id>
        <url>托管jar包的URL</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> <!-- 用户名 -->
      <password>****</password> <!-- 密码 -->
    </server>
  </servers>
</settings>

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

<dependency>
    <groupId>项目的groupId</groupId> 
    <artifactId>项目的artifactId</artifactId> <!-- jar包的artifactId -->
    <version>项目的版本号</version>
</dependency>

在创建jar包时,你需要添加这些groupIdartifactIdversion信息。

英文:

You can create a sh file and this to download 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 &quot;$@&quot;

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

In your pom.xml you need to add repository tag

&lt;repositories&gt;
    &lt;repository&gt;
       &lt;id&gt;remote&lt;/id&gt;
       &lt;url&gt;Url where you have hosted the jar&lt;/url&gt;
     &lt;/repository&gt;
&lt;/repositories&gt;

Also, you need to add setting.xml if your remote is password protected

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

After that add dependency in your pom.xml, it will download the jar to you local m2 folder

&lt;dependency&gt;
			&lt;groupId&gt;group id of project&lt;/groupId&gt; 
			&lt;artifactId&gt;artifact of project&lt;/artifactId&gt;    //artifact of your jar
			&lt;version&gt;version of your project&lt;/version&gt;
		&lt;/dependency&gt;

This groupId, artifactId and version that you need to add when you will create the jar

huangapple
  • 本文由 发表于 2020年5月19日 20:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61891491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定