gpt4 book ai didi

maven - 使用带有参数的 Maven 'exec:exec'

转载 作者:行者123 更新时间:2023-12-03 01:29:59 34 4
gpt4 key购买 nike

我有一个配置为使用 Maven 构建和运行的项目。该项目取决于平台特定的 native 库,我正在使用发现的策略 here管理这些依赖项。

本质上,特定平台的 .dll.so 文件被打包到 jar 中,并使用标识目标平台的分类器推送到 Maven 服务器。然后,maven-dependency-plugin 解压平台特定的 jar,并将 native 库复制到目标文件夹。

通常我会使用 mvn exec:java 来运行 Java 程序,但是 exec:java 在与 Maven 相同的 JVM 中运行应用程序,这使得我无法修改类路径。由于必须将 native 依赖项添加到类路径中,因此我被迫使用 mvn exec:exec 来代替。这是 pom 的相关片段:

...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Djava.library.path=target/lib</argument>
<argument>-classpath</argument>
<classpath />
<argument>com.example.app.MainClass</argument>
</arguments>
</configuration>
</plugin>
...

这对于应用程序的默认配置来说效果很好,但我希望能够在命令行指定一些可选参数。理想情况下,我想做这样的事情:

mvn exec:exec -Dexec.args="-a <an argument> -b <another argument>"

不幸的是,指定 exec.args 变量会覆盖我在 pom 中的参数(这是设置类路径和运行应用程序所必需的)。有没有解决的办法?在命令行指定一些可选参数而不覆盖我在 pom 中的内容的最佳方法是什么?

最佳答案

我设法使用 Maven 环境变量找到了一个相当优雅的解决方案来解决我的问题。

默认值在 pom 中定义为属性,并作为参数添加到 exec 插件中:

...
<properties>
<argumentA>defaultA</argumentA>
<argumentB>defaultB</argumentB>
</properties>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Djava.library.path=${project.build.directory}/lib</argument>
<argument>-classpath</argument>
<classpath />
<argument>com.example.app.MainClass</argument>
<argument>-a</argument>
<argument>${argumentA}</argument>
<argument>-b</argument>
<argument>${argumentB}</argument>
</arguments>
</configuration>
</plugin>
...

现在我可以像以前一样使用默认参数运行:

mvn exec:exec

我可以使用以下命令轻松覆盖命令行中每个参数的默认值:

mvn exec:exec -DargumentA=alternateA -DargumentB=alternateB

关于maven - 使用带有参数的 Maven 'exec:exec',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15013651/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com