在maven-antrun-plugin中调用foreach

Bri*_*kel 5 ant ant-contrib maven maven-antrun-plugin

我正在尝试设置maven来在我的web项目中的目录上执行LESS CSS预处理器.基本流程是:

  1. Maven调用maven-antrun-plugin.
  2. Ant-contrib <foreach/>循环遍历目录并查找所有.less文件并调用目标.
  3. 目标执行Rhino,执行LESS转换文件.

为此,我有以下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>
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是"主"目标开始执行但后来失败<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]
Run Code Online (Sandbox Code Playgroud)

似乎Ant中的某些东西导致它尝试读取POM文件,可能正在寻找目标.我看到两个目标都生成了文件target/antrun/build-main.xml和target/antrun/build-processLessFile.xml,因此它应该是它应该查找的目录.

Mar*_*nor 6

ant插件似乎不支持多个目标部分的声明.您可以检查生成的ANT脚本以查看我在说什么:

target/antrun/build-main.xml
Run Code Online (Sandbox Code Playgroud)

深入挖掘插件文档说明如下:

这个插件并不打算提供一种污染POM的方法,因此鼓励将所有Ant任务移动到build.xml文件,然后使用Ant的任务从POM中调用它.

苛刻的话,但建议是合理的.我建议将您的ANT逻辑移动到专用文件中并按如下方式调用它:

        <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>
Run Code Online (Sandbox Code Playgroud)