9

I need to execute some ant commands depending on an environment variable passed in as a parameter to the maven build command.

At the moment I have 3 tasks blocks and only the tasks block with no condition is being executed.

<tasks name="isProdCheck">
  <condition property="isProd">
    <equals arg1="${environment}" arg2="PROD" />
  </condition>
</tasks>

<tasks if="isProd" depends="isProdCheck">
...
</tasks>

<tasks>
... I am the only block executed
</tasks>

What am I doing wrong, is there a better way to do this?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
vaughan
  • 6,982
  • 6
  • 47
  • 63

4 Answers4

12

First, according to Maven 1.x website, the current stable release for maven 1.x is version 1.1, not 1.4. Second, there is no AntRun Plugin version 1.7 and, to my knowledge, this is a Maven 2 plugin. Third, the syntax you are using seems very similar to Using Attributes which, again, is about Maven 2.

So, I may be missing something but, this is very confusing and you should maybe clarify these points in your question.

Anyway, as you explicitly mentioned Maven 1, I'll try to answer. If I remember well, I would write a custom goal and use Jelly's core:if or core:when. To do so, provide something like this in maven.xml:

<project xmlns:j="jelly:core" xmlns:ant="jelly:ant">
  <goal name="my-goal">
    <j:if test="${environment == 'PROD'}">
      <ant:xxx .../>
    </j:if>
  </goal>
</project>

I'm really not sure of the syntax, all this Maven 1 stuff is just too far away, and I didn't test it (I'm too lazy to install Maven 1). But I guess you will. The scripting reference may help you.

To be honest, I really hope you have a good reason to prefer Maven 1.x over Maven 2.x :)

UPDATE: It appears that the OP is actually using Maven 2 so I'll update my question accordingly. To implement the desired behavior, you could use Ant-contrib's if task as shown below:

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                  classpathref="maven.plugin.classpath" />
                <if>
                  <equals arg1="${foo}" arg2="bar" />
                  <then>
                    <echo message="The value of property foo is bar" />
                  </then>
                  <else>
                    <echo message="The value of property foo is not bar" />
                  </else>
                </if>
              </tasks>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>20020829</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

And then call mvn compile -Dfoo=bar (this is just an example).

But, all this is not the "maven way" to do things. Now that I understand a bit better what you are trying to do (but not entirely as you didn't explain your ultimate goal), I think that using build profiles would be more appropriate and, having read your own answer, I think that you are over complicating things (and that you are on the wrong path).

I understand that you are a Maven beginner but I'd suggest to try to use it though instead of falling back on Ant or you won't get the benefits of it. Also, when opening a question, instead of asking for a specific solution, you should rather explain your general problem, you'll get better answers. Here, I can't provide more guidance as I don't know what you are really trying to achieve.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Apologies, quite new to Maven. I am using Maven 2.0.9 with JDK 1.4. – vaughan Dec 29 '09 at 23:36
  • No problem, this makes more sense. I'll update my answer accordingly. – Pascal Thivent Dec 29 '09 at 23:46
  • Thanks for your help Pascal. Your solution above looks like exactly what I was trying to acheive. I couldn't get the 'if' statements working before as I was not aware that i needed to include an ant-contrib resource. Essentially what I am trying to achieve is creating a directory structure on an app server which differs depending on whether I am deploying to a development/testing environment or a production environment. I have just had a quick look at build profiles and it sounds like this would be a way better way of doing things. Thanks! – vaughan Dec 30 '09 at 03:20
8

You don't need to use AntContrib after maven-antrun-plugin 1.5 that uses <target> instead of <tasks> according to plugin usage. This tag works in the same way as , but in this one you can add conditions like the example below.

<properties>
    <execute.my.target>true</execute.my.target>
</properties>

<build>
    ...
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>config</id>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <!-- this target will be executed always -->
                        <target>
                            <echo message="Hello there, I'm a simple target" />
                        </target>
                        
                        <!-- 
                            This target will be executed if and only if
                            the property is set to true
                        -->
                        <target name="my-target" if="execute.my.target">
                            <echo message="Conditional target..." />
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    ...
</build>

The code above always executes the first target, but the second target depends on a property value. You can configure it for a parent and a sub-module project too, defining the plugin on <pluginsManagement> tag and calling some properties at the sub-modules, then calling the plugin.

Update: Be sure to use the if parameter without ${}, since it can cause troubles mentioned in the comments section.

nightshiba
  • 37
  • 5
R. Karlus
  • 2,094
  • 3
  • 24
  • 48
  • Thanks ! "my-target" is executed only if "execute.my.target" is defined and equals "true", "on", or "yes". – Sxilderik Jul 05 '17 at 10:17
  • 4
    Not working in 1.8. If I put two `target` in `` the plugin only executes the last one (and Intellij IDEA marks the the last target with error on pom.xml). – Dherik Nov 29 '17 at 18:34
  • how about if equals a value?? – mjs Jan 03 '18 at 15:55
  • 2
    As @Dherik said, not working with multiple targets under ``. But works when you define multiple `s` (with unique IDs) with one `` per `` – kool79 May 02 '18 at 20:44
0

Resolved this issue by creating multiple named targets with "if" attributes and a condition property in a build.xml file in the project root as follows.

<target name="prod" if="isProd" depends="isProdCheck">
    // do something
</target>

Passed properties of the command line switches I required, and called the ant targets in the build.xml file from the tasks section in the Maven POM as follows:

<tasks>
 <ant antfile="${basedir}/build.xml">
    <property name="environment" value="${environment}"/>        
    <target name="prod"/>
 </ant>          
</tasks>
vaughan
  • 6,982
  • 6
  • 47
  • 63
  • No offense but this is an horrible way to use maven to solve your problem. – Pascal Thivent Dec 30 '09 at 00:55
  • why do you need to call a specific target and than check if you called the right target? Can't you just call the right target from maven depending on the environment (e.g. tasked named according to the environment names)? – Peter Schuetze Dec 30 '09 at 16:58
0

runable example here https://www.surasint.com/run-ant-with-if-from-maven/

Another solution would be: keep the ant-contrib-1.0b3.jar to a path and then define it like this

<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="${runningLocation}/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

then

<target name="doSomething">
    <if>
        <equals arg1="${someProp}" arg2="YES" />
        <then>
            <echo message="It is YES" />
        </then>
        <else>
            <echo message="It is not YES" />
        </else>
    </if>
</target>

I put full code example here which you can download https://www.surasint.com/2017/04/09/run-ant-with-if-from-maven/

Surasin Tancharoen
  • 5,520
  • 4
  • 32
  • 40