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

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

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包:

  1. wget --user USERNAME --password PASSWORD 上传jar包的Nexus URL
  2. java -Djava.security.egd=file:/dev/./urandom -jar jar包的名称.jar
  3. exec "$@"
  4. > 下一步将帮助你将jar包下载到另一个项目中

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

  1. <repositories>
  2. <repository>
  3. <id>remote</id>
  4. <url>托管jar包的URL</url>
  5. </repository>
  6. </repositories>

如果你的远程仓库受密码保护,你还需要添加setting.xml文件:

  1. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
  4. http://maven.apache.org/xsd/settings-1.0.0.xsd">
  5. <servers>
  6. <server>
  7. <id>remote</id>
  8. <username>***</username> <!-- 用户名 -->
  9. <password>****</password> <!-- 密码 -->
  10. </server>
  11. </servers>
  12. </settings>

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

  1. <dependency>
  2. <groupId>项目的groupId</groupId>
  3. <artifactId>项目的artifactId</artifactId> <!-- jar包的artifactId -->
  4. <version>项目的版本号</version>
  5. </dependency>

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

英文:

You can create a sh file and this to download jar

  1. wget --user USERNAME --password PASSWORD url of the nexus where the jar is uploaded
  2. java -Djava.security.egd=file:/dev/./urandom -jar name of the jar.jar
  3. 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

  1. &lt;repositories&gt;
  2. &lt;repository&gt;
  3. &lt;id&gt;remote&lt;/id&gt;
  4. &lt;url&gt;Url where you have hosted the jar&lt;/url&gt;
  5. &lt;/repository&gt;
  6. &lt;/repositories&gt;

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

  1. &lt;settings xmlns=&quot;http://maven.apache.org/SETTINGS/1.0.0&quot;
  2. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xsi:schemaLocation=&quot;http://maven.apache.org/SETTINGS/1.0.0
  4. http://maven.apache.org/xsd/settings-1.0.0.xsd">
  5. &lt;servers&gt;
  6. &lt;server&gt;
  7. &lt;id&gt;remote&lt;/id&gt;
  8. &lt;username&gt;***&lt;/username&gt; //username
  9. &lt;password&gt;****&lt;/password&gt; //password
  10. &lt;/server&gt;
  11. &lt;/servers&gt;
  12. &lt;/settings&gt;

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

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;group id of project&lt;/groupId&gt;
  3. &lt;artifactId&gt;artifact of project&lt;/artifactId&gt; //artifact of your jar
  4. &lt;version&gt;version of your project&lt;/version&gt;
  5. &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:

确定