Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an enviornment variable to executed process in Maven

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)

like image 486
TheLQ Avatar asked Oct 17 '10 02:10

TheLQ


2 Answers

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.

like image 92
TheLQ Avatar answered Sep 22 '22 19:09

TheLQ


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.

like image 43
oiavorskyi Avatar answered Sep 23 '22 19:09

oiavorskyi