使用maven-exec-plugin和java目标,我执行了一个jar程序来验证我的项目中的一些文件。当验证失败时,它调用System.exit返回一个非零返回代码。
问题是它与Maven在同一个JVM中执行,所以当它调用exit时,处理会停止,因为它是不叉。
我将其配置为使用maven-exec-plugin和java目标执行(类似于这里 )。执行jar位于我的Nexus存储库中,因此我希望将其作为依赖项下载到我的pom.xml中。
配置maven-exec-plugin依赖关系的一个非常好的特性是,它下载jar及其所有依赖项,因此没有必要使用maven程序集插件将所有jar包含在可执行文件中。
如何配置我的pom.xml以执行jar依赖关系,并在其失败时正确停止?
发布于 2018-12-04 14:29:34
我解决了我的问题。基本上,我必须使用exec ,而不是使用java目标,并运行java可执行文件。下面的代码使用main方法设置类路径和类。
这个使用pom.xml和Nexus存储库的解决方案比只为我的用户处理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>
<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。
发布于 2022-10-21 20:24:13
我偶然发现了同样的问题-- System.exit用exec:java阻止了maven。
我试验了使用exec:exec目标,并使其与以下配置一起工作:
(使用exec-maven-plugin 3.1.0)
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-observability-docs</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>${settings.localRepository}/io/micrometer/micrometer-docs-generator/${micrometer-docs-generator.version}/micrometer-docs-generator-${micrometer-docs-generator.version}.jar</argument>
<argument>${micrometer-docs-generator.inputPath}</argument>
<argument>${micrometer-docs-generator.inclusionPattern}</argument>
<argument>${micrometer-docs-generator.outputPath}</argument>
</arguments>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-docs-generator</artifactId>
<version>${micrometer-docs-generator.version}</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>https://stackoverflow.com/questions/53565167
复制相似问题