改变每个子模块的插件配置

时间:2012-10-12 08:47:34

标签: maven maven-3

我有一个多模块构建,其中包含可以针对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.sourceosgi.bree,可以由每个模块定义,但这会留下错误。

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

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

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

为了获得最大的可维护性,我会编写shell脚本(和/或Windows上的批处理文件),以防Maven无法正常工作,例如,set-version.sh(和set-version.bat)循环所有子模块并根据java.compiler.source重置默认的osgi.breeversion-feed.txt属性,version-feed.txt为您提供了一个操作版本变化的中心位置。正如您所看到的,缺点是这不是Maven解决方案,每次需要进行版本自定义时,它需要在set-version.sh之前运行mvn ...

此外,对于构建/发布标准化,我将使用maven-enforcer-plugin基于属性version.set(由set-version.sh标记)来播放/暂停构建过程并提示一些如果开发人员在构建时未遵循正确的过程,则会出现警告/错误消息。如果您希望使用每个子模块中定义的默认值而不是运行version.set,只需在父pom.xml或命令行中直接将其设置为true,set-version.sh也可以提供灵活性参数。

示例目录结构:

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

希望这是有道理的。

答案 2 :(得分: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>