4

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

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>
4

3 回答 3

6

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

<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>

希望有帮助:)

于 2016-01-05T08:20:45.380 回答
3

对于 Java 9,可以使用以下配置文件。

    <profiles>
        <profile>
            <id>java8</id>
            <activation>
                <jdk>1.8</jdk>
            </activation>
            (...)
        </profile>
        <profile>
            <id>java9</id>
            <activation>
                <jdk>9</jdk>
            </activation>
            (...)
        </profile>
    </profiles>
于 2018-01-29T08:42:54.987 回答
0

您需要声明两个互斥配置文件,一个用于 jdk7 构建,一个用于 jdk8 构建。每个配置文件声明都有两个重要部分:

activation元素就像一个条件,并且嵌套在. 您的个人资料是打开还是关闭?您将需要设置jdk基于激活。您的 jdk7 配置文件示例:

<activation>
  <jdk>1.7</jdk>
</activation>

接下来,您需要定义在您的配置文件处于活动状态时设置的一个或多个属性。jdk7 示例:

<properties>
    <jdk.version>1.7<jdk.version>
</properties>

这两个部分都与一个id元素(例如<id>jdk7</id>)组合并嵌套在一个profile元素中。

如果您在使 jdk 检测激活工作时遇到任何问题,我建议您尝试使用显式触发器,例如使用mvn -P jdk7.

从它的声音来看,您希望在您的pom.xml. 但另一种选择是拥有一个始终打开的配置文件,但jdk.version在文件内部以不同方式定义配置文件的属性~/.m2/settings.xml。办公室的 settings.xml 文件将具有 jdk7 属性;在家里它会有jdk8。

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

于 2016-01-05T08:38:43.277 回答