1

我正在使用 Maven 3.2.3。我有一个多模块项目,目前我的 Maven 编译器插件设置为这样

                    <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <version>3.1</version>
                            <configuration>
                                    <source>1.6</source>
                                    <target>1.6</target>
                                    <compilerArgument>-proc:none</compilerArgument>
                                    <fork>true</fork>
                                    <!-- <debug>true</debug> <debuglevel>none</debuglevel> -->
                            </configuration>
                            <executions>
                                    <execution>
                                            <id>default-testCompile</id>
                                            <phase>test-compile</phase>
                                            <goals>
                                                    <goal>testCompile</goal>
                                            </goals>
                                    </execution>
                            </executions>
                    </plugin>

我想知道如何根据系统中的 $JAVA_HOME 变量设置编译器版本,这意味着,如果 $JAVA_HOME 指向 Java 6 安装,我的 Maven 编译器将使用“1.6”作为其源和目标。同样,如果我的 $JAVA_HOME 指向 Java 7 安装,则源和目标将是“1.7”,依此类推。如果环境中没有设置 $JAVA_HOME,我希望编译器默认为 1.6。

感谢您提供有关如何设置的建议和示例。

4

2 回答 2

0

使用 Profiles 为不同环境构建的指南

为不同的环境构建相同的工件一直很烦人。您有多个环境,例如测试和生产服务器,或者可能是一组运行具有不同配置的相同应用程序的服务器。在本指南中,我将解释如何使用配置文件来构建和打包为特定环境配置的工件。有关配置文件概念的更深入解释,请参阅构建配置文件简介。

于 2015-06-08T19:10:24.630 回答
0

正如 Jarrod 指出的那样,解决方案是使用配置文件。使用 Maven 文档提供的示例。

可以根据检测到的构建环境状态自动触发配置文件。这些触发器是通过配置文件本身的一个部分指定的。目前,此检测仅限于 JDK 版本的前缀匹配、系统属性的存在或系统属性的值。这里有些例子。

以下配置将在 JDK 的版本以“1.4”开头时触发配置文件(例如“1.4.0_08”、“1.4.2_07”、“1.4”):

请参阅下面与您的示例和 Maven 文档示例合并的示例。

<profiles>
    <profile>
    <activation>
      <jdk>1.6</jdk>
    </activation>
    <build>
        <plugins>
            <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                            <source>1.6</source>
                            <target>1.6</target>
                            <compilerArgument>-proc:none</compilerArgument>
                            <fork>true</fork>
                            <!-- <debug>true</debug> <debuglevel>none</debuglevel> -->
                    </configuration>
                    <executions>
                            <execution>
                                    <id>default-testCompile</id>
                                    <phase>test-compile</phase>
                                    <goals>
                                            <goal>testCompile</goal>
                                    </goals>
                            </execution>
                    </executions>
            </plugin>
        </plugins>
    </build>
    </profile>
</profiles>
于 2015-06-09T20:33:28.457 回答