Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling foreach in maven-antrun-plugin

I'm trying to set up maven to execute the LESS CSS preprocessor on a directory in my web project. The basic flow is:

  1. Maven calls maven-antrun-plugin.
  2. Ant-contrib <foreach/> loops through a directory and finds all the .less files and calls a target.
  3. The target executes Rhino, which executes LESS which converts the file.

To do this, I have the following XML:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <configuration>
                        <target name="processLessFile">
                            <!-- ... -->
                        </target>
                        <target name="main">
                            <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.plugin.classpath"/>
                            <property name="css.dir" location="src/main/webapp/css"/>
                            <foreach param="file" target="processLessFile">
                                <fileset dir="${css.dir}">
                                    <filename name="**/*.less" />
                                </fileset>
                            </foreach>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>ant</groupId>
                    <artifactId>ant-contrib</artifactId>
                    <version>1.0b2</version>
                </dependency>
            </dependencies>
        </plugin>

The problem I'm encountering is that the "main" target starts to execute but then fails in <foreach>:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (default) on project model-web-project: An Ant BuildException has occured: The following error occurred while executing this line:
[ERROR] /Users/.../pom.xml:4: Unexpected element "{http://maven.apache.org/POM/4.0.0}project" {antlib:org.apache.tools.ant}project
[ERROR] around Ant part ...<foreach param="file" target="processLessFile">... @ 6:50 in /Users/.../target/antrun/build-main.xml
[ERROR] -> [Help 1]

It appears that something in Ant is causing it to try read the POM file, possibly looking for the target. I see that both targets were generated into files target/antrun/build-main.xml and target/antrun/build-processLessFile.xml so that is the directory it should be looking in.

like image 362
Brian Nickel Avatar asked May 23 '12 22:05

Brian Nickel


People also ask

What is Maven AntRun plugin used for?

This plugin provides the ability to run Ant tasks from within Maven. You can even embed your Ant scripts in the POM! It is not the intention of this plugin to provide a means of polluting the POM, so it's encouraged to move all your Ant tasks to a build.

How do I run an Ant script from Maven?

Action. Configure the run goal form the Maven AntRun plugin. Define any properties you wish to pass to the external Ant build, and then call the external Ant build with the ant task, specifying the antfile and target you wish to execute. To execute this external Ant build, run mvn package.

What is Maven Ant task?

The Maven Ant Tasks allow several of Maven's artifact handling features to be used from within an Ant build. These include: Dependency management - including transitive dependencies, scope recognition and SNAPSHOT handling. Artifact deployment - deployment to a Maven repository (file integrated, other with extensions)


2 Answers

The ant plugin does not appear to support the declaration of more than one target section. You can check the generated ANT script to see what I'm talking about:

target/antrun/build-main.xml

Digging deeper the plug-in documentation states the following:

It is not the intention of this plugin to provide a means of polluting the POM, so it's encouraged to move all your Ant tasks to a build.xml file and just call it from the POM using Ant's task.

Harsh words, but the advice is sound. I'd recommend moving your ANT logic into a dedicated file and invoke it as follows:

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <configuration>
                        <target>
                            <ant antfile="${basedir}/src/main/ant/build.xml"/>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
like image 98
Mark O'Connor Avatar answered Oct 05 '22 23:10

Mark O'Connor


You are aware of the lesscss-maven-plugin which i think should solve your problems.

like image 45
khmarbaise Avatar answered Oct 06 '22 00:10

khmarbaise