How to do tasks conditionally using maven-antrun-plugin?

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

At the moment I have 3 task blocks and only a task block is executed without any conditions.

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

+6
maven-2 maven-antrun-plugin
source share
4 answers

First, according to the Maven 1.x website, the current stable version for maven 1.x is version 1.1, not 1.4. Secondly, there is no AntRun Plugin version 1.7 and, as far as I know, this is the Maven 2 plugin. Thirdly, the syntax you use is very similar to "Using Attributes" , which, again, refers to Maven 2.

So, I can miss something, but it is very confusing, and you should perhaps clarify these points in your question.

In any case, since you explicitly mentioned Maven 1, I will try to answer. If I remember well, I would write a custom goal and use Jelly core:if or core:when . To do this, enter maven.xml something like this:

 <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 about the syntax, all this Maven 1 stuff is too far away, and I haven't tested it (I'm too lazy to install Maven 1). But I think you will do it. a link to the script can help you.

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

UPDATE: It looks like the OP is actually using Maven 2, so I will update my question accordingly. To implement the desired behavior, you can use the Ant -contrib 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 does not mean "maven way" to do something. Now that I understand a little better than what you are trying to do (but not completely, since you did not explain your final goal), I think that using building profiles would be more appropriate and, after reading your own answer, I think that you complicating things too much (and that you're on the wrong track).

I understand that you are new to Maven, but I suggest trying it out instead of returning to Ant, or you won’t get its benefits. In addition, when opening a question, instead of asking for a specific solution, you should rather explain your general problem, you will get better answers. I can’t give more guidance here because I don’t know what you are really trying to achieve.

+9
source share

You do not need to use AntContrib after maven-antrun-plugin 1.5 , which uses <target> instead of <tasks> according to the use of plugins . This tag works the same as in this case, you can add conditions such as 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 fulfills the first goal, but the second goal depends on the value of the property. You can also configure it for the parent and project of the submodule by defining the plugin on the <pluginsManagement> and calling some properties in the submodules, and then calling the plugin.

+4
source share

This problem has been fixed by creating several named targets with "if" attributes and a condition property in the build.xml file in the project root as follows.

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

The properties passed to the required command line switches and ant called in the build.xml file from the task section in Maven POM are as follows:

 <tasks> <ant antfile="${basedir}/build.xml"> <property name="environment" value="${environment}"/> <target name="prod"/> </ant> </tasks> 
0
source share

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

Another solution would be: save ant -contrib-1.0b3.jar to the path, and then define it as follows

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

Here I will give a complete example of the code that you can download https://www.surasint.com/2017/04/09/run-ant-with-if-from-maven/

0
source share

All Articles