views:

43

answers:

2

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)

A: 

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.

iYasha
I did add `-Djava.library.path=${java.library.path}` to `exec.args` in the Netbeans actions, but it seemed to be interpreted as part of the whole argument list for maven. When escaping the quotes, I got a "invalid parameter error" (or something like that). And even if I specifically set it in my pom, Netbeans will override it in the command line
TheLQ
As far as I understand you're calling Maven from NetBeans and from Maven you need to call another eternal process. Thus you use exec-maven plugin. The trick is that arguments specified for Maven execution in the Netbeans aren't passed to exec-maven plugin directly. Instead you need to specify them in POM itself. Did you try to specify parameters in POM file as I wrote above?
iYasha
I did try and couldn't get it to work. I did find out the simpler but platform/IDE dependent way in my answer. If you have any other suggestions please tell me.: http://stackoverflow.com/questions/3951818/passing-an-enviornment-variable-to-executed-process-in-maven/3979239#3979239
TheLQ
+2  A: 

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.

TheLQ