Maven 构建配置文件激活 [英] Maven Build Profile Activation

查看:111
本文介绍了Maven 构建配置文件激活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一些相关的主题,但我仍然不明白如何去做.

I know there are some related topics but still I can't understand how to do it.

我正在学习 maven,目前正在创建构建配置文件.我想让 maven 自动检测我机器上当前安装的 java 版本.假设我在使用 (jdk7) 或 home (jdk8) 的办公室工作,我想要 和 <maven-compiler-plugin pom.xml 中的 code><target> 元素自动检测 java -version 无论环境(办公室/家庭)如何.我已经阅读了关于 activation 的内容,但不能完全理解其目的.

I'm learning maven and currently in process of creating build profiles. I want maven to auto detect the currently installed java version on my machine. Let's say I'm working in our office which uses (jdk7) or home (jdk8), I want the <source> and <target> elements in maven-compiler-plugin pom.xml to auto detect the java -version regardless of environment (office / home). I've read about activation but can't perfectly understand the purpose.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>   
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>    
        </plugin>
    </plugins>
  </build>

推荐答案

我不认为你可以让你的构建、环境自动感知,只使用默认值,你需要一些配置文件的帮助及其激活功能,请参阅 此处.您可以做的是引入 2 个不同的配置文件,在每个配置文件中您可以定义所需的 JDK,如果存在,它将为您激活,然后您可以使用不同的源/目标配置编译器插件,或者只设置不同的值对于将指示 java 版本的属性.示例:

Hi I don't think you can have your build, environment aware automatically, just using the defaults, you need some help of profiles and their activation capability see here. What you can do is introduce 2 different profiles, in each profile you can define the JDK you want, and it will be activated for your if present, then you can configure either the compiler plugin with different source /target or, just set different values for a a property that is going to indicate the java version. Example:

<profiles>
 <profile>
     <id>java8</id>
     <activation>
       <jdk>1.8</jdk>
     </activation>
   <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>   
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>    
        </plugin>
    </plugins>
  </build>
   </profile>
<profile>
     <id>java7</id>
     <activation>
       <jdk>1.7</jdk>
     </activation>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>   
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>    
        </plugin>
    </plugins>
  </build>
   </profile>
</profiles>

希望有帮助:)

这篇关于Maven 构建配置文件激活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆