将环境变量传递给 Maven 中执行的进程

发布于 2024-09-28 06:57:27 字数 1805 浏览 1 评论 0原文

我已经用头撞墙大约一个小时了:我试图将一个简单的属性(java.library.path)传递给 exec-maven-plugin。目标是让它与 Netbeans 集成 右键单击​​文件 >运行文件过程。

所以我像这样设置我的 POM:(

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1.1</version>
            <configuration>
                <environmentVariables>
                    <java.library.path>native/win32-x86</java.library.path>
                </environmentVariables>
            </configuration>
        </plugin>
    </plugins>
</build>

我使用旧版本,所以我可以看到执行参数,但它可以在 1.2 中完全重现)

然后我右键单击我的文件并单击“运行文件”。 Netbeans 启动此过程:(

 W:\programming\apache-maven-2.2.1\bin\mvn.bat -Dexec.classpathScope=runtime -Dexec.args=-classpath %classpath org.quackedcube.camera.CameraDemo -Dexec.executable=C:\Program Files\Java\jdk1.6.0_21\bin\java.exe -Dnetbeans.execution=true -Dmaven.repo.local=W:\programming\maven-repo process-classes exec:exec

原始的完整类路径执行更改为 exec:exec 因此希望应用我的配置)

但是我的环境变量显然被忽略,因为生成的执行程序是:

 Result of cmd.exe /X /C ""C:\Program Files\Java\jdk1.6.0_21\bin\java.exe" -classpath *snip* org.quackedcube.camera.CameraDemo" execution is: '1'.

我尝试过

  • 使用环境变量标签内单独的键和值标签
  • 直接在环境变量标签内使用键和值标签(值得一试)
  • 阶段
  • 绑定到作为maven arg传递的
  • 并使用exec:java代替传递-Djava.library.path=native/ win32-x86 作为项目配置页面中的运行参数和 VM 选项

,但全部失败。我真的很不知所措。

我想这是在 Maven 中使用 JNI 的缺点:你必须将参数传递给你的测试、你的运行时、你的模块运行 POM 和你的父 POM。

所以我的问题是:如何将 java.library.path 属性传递给执行的文件?如果它与 Netbeans 运行文件功能集成,那就太好了(因此我不必更改 POM 中的类名,构建,然后运行)

I've been banging my head against a wall for about an hour on this: I'm trying to pass a simple property (java.library.path) to exec-maven-plugin. The goal is to have it integrate with Netbeans Right Click file > Run File procedure.

So I set my POM like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1.1</version>
            <configuration>
                <environmentVariables>
                    <java.library.path>native/win32-x86</java.library.path>
                </environmentVariables>
            </configuration>
        </plugin>
    </plugins>
</build>

(I use an old version so I can see the execution args, but its fully reproducible with 1.2)

Then I right click my file and click "Run File". Netbeans starts this process:

 W:\programming\apache-maven-2.2.1\bin\mvn.bat -Dexec.classpathScope=runtime -Dexec.args=-classpath %classpath org.quackedcube.camera.CameraDemo -Dexec.executable=C:\Program Files\Java\jdk1.6.0_21\bin\java.exe -Dnetbeans.execution=true -Dmaven.repo.local=W:\programming\maven-repo process-classes exec:exec

(The original full classpath execution was changed to exec:exec so hopefully my configuration applied)

But my environment variable is apparently ignored, as the resulting executed program is:

 Result of cmd.exe /X /C ""C:\Program Files\Java\jdk1.6.0_21\bin\java.exe" -classpath *snip* org.quackedcube.camera.CameraDemo" execution is: '1'.

I've tried

  • Using separate Key and Value tags inside an enviornmentVariable tag
  • Use a key and value tag directly inside an enviornmentVariables tag (worth a try)
  • binding to a phase
  • passing as a maven arg and using exec:java instead
  • Passing -Djava.library.path=native/win32-x86 as a Run argument and VM option in Project Configuration page

and all have failed. I'm really at a loss here.

I guess this is the disadvantage of using JNI in maven: You have to pass as an argument to your tests, your runtime, your module run POM, and your parent POM.

So my question: How can I pass a java.library.path property to an executed file? It would be nice if it integrated with Netbeans Run File functionality (therefor I don't have to change the class name in a POM, build, then run)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉睡月亮 2024-10-05 06:57:27

不知道这一点,但显然在执行此操作时,您需要首先放置此属性。我认为没有必要,因为类路径不会立即执行,但显然它确实有所作为。

为了解决这个问题,我只需在“项目属性”>“项目属性”中更改它即可。行动>通过 Main 运行文件

exec.classpathScope=${classPathScope}
exec.args=-Djava.library.path="native/win32-x86" -classpath %classpath ${packageClassName}
exec.executable=java

您无法在 POM 中指定它的原因是 NB 通过命令行 exec.args 传递类路径及其执行内容,这会覆盖 POM 中的内容。

虽然这可能很丑陋并且依赖于平台,但当您混合使用 JNI 和 Maven 时就会发生这种情况。我确实没有其他办法可以看到。

Didn't know this, but apparently when doing this you need to put this property first. I didn't think it was necessary since the classpath isn't immediately executed, but apparently it does make a difference.

To fix it, I simply changed this in Project Properties > Actions > Run File via Main

exec.classpathScope=${classPathScope}
exec.args=-Djava.library.path="native/win32-x86" -classpath %classpath ${packageClassName}
exec.executable=java

The reason you can't specifcy it in the POM is that NB passes the classpath and what its execution via command line exec.args, which overrides whats in your POM.

While this might be ugly and platform dependant, its what happens when you mix JNI and Maven. There isn't really another way that I can see.

拥有 2024-10-05 06:57:27

不确定您是否尝试过此操作,但只要您需要在 JVM 级别上设置属性,就应该使用 -Djava.library.path=/some/path 来完成,

因此为了指定它exec-maven-plugin 你可以写这样的东西:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-Djava.library.path=${java.library.path}</argument>
    </arguments>
    </configuration>
</plugin>

当然,你需要更新可执行文件,也许添加其他属性。

Not sure if you tried this but as long as you need to set property on a level of JVM it should be done with -Djava.library.path=/some/path

So in order to specify it for exec-maven-plugin you could write something like this:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-Djava.library.path=${java.library.path}</argument>
    </arguments>
    </configuration>
</plugin>

You need, of course, to update the executable and maybe add another attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文