Maven antrun 插件集 属性 基于 OS

Maven antrun plugin set property based on OS

我正在将 ant 脚本转换为 Maven 并决定使用 Maven ant 运行 插件。我走的很好,但我遇到了以下问题。 Ant源脚本使用目标定义如下:

<condition property="isWindows">
  <os family="windows"/>
</condition>
<condition property="isUnix">
  <os family="unix"/>
</condition>


<target name="init-windows" depends="" if="isWindows">
  <property file="${user.home}/abc.properties"/>
</target>

<target name="init-unix" depends="" if="isUnix">
 <property name="abc.home" value="${env.ABC_HOME}"/>
</target>

重点是使用属性值

abc.home

稍后在依赖于OS(Win,Linux)的构建周期中。 在 ant 脚本中没问题,但是 maven ant 运行 插件无法使用多个目标。我不想使用 Maven 配置文件标签。如果有的话,我想为此使用 ant 标签?有人有提示吗?

我认为更简单的解决方案是使用maven-antrun-plugin,而是使用profiles

配置文件是一种很好的方式,可以根据某些激活来获得不同的属性 属性。这些激活之一 属性 可以是 OS,它正在构建 运行。

考虑以下 POM:

<profiles>
  <profile>
    <id>windows</id>
    <activation>
      <os>
        <family>windows</family>
      </os>
    </activation>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
          <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>${user.home}/abc.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </profile>
  <profile>
    <id>unix</id>
    <activation>
      <os>
        <family>unix</family>
      </os>
    </activation>
    <properties>
        <abc.home>${env.ABC_HOME}</abc.home>
    <properties>
  </profile>
</profiles>
  • 当 运行 在 Windows 机器上时,windows 配置文件将被激活。读取 属性 文件是通过 properties-maven-plugin 完成的,其内容将被放置在属性中,就像 Ant <property> 任务一样。
  • 当 运行 在 Unix 机器上时,unix 配置文件将被激活并且 abc.home 属性 将被设置为 ${env.ABC_HOME}

然后,在您的 Maven 构建中,您可以使用 ${abc.home} 而不必担心您在哪个配置文件中。


话虽如此,另一种解决方案是 运行 多次执行 maven-antrun-plugin。在第一次执行时,您决定在 运行ning 中构建 Windows 还是 Unix 机器,然后相应地跳过以下执行。这将是一个示例配置,其中 myPhase 将是您希望 运行.

的阶段
<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>myphase</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <condition property="isWindows">
                        <os family="windows" />
                    </condition>
                    <condition property="isUnix">
                        <os family="unix" />
                    </condition>
                </target>
                <exportAntProperties>true</exportAntProperties>
            </configuration>
        </execution>
        <execution>
            <phase>myphase</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target name="init-windows" depends="">
                    <property file="${user.home}/abc.properties" />
                </target>
                <exportAntProperties>true</exportAntProperties>
                <skip>${isUnix}</skip>
            </configuration>
        </execution>
        <execution>
            <phase>myphase</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target name="init-unix" depends="">
                    <property name="abc.home" value="${env.ABC_HOME}" />
                </target>
                <exportAntProperties>true</exportAntProperties>
                <skip>${isWindows}</skip>
            </configuration>
        </execution>
    </executions>
</plugin>