Maven 每个子模块的不同插件配置
我有一个多模块构建,其中包含可以针对Java5或Java6的模块。我希望允许模块选择使用Java6,并将默认值保留为5 要将Java 5设置为目标,我需要配置以下内容:Maven 每个子模块的不同插件配置,maven,maven-3,Maven,Maven 3,我有一个多模块构建,其中包含可以针对Java5或Java6的模块。我希望允许模块选择使用Java6,并将默认值保留为5 要将Java 5设置为目标,我需要配置以下内容: maven编译器插件:源和目标设置为1.5 maven bundle plugin:将bundle RuntimeExecutionEnvironment配置为J2SE-1.5 要将Java 6设置为目标,我需要配置以下内容: maven编译器插件:源和目标设置为1.6 maven bundle plugin:将bundl
:源和目标设置为1.5maven编译器插件
:将bundle RuntimeExecutionEnvironment配置为J2SE-1.5maven bundle plugin
:源和目标设置为1.6maven编译器插件
:将bundle RuntimeExecutionEnvironment配置为JavaSE-1.6maven bundle plugin
java.compiler.source
和osgi.bree
,这两个属性可以由每个模块定义,但这会导致错误
如何用一个开关覆盖每个模块的这两个插件的配置?我个人会构建您的项目,以便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
我不认为有一个优雅的Maven方法可以解决这个复杂的场景,当子模块的数量变得巨大时,您或Duncan提出的解决方案在IMO中都不容易维护 为了获得最大的可维护性,我会编写shell脚本(和/或Windows上的批处理文件),以防Maven不能很好地完成这项工作,例如,
set version.sh
(和set version.bat
)循环所有子模块并重置默认的java.compiler.source
和osgi.bree
属性,基于version feed.txt
,version feed.txt
为您提供了一个操作版本变化的中心位置。正如您所看到的,缺点是这不是一个Maven解决方案,它需要在mvn…
之前运行set version.sh
此外,对于构建/发布标准化,我将基于属性version.set
(由set version.sh
标记)播放/暂停构建过程,并在开发人员在进行构建时未遵循正确的过程时提示一些警告/错误消息。如果您希望使用每个子模块中定义的默认值,而不是运行set version.sh
,只需在父pom.xml或从命令行参数中直接将其设置为true,version.set
也提供了灵活性
目录结构示例:
parent/
module-a/
module-b/
module-c/
... ...
pom.xml
set-version.sh
set-version.bat
version-feed.txt
希望这是有意义的。允许子模块设置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>
...
1.5
org.codehaus.groovy.maven
gmaven插件
1
初始化
执行
if(project.properties['my.java.version']='1.6'){
project.properties['my.compiler.version']='1.6'
project.properties['my.execution.environment.version']='JavaSE-1.6'
}
否则{
project.properties['my.compiler.version']='1.5'
project.properties['my.execution.environment.version']='J2SE-1.5'
}
maven编译器插件
${my.compiler.version}
${my.compiler.version}
org.apache.felix
maven捆绑插件
${my.execution.environment.version}
谢谢你的回答;我不会为此而重组一个100多个项目。我同意它可以解决一个新项目的问题,但不是我的。我不知道你有这么多项目。一个稍微简单一点的替代方案是让您的Java 6项目使用一个单独的父项目,覆盖全局父项目中的Java 5设置,Ant和Maven经常使用shell脚本来解决构建/发布/部署大型企业项目的一些复杂场景。谢谢