是否可以从 POM 文件内部一致地将 -Djava.library.path 传递给 Maven 测试?

发布于 2024-12-11 16:26:16 字数 711 浏览 0 评论 0原文

我有一个外部库,需要与我的 java 项目中的测试动态链接。该项目是使用 maven 设置的,我需要将以下内容添加到 eclipse 中的 vm 参数中才能通过测试:

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

不幸的是,这意味着使用 mvn test 从 Maven 运行测试总是会失败。

一种解决方法是使用 -DargLine 参数调用 mvn,如下所示:

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

但是,显然这存在特定于我的机器的问题,因此我无法将其直接放入 pom 文件中。我想我正在寻找的是一种在每台机器上修改该字符串的方法,就像第一行对 Eclipse 所做的那样。

我也很好奇如何将它放入 POM 文件中,我尝试将其放入 标签内,但这似乎不起作用,有什么我' m 缺失:

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

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>

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

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

发布评论

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

评论(1

々眼睛长脚气 2024-12-18 16:26:16

经过一些研究,我发现了解决我的问题的一个不错的方法。

在 maven 的 settings.xml 文件中,您可以定义 localRepository 的位置,如果您什么都不设置,这里是默认值:

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

如您所见,这至少与我尝试设置的目录的第一部分匹配:/Users/rob/.m2

由于动态链接是操作系统具体的,你可以还想为备用路径后缀设置配置文件。您可以在 .pom 中执行此操作,如下所示:

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

然后,您可以在 .pom 中为要测试的项目使用此属性。在插件类别下,您可以添加:

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

现在,maven 可以运行这些测试,而无需用户指定动态链接库的位置。您还可以通过添加另一个配置文件来处理使用不同操作系统的用户。

注意:关于我之前的 问题。我想我只是在错误的 .pom 中使用它

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

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