[英]download and execute jar file from remote maven repository
That might be a dumb question but somehow I cannot figure it out (even with the lots of already given answers on stackoverflow) how to do it: 这可能是一个愚蠢的问题,但不知怎的,我无法弄明白(即使有很多已经给出的stackoverflow答案)如何做到这一点:
mvn package
and can execute the jar file with java -jar
... and everything works fine. mvn package
,可以用java -jar
执行jar文件......一切正常。 mvn exec:java
or something like that) on the command line and Maven shall download the jar file from the remote repository and execute it. mvn exec:java
或类似的东西),Maven将从远程数据库下载jar文件存储库并执行它。 Independent of the current directory in which the user is. 独立于用户所在的当前目录。 How do I do that?
我怎么做? Currently I get the error message that I need to be in a directory with an existing pom.xml file.
目前,我收到的错误消息是我需要在一个包含现有pom.xml文件的目录中。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<groupId>handof.nod</groupId>
<artifactId>clirunnertest</artifactId>
<version>1.0</version>
<name>CLIRunnerTest</name>
<description>Kleines Testprogramm um zu testen wie Spring Boot auf der Command Line funktioniert</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>target/clirunnertest-1.0.jar</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
Your requirements are quite specific, there are ways to do what you want, but you may not find them suitable for your needs. 你的要求是非常具体的,有很多方法可以做你想做的,但你可能无法找到他们适合您的需求。 In a nutshell, your requirements are:
简而言之,您的要求是:
Solution 1: 解决方案1:
What you can do is, in a single command line, download your artifact from the repo and execute it, with something like: 你可以做的是,在一个命令行中,从repo下载你的工件并执行它,例如:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.0.2:copy -Dartifact=handof.nod:clirunnertest:1.0.0:jar -DoutputDirectory=. && java -jar clirunnertest.jar
What it does is: 它的作用是:
maven-dependency-plugin
(which does not require any POM, lucky us) maven-dependency-plugin
下载jar文件(不需要任何POM,幸运的是我们) java -cp clirunnertest.jar MyMainClass
java -cp clirunnertest.jar MyMainClass
Solution 2: 解决方案2:
Solution 1 require your user to specify the java command and its argument, not very flexible. 解决方案1要求您的用户指定java命令及其参数,不是很灵活。 With this solution you'll be able to change the way the command runs without impacting the end user.
使用此解决方案,您将能够在不影响最终用户的情况下更改命令的运行方式。
First you will need to create a small project containing your exec:java
or exec:exec
configuration and upload it in your Maven repository, alongside with the jar you want to execute. 首先,您需要创建一个包含
exec:java
或exec:exec
配置的小项目,并将其上传到Maven存储库,以及要执行的jar。 You only need to write a standalone pom.xml with your jar dependency and related exec
configuration such as: 您只需要编写一个带有jar依赖关系和相关
exec
配置的独立pom.xml,例如:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>handof.nod</groupId>
<artifactId>clirunnertest-pomrunnerproject</artifactId>
<version>0.1</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>run-java</id>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>handof.nod.clirunnertest.MainClassOrWhatever</mainClass>
<arguments>
<argument>bla</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>handof.nod</groupId>
<artifactId>clirunnertest</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
Once this project is available on your Maven repository, you can run: 一旦此项目在您的Maven存储库上可用,您就可以运行:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.0.2:copy -Dartifact=handof.nod:clirunnertest-pomrunnerproject:0.1:pom -DoutputDirectory=. && mvn exec:java -f clirunnertest-pomrunnerproject-0.1.pom
That will download the pom for this project and execute it like any other Maven project. 这将下载该项目的pom,并像任何其他Maven项目一样执行它。
You can also do it in two times, first download the pom with the copy goal somewhere on your machine, and then run it from anywhere by specifying the path to the downloaded pom using the -f
parameter: 您也可以两次执行此操作,首先在计算机上的某个位置下载带有复制目标的pom,然后通过使用
-f
参数指定下载的pom的路径从任何位置运行它:
mvn exec:java -f /path/to/the/pom/clirunnertest-pomrunnerproject-0.1.pom
This allow you to run this command from anywhere as long as you specify the path of the pom. 这允许您从任何位置运行此命令,只要您指定pom的路径即可。
As you see, though they work, these solutions (and many variant you can imagine like these, such as having a bash script available on Nexus, having said configuration in a parent pom and using a sub-project with the -f
parameter to use it from anywhere, etc.) are not really flexible nor easy to use and distribute. 如你所见,虽然它们有效,但是这些解决方案(以及你可以想象的许多变体,例如在Nexus上有一个bash脚本,在父pom中使用了一个子项目并使用带
-f
参数的子项目)它来自任何地方等)不是非常灵活,也不易于使用和分发。 Maven may not be the best tool to achieve what you want, though implementing your own plugin as you discussed may be a solution ;) Maven可能不是实现你想要的最好的工具,尽管如你所讨论的那样实现你自己的插件可能是一个解决方案;)
There is not a single step to do so, and Maven generally wants to be able to use the ~/.m2 folder as a working area. 没有一个步骤可以这样做,并且Maven通常希望能够将〜/ .m2文件夹用作工作区域。 You can however do it in two steps.
但是,您可以分两步完成。
dependency:get
- see How can I download a specific Maven artifact in one command line? dependency:get
下载工件dependency:get
- 请参阅如何在一个命令行中下载特定的Maven工件? for details. You can also create a pom.xml file for it which the users can download (again as an artifact) and then set it up for mvn exec:java
. 您还可以为其创建一个pom.xml文件,用户可以将其下载(再次作为工件),然后将其设置为
mvn exec:java
。
If this is something you want to do on a regular basis, I will suggest a regular deployment! 如果您想定期做这件事,我会建议定期部署! Java WebStart works well if you have a webserver somewhere you can store the files.
如果您有一个可以存储文件的Web服务器,Java WebStart可以很好地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.