maven - Maven:如何在分叉的 JVM 中执行依赖项?
问题描述
使用maven-exec-plugin
一个java目标,我执行一个 jar 程序来验证我的项目中的一些文件。当验证失败时,它调用System.exit
返回一个非零返回码。
问题是它与 Maven 在同一个 JVM 中执行,所以当它调用 exit 时,处理停止,因为它没有 fork。
我将其配置为使用 java 目标执行maven-exec-plugin
(如此处)。执行 jar 在我的 Nexus 存储库中,所以我想将它作为依赖项下载到我的 pom.xml 中。
配置 maven-exec-plugin 依赖项的一个非常好的特性是它会下载 jar 及其所有依赖项,因此无需使用 maven 程序集插件将所有 jar 包含在可执行文件中。
如何配置我的 pom.xml 以执行 jar 依赖项并在失败时正确停止?
解决方案
我解决了我的问题。基本上,java
我必须使用exec
目标,而不是使用目标,并运行 java 可执行文件。下面的代码使用 main 方法设置类路径和类。
这个使用 pom.xml 和 Nexus 存储库的解决方案比只为我的用户处理 jar 文件有很多优势:
- 无需在将运行它的机器上安装任何东西,无论是开发机器还是持续集成机器。
- 验证工具开发者可以发布新版本,并且会自动更新。
- 开发者可以通过一个简单的参数来关闭它。
- 也解决了原来的问题:验证工具会在单独的进程中执行,所以maven进程在调用System.exit时不会中止。
这是一个评论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>
<groupId>com.company</groupId>
<artifactId>yourId</artifactId>
<version>1.0</version>
<properties>
<!--
Skip the validation executing maven setting the parameter below
mvn integration-test -Dvalidation.skip
-->
<validation.skip>false</validation.skip>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>MyValidator</id>
<phase>integration-test</phase> <!-- you can associate to any maven phase -->
<goals>
<goal>exec</goal> <!-- forces execution in another process -->
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable> <!-- java must be in your PATH -->
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>false</includePluginDependencies>
<skip>${validation.skip}</skip>
<arguments>
<argument>-classpath</argument>
<classpath/> <!-- will include your class path -->
<mainClass>com.company.yourpackage.AppMain</mainClass> <!-- the class that has your main file -->
<argument>argument.xml</argument> <!-- any argument for your executable -->
</arguments>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<!-- Specify your executable jar here -->
<groupId>com.company.validator</groupId>
<artifactId>validatorId</artifactId>
<version>RELEASE</version> <!-- you can specify a fixed version here -->
<type>jar</type>
</dependency>
</dependencies>
</project>
您可以运行多个传递其 id 的可执行文件:mvn exec:exec@MyValidator
推荐阅读
- powerbi - Power BI DAX 从自定义日期获取上个月的值
- laravel - laravel post 方法不工作。虽然 GET 方法工作
- javascript - 如何使用钩子在组件状态下复制 Redux 存储?
- javascript - 如何将 Vue 变量设置为等于函数参数?
- javascript - 在 JavaScript 中使用 forEach() 函数调用 $el 时出现问题
- php - php日期比较问题,我尝试了很多日期,但它根本不起作用
- pine-script - 交易视图 / Pine 脚本平均高点和低点
- c - 如何将 sigaction 与 4 个子进程一起使用?
- docker - Docker Desktop 未启动 - 序列不包含匹配元素
- python - 舰队和船的空间在右边而不是它们之间