Resolving a Maven dependency differently if the JVM in use is x86 or x64?

岁酱吖の 提交于 2019-12-09 11:42:44

问题


I have a Maven repository set up to host some dlls, but I need my Maven projects to download different dlls depending on whether the JVM in use is x86 or x64.

So for example, on a computer running an x86 version of the JVM I need ABC.dll to be downloaded from the repository as a dependency, but on another computer running an x64 version of the JVM, I need it download XYZ.dll instead.

How would I go about doing this? An example pom.xml file would be nice.


回答1:


This will work on any VM. You can use profiles to have alternate configurations according to the environment.

A profile contains an activation block, which describes when to make the profile active, followed by the usual pom elements, such as dependencies:

<profiles>
  <profile>
    <activation>
      <os>
        <arch>x86</arch>
      </os>
    </activation>
    <dependencies>
     <dependency>
        <!-- your 32-bit dependencies here -->
     </dependency>
    </dependencies>
  </profile>
  <profile>
    <activation>
      <os>
        <arch>x64</arch>
      </os>
    </activation>
    <dependencies>
        <!-- your 64-bit dependencies here -->
    </dependencies>
  </profile>
</profiles>

As you mentioned DLLs, I'm assuming this is Windows-only, so you may also want to add <family>Windows</family> under the <os> tags.

EDIT: When mixing 32-bit VM on a 64-bit OS, you can see what value the VM is giving to the os.arch system property by running the maven goal

mvn help:evaluate

And then entering

${os.arch}

Alternatively, the goal help:system lists all system properties (in no particular order.)




回答2:


You can do this with profiles.This will only work in Sun's JVM.

<profiles>
    <profile>
        <id>32bits</id>
        <activation>
            <property>
                <name>sun.arch.data.model</name>
                <value>32</value>
            </property>
        </activation>
        <dependencies>
            ...
        </dependencies>
    </profile>

    <profile>
        <id>64bit</id>
        <activation>
            <property>
                <name>sun.arch.data.model</name>
                <value>64</value>
            </property>
        </activation>
        <dependencies>
            ...
        </dependencies>
    </profile>
</profiles>



回答3:


Maven Profiles may be helpful to you.



来源:https://stackoverflow.com/questions/3500664/resolving-a-maven-dependency-differently-if-the-jvm-in-use-is-x86-or-x64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!