Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible consistently pass -Djava.library.path to a maven test from inside a POM file?

I have an external library that needs to be dynamically linked with a test in my java project. The project is setup using maven, and I need to add the following to my vm arguments in eclipse for the test to pass:

-Djava.library.path=${env_var:HOME}/.m2/repository/natives/dist/lib -ea

Unfortunately this means that running the test from maven using: mvn test will always fail.

One work around is to call mvn with a -DargLine argument like so:

mvn test -DargLine="-Djava.library.path=/Users/rob/.m2/repository/natives/dist/lib -ea"

However, clearly this has the problem of being specific to my machine, so I can't put it directly in the pom file. I guess what I'm looking for is a way of modifying that string on a per machine basis kinda like the first line does for eclipse.

I'm also curious how I could put it into the POM file, I've tried placing it inside of <argLine> tags, but that doesn't seems to work, is there something I'm missing:

<argLine>-Djava.library.path=/Users/rob/.m2/repository/natives/dist/lib -ea</argLine>

like image 264
Lockyer Avatar asked Oct 21 '11 20:10

Lockyer


People also ask

Which of the following are true about POM?

POM stands for Project Object Model. Q 9 - What of the following is true about POM? A - It is fundamental Unit of Work in Maven.

How do you pass variables in POM xml?

To refer to environment variables from the pom. xml, we can use the ${env. VARIABLE_NAME} syntax. We should remember to pass the Java version information via environment variables.


1 Answers

After some research I've discovered a decent solution to my problem.

In maven your settings.xml file, you can define a location for the localRepository here are the defaults if you set nothing:

  • Unix/Mac OS X – ~/.m2
  • Windows – C:\Documents and Settings\username.m2

As you can see this matches at least the first part of the directory I was trying to set: /Users/rob/.m2

Since dynamic linking is OS specific, you may also want to setup a profile for alternate path suffixes. You can do this in a .pom like this:

<profile>
    <id>OSX</id>
        <activation>
            <os>
                <family>mac</family>
            </os>
        </activation>
    <properties>
        <dynamic.libLoc>${settings.localRepository}/natives/dist/lib</dynamic.libLoc>
    </properties>
</profile>

You can then use this property in the .pom for the project you wish to test. Under the plugins category you can add:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <argLine>-Djava.library.path=${dynamic.libLoc}</argLine>
    </configuration>
</plugin>

Now maven can run those tests without users having to specify the location of the dynamically linked libraries. You can also handle users with different operating systems by just adding another profile.

Note: With regards to my problem with <argLine> earlier. I think I was just using it in the wrong .pom

like image 84
Lockyer Avatar answered Nov 01 '22 18:11

Lockyer