这可能是一个愚蠢的问题,但不知怎的,我无法弄明白(即使有很多已经给出的stackoverflow答案)如何做到这一点:
mvn package,可以用java -jar... 执行jar文件,一切正常.mvn exec:java在命令行上调用maven命令(比如或类似的东西),Maven将从远程存储库下载jar文件并执行它.独立于用户所在的当前目录.我怎么做?目前,我收到的错误消息是我需要在一个包含现有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>
Run Code Online (Sandbox Code Playgroud)
您的要求非常具体,有很多方法可以做您想做的事,但您可能会发现它们不适合您的需求。简而言之,您的要求是:
解决方案1:
您可以做的是,在单个命令行中,从 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
Run Code Online (Sandbox Code Playgroud)
它的作用是:
maven-dependency-plugin(不需要任何 POM,我们很幸运)下载 jar 文件java -cp clirunnertest.jar MyMainClass解决方案2:
解决方案 1 要求您的用户指定 java 命令及其参数,不是很灵活。使用此解决方案,您将能够在不影响最终用户的情况下更改命令的运行方式。
首先,您需要创建一个包含您的exec:java或exec:exec配置的小项目,并将其与要执行的 jar 一起上传到您的 Maven 存储库中。您只需要使用您的 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>
Run Code Online (Sandbox Code Playgroud)
一旦此项目在您的 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
Run Code Online (Sandbox Code Playgroud)
这将下载该项目的 pom 并像任何其他 Maven 项目一样执行它。
你也可以分两次执行,首先在你的机器上的某个地方下载带有复制目标的 pom,然后通过使用-f参数指定下载的 pom 的路径从任何地方运行它:
mvn exec:java -f /path/to/the/pom/clirunnertest-pomrunnerproject-0.1.pom
这允许您从任何地方运行此命令,只要您指定 pom 的路径。
如您所见,尽管它们有效,但这些解决方案(以及您可以想象的许多变体,例如在 Nexus 上提供 bash 脚本,在父 pom 中进行所述配置,并使用带有-f参数的子项目从任何地方等)都不是很灵活,也不容易使用和分发。Maven 可能不是实现您想要的最佳工具,尽管如您所讨论的那样实现您自己的插件可能是一种解决方案;)
没有一个步骤可以做到这一点,Maven 通常希望能够使用 ~/.m2 文件夹作为工作区域。不过,您可以分两步完成。
dependency:get- 请参阅如何在一个命令行中下载特定的 Maven 工件?了解详情。您还可以为其创建一个 pom.xml 文件,用户可以下载该文件(再次作为工件),然后将其设置为mvn exec:java.
如果这是您想要定期执行的操作,我会建议定期部署!如果您有一个可以存储文件的网络服务器,Java WebStart 就可以很好地工作。