2

我有一个多模块构建,其中包含可以针对 Java 5 或 Java 6 的模块。我希望允许模块选择加入 Java 6,并将默认值保留为 5。

要将 Java 5 设置为目标,我需要配置以下内容:

  • maven-compiler-plugin:源和目标设置为 1.5
  • maven-bundle-plugin: 将 Bundle-RuntimeExecutionEnvironment 配置为 J2SE-1.5

要将 Java 6 设置为目标,我需要配置以下内容:

  • maven-compiler-plugin:源和目标设置为 1.6
  • maven-bundle-plugin: 将 Bundle-RuntimeExecutionEnvironment 配置为 JavaSE-1.6

我考虑有两个属性:java.compiler.source并且osgi.bree可以由每个模块定义,但这为错误留下了空间。

如何使用单个开关覆盖每个模块的这两个插件的配置?

4

3 回答 3

3

我会亲自构建您的项目,以便 Java 5 模块来自一个父 POM,Java 6 模块来自另一个父 POM。

Global Parent (majority of global settings)
  Java5 parent (just define source/bundle)
    module A
    module B
  Java 6 parent (just define source/bundle)
    module C
于 2012-10-12T08:55:00.623 回答
2

允许子模块设置my.java.version属性(或任何您想要的名称)并嵌入一个为编译器和捆绑插件设置版本属性的 Groovy 脚本怎么样?在父 pom 中是这样的:

<project ...>
    ...
    <properties>
        <my.java.version>1.5</my.java.version>     <!-- default Java version -->
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.groovy.maven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <!-- set up properties in an early lifecycle phase -->
                        <phase>initialize</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <!-- this can be as simple or complex as you need it to be -->
                            <source>
                                if (project.properties['my.java.version'] == '1.6') {
                                    project.properties['my.compiler.version'] = '1.6'
                                    project.properties['my.execution.environment.version'] = 'JavaSE-1.6'
                                }
                                else {
                                    project.properties['my.compiler.version'] = '1.5'
                                    project.properties['my.execution.environment.version'] = 'J2SE-1.5'
                                }
                            </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <!-- now use the properties from above in the plugin configurations -->
        <!-- assume that both of these plugins will execute in a phase later than 'initialize' -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${my.compiler.version}</source>
                        <target>${my.compiler.version}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <configuration>
                        <!-- sorry if this part isn't correct; never used this plugin before -->
                        <instructions>
                            <Bundle-RuntimeExecutionEnvironment>${my.execution.environment.version}</Bundle-RuntimeExecutionEnvironment>
                        </instructions>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
于 2012-10-18T20:06:09.573 回答
2

我不认为有一个优雅的 Maven 方法来解决这个复杂的场景,当子模块的数量变得巨大时,你或 Duncan 提出的解决方案都不是易于维护的 IMO。

为了获得最大的可维护性,我会编写 shell 脚本(和/或 Windows 上的批处理文件)以防 Maven 不能很好地完成这项工作,例如,一个set-version.sh(and set-version.bat) 循环所有子模块并重置默认值java.compiler.sourceosgi.bree基于a version-feed.txt,这version-feed.txt为您提供了一个单一的中心位置来操作您的版本变化。如您所见,缺点是这实际上不是一个 Maven 解决方案,它需要在每次需要版本定制set-version.sh之前运行。mvn ...

此外,对于构建/发布标准化,我会使用maven-enforcer-plugin来播放/暂停基于属性version.set(由 标记set-version.sh)的构建过程,如果开发人员没有遵循正确的程序做构建。version.set如果您更喜欢使用在每个子模块中定义的默认值,而不是运行,这也提供了灵活性set-version.sh,只需在父 pom.xml 或命令行参数中直接将其设置为 true。

示例目录结构:

parent/
    module-a/
    module-b/
    module-c/
    ... ...
    pom.xml
    set-version.sh
    set-version.bat
    version-feed.txt

希望这是有道理的。

于 2012-10-18T10:15:42.580 回答