使用Maven构建多平台多模块

时间:2009-10-22 08:56:35

标签: java maven-2 swt

我有一个大型项目,它在几个不同的平台上使用SWT,如windows(32位和64位),linux-gtk(32位和64位)等。该项目有几个模块,结构简单:

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

我希望能够在所有支持的平台上运行Eclipse中的代码。例如。如果我查看32位Windows机器上的swt-bridge模块,它应该具有32位Windows SWT依赖项,并且还将所需的DLL解压缩到某个目录,然后我可以将其添加到{{1} }。

我可以通过定义java.library.pathos.family激活的配置文件来指定不同的依赖关系。

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

我还可以使用os.arch从SWT JAR中提取任何DLL,.so文件到maven-dependency-plugin,并从Eclipse中使用它们。

如果我将配置文件放在target/swtlib模块中,这样可以正常工作。但是这样我无法看到其他项目中活动配置文件设置的属性,例如我不能将依赖插件用于这些属性(我想这样做)。

我认为我可以通过将配置文件移动到父POM来解决这个问题,事实上,如果我构建整个产品,它就可以工作。但是这样我就不能只构建一个模块,因为Maven会抱怨缺少属性。

我很确定很多人都会遇到这个问题,所以必须有办法在Maven中正确处理这个问题。谁能告诉我它是什么?

1 个答案:

答案 0 :(得分:3)

项目继承应该在这里工作:如果你将它们放在父pom 中,如果激活了配置文件,则继承properties。请注意,我说当前项目的已激活而非有效(有关这两个术语之间的差异,请参阅MNG-3228)。

我已经设置了一个简单的多模块构建,演示了使用maven 2.2.1从父pom中的配置文件继承属性。该项目具有以下结构:

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

父pom.xml定义如下(适用于我的平台):

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

这是模块的pom:

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

如果我从父母那里运行Maven:

$ 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] ------------------------------------------------------------------------

如果我从模块中运行maven:

$ 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] ------------------------------------------------------------------------

如您所见,配置文件已激活(但未激活),并且父pom中配置文件中定义的属性将被继承。