Search code examples
mavenantmaven-antrun-plugin

Maven antrun plugin set property based on OS


I am converting an ant script to Maven and decided to use Maven ant run plugin. I am on the good way, but I hit the following problem. Ant source script uses target definition as follows:

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

The point is to use property value

abc.home

later in the build cycle which is dependent on OS (Win,Linux). In the ant script it is ok,but maven ant run plugin does not enable using the multiple targets. I do not want to use Maven profile tags. I would like to use ant tag for this if there is any? Does anybody have a hint?


Solution

  • I think an easier solution would be not to use the maven-antrun-plugin and use profiles instead.

    Profiles are a good way to have a different of properties depending of some activation property. One of those activation property can be the OS which is running the build.

    Consider the following 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>
    
    • When run on a Windows machine, the windows profile will be activated. Reading a property file is done with the properties-maven-plugin and its content will be placed in properties, just like the Ant <property> task.
    • When run on an Unix machine, the unix profile will be activated and the abc.home property will be set to ${env.ABC_HOME}.

    Then, in your Maven build, you can use ${abc.home} without having to worry in which profile you are in.


    Having said that, another solution would be to run multiple executions of the maven-antrun-plugin. In the first execution, you decide whether the build in running in a Windows or Unix machine and then skip the following executions accordingly. This would be a sample configuration, where myPhase would be the phase you want this to run.

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