在maven-antrun-plugin中调用foreach

时间:2012-05-23 22:13:58

标签: maven ant ant-contrib maven-antrun-plugin

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

  1. Maven调用maven-antrun-plugin。
  2. Ant-contrib <foreach/>遍历目录并查找所有.less文件并调用目标。
  3. 目标执行Rhino,执行LESS转换文件。
  4. 为此,我有以下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>
    

    我遇到的问题是“主”目标开始执行但在<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]
    

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

2 个答案:

答案 0 :(得分:6)

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

target/antrun/build-main.xml

深入挖掘plug-in documentation以下内容:

  

此插件并不打算提供污染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>

答案 1 :(得分:1)

您知道lesscss-maven-plugin我认为应该解决您的问题。

相关问题