Resolving a Maven dependency differently if the JV

2019-03-26 14: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.