Maven and profiles activation

Working on a Java training about SWT, I am in front of a bother issue : multi-platform !

I mean, on this specific API, SWT, the needed JAR depends on the target os/platform.

So, managing such thing, I am using the specific maven tag in a declaration.

See bollow file explaining dependency switching for a Linux 64 bits platform :

<profile>
        <id>linux</id>
        <activation>
                <os>
                        <family>unix</family>
                        <arch>x86_64</arch>
                </os>
        </activation>
        <dependencies>
                <dependency>
                        <groupId>org.eclipse.swt.linux</groupId>
                        <artifactId>x86_64</artifactId>
                        <version>3.7.2</version>
                        <scope>system</scope>
                        <systemPath>${project.projectDirectory}/lib/linux/swt-3.7.2-linux-x86_64.jar</systemPath>
                </dependency>
        </dependencies>
</profile>

You can refine os and name and arch following your needs :

  • Linux 64 bits
<id>linux64</id>
<activation>
  <os>
    <family>unix</family>
    <arch>x86_64</arch>
  </os>
</activation>
  • Linux 32 bits:
<id>linux</id>
<activation>
  <os>
    <family>unix</family>
    <arch>x86</arch>
  </os>
</activation>
  • Windows XP (32 bits)
<id>win32</id>
<activation>
  <os>
    <family>windows</family>
    <arch>x86</arch>
    <name>Windows XP</name>
  </os>
</activation>
  • Windows 7 (64 bits)
<id>win64</id>
<activation>
  <os>
    <family>windows</family>
    <name>Windows 7</name>
    <arch>x86</arch>
  </os>
</activation>

To verify wich maven profile is(are?) activated, just execute the following command :

$ mvn help:active-profiles
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building swt-project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-help-plugin:2.1.1:active-profiles (default-cli) @ oop-6-swt ---
[INFO] Active Profiles for Project 'fr.mcgivrer.samples:swt-project:jar:0.0.1-SNAPSHOT': The following profiles are active: - win64 (source: pom)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.536s[INFO] Finished at: Fri Jan 25 11:48:58 CET 2013
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------

Oh my ! I am also working on Windows OS based station ! 🙂

Have fun !

Sample pom.xml on the following gist  https://gist.github.com/mcgivrer/5394911

McG.

Publicités