首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Maven:如何在分叉JVM中执行依赖项?

Maven:如何在分叉JVM中执行依赖项?
EN

Stack Overflow用户
提问于 2018-11-30 21:28:48
回答 2查看 1.3K关注 0票数 1

使用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依赖关系,并在其失败时正确停止?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-04 14:29:34

我解决了我的问题。基本上,我必须使用exec ,而不是使用java目标,并运行java可执行文件。下面的代码使用main方法设置类路径和类。

这个使用pom.xml和Nexus存储库的解决方案比只为我的用户处理jar文件有很多优点:

  • 不需要在运行它的机器上安装任何东西,无论是开发人员机器还是持续集成机器。
  • 验证工具开发人员可以发布新版本,并将自动更新。
  • 开发人员可以使用一个简单的参数关闭它。
  • 还解决了原来的问题:验证工具将在一个单独的进程中执行,因此maven进程在调用System.exit时不会中止。

下面是一个注释的pom.xml

代码语言:javascript
复制
<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

票数 2
EN

Stack Overflow用户

发布于 2022-10-21 20:24:13

我偶然发现了同样的问题-- System.exitexec:java阻止了maven。

我试验了使用exec:exec目标,并使其与以下配置一起工作:

(使用exec-maven-plugin 3.1.0)

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53565167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档