Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 276
Steve Jobs Avatar asked Aug 17 '10 08:08

Steve Jobs


People also ask

How Maven handles and determines what version of dependency will be used when multiple version of an artifact are encountered?

Dependency mediation - this determines what version of an artifact will be chosen when multiple versions are encountered as dependencies. Maven picks the "nearest definition". That is, it uses the version of the closest dependency to your project in the tree of dependencies.

How do I exclude a specific version of a dependency in Maven?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.

What is Maven dependency resolve?

org.apache.maven.plugins:maven-dependency-plugin:3.3.0:resolve. Description: Goal that resolves the project dependencies from the repository. When using this goal while running on Java 9 the module names will be visible as well.


1 Answers

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.)

like image 82
mdma Avatar answered Oct 29 '22 19:10

mdma