tags:

views:

919

answers:

1

I have a large project which uses SWT on several different platforms like windows (32 and 64 bits), linux-gtk (32 and 64 bits) etc. The project has several modules in a simple structure:

project-parent
 +- core
 +- swt-bridge (uses SWT)
 +- module1 (uses swt-bridge module)
 +- module2
 +- ...
 +- module17 (uses swt-bridge module)

I would like to be able to run my code from Eclipse on all the supported platforms. E.g. if I look at the swt-bridge module on a 32-bit Windows machine, it should have the 32-bit Windows SWT dependencies, and also have the required DLLs extracted to some directory which I can then add to java.library.path.

I can specify different dependencies by defining profiles activated by os.family and os.arch.

 <profile>
  <id>macos</id>
  <activation>
   <os>
    <family>macosx</family>
    <arch>x86</arch>
   </os>
  </activation>
  <properties>
   <swt.native.groupId>org.eclipse.swt.carbon.macosx</swt.native.groupId>
   <swt.native.artifactId>x86</swt.native.artifactId>
   <swt.native.version>3.5.1</swt.native.version>
  </properties>
 </profile>

I can also use the maven-dependency-plugin to extract any DLLs, .so files from the SWT JAR to target/swtlib, and use them from Eclipse.

This works fine if I put the profiles in the swt-bridge module. But this way I cannot see the properties set by the active profile from other projects, so e.g. I cannot use the dependency plugin with these properties (which I would like to).

I thought I could solve this by moving the profiles to the parent POM, and indeed, if I build the whole product, it works. But this way I cannot just build one module, because Maven will complain about missing properties.

I'm pretty sure many people face this problem, so there must be a way to handle this properly in Maven. Can anyone tell me what it is?

+1  A: 

Project inheritance should work here: properties are inherited if you place them in the parent pom and if the profile is activated. Please note that I said activated and not active for the current project (see MNG-3228 for the difference between both terms).

I've setup a simple multi-modules build demonstrating properties inheritance from a profile in the parent pom with maven 2.2.1. The project has the following structure:

.
|-- my-module
|   |-- pom.xml
|   `-- src
|       |-- main
|       |   `-- java
|       `-- test
|           `-- java
`-- pom.xml

The parent pom.xml is defined as follow (adapted to my platform):

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>my-module</module>
  </modules>
  <profiles>
    <profile>
      <id>linux-x86</id>
      <properties>
        <someVar>some value</someVar>
      </properties>
      <activation>
        <os>
          <name>Linux</name>
          <arch>i386</arch>
        </os>
      </activation>
    </profile>
  </profiles>
</project>

And this is the pom of the module:

<project>
  <parent>
    <artifactId>my-app</artifactId>
    <groupId>com.mycompany.app</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <phase>compile</phase>
            <configuration>
              <tasks>
                <echo message="${someVar}"/>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

If I run Maven from the parent:

$ mvn help:active-profiles compile 
[INFO] Scanning for projects...
[INFO] Reactor build order: 
[INFO]   Unnamed - com.mycompany.app:my-app:pom:1.0-SNAPSHOT
[INFO]   Unnamed - com.mycompany.app:my-module:jar:1.0-SNAPSHOT
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - com.mycompany.app:my-app:pom:1.0-SNAPSHOT
[INFO]    task-segment: [help:active-profiles] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [help:active-profiles {execution: default-cli}]
[INFO] 
Active Profiles for Project 'com.mycompany.app:my-app:pom:1.0-SNAPSHOT': 

The following profiles are active:

 - linux-x86 (source: pom)



Active Profiles for Project 'com.mycompany.app:my-module:jar:1.0-SNAPSHOT': 

There are no active profiles.



[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - com.mycompany.app:my-app:pom:1.0-SNAPSHOT
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] No goals needed for project - skipping
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - com.mycompany.app:my-module:jar:1.0-SNAPSHOT
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/my-app/my-module/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
     [echo] some value
[INFO] Executed tasks
[INFO] 
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] Unnamed - com.mycompany.app:my-app:pom:1.0-SNAPSHOT ... SUCCESS [0.003s]
[INFO] Unnamed - com.mycompany.app:my-module:jar:1.0-SNAPSHOT  SUCCESS [1.256s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Thu Oct 22 18:08:16 CEST 2009
[INFO] Final Memory: 9M/80M
[INFO] ------------------------------------------------------------------------

And if I run maven from the module:

$ cd my-module
$ mvn help:active-profiles compile 
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - com.mycompany.app:my-module:jar:1.0-SNAPSHOT
[INFO]    task-segment: [help:active-profiles] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [help:active-profiles {execution: default-cli}]
[INFO] 
Active Profiles for Project 'com.mycompany.app:my-module:jar:1.0-SNAPSHOT': 

There are no active profiles.



[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - com.mycompany.app:my-module:jar:1.0-SNAPSHOT
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/my-app/my-module/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
     [echo] some value
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Oct 22 18:09:38 CEST 2009
[INFO] Final Memory: 10M/79M
[INFO] ------------------------------------------------------------------------

As you can see, the profile is activated (but not active) and the property defined in the profile in the parent pom is inherited.

Pascal Thivent