在Maven的antrun-插件调用的foreach [英] Calling foreach in maven-antrun-plugin

查看:443
本文介绍了在Maven的antrun-插件调用的foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立的maven在我的web项目执行上的目录少CSS preprocessor。其基本流程是:

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调用Maven的antrun-插件。

  2. 蚂蚁的contrib <的foreach /> 遍历目录,发现所有的.LESS文件并调用目标

  3. 目标执行犀牛,它执行LESS该文件进行转换。

  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.

要做到这一点,我有以下XML:

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>

我现在遇到的问题是,主的目标开始执行,但随后在失败&LT;&的foreach GT;

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

看来,东西蚂蚁导致它尝试读取POM文件,可能是在寻找目标。我看,这两个指标均生成到目标文件/ antrun /集结main.xml中和目标/ antrun /集结processLessFile.xml所以这是它应该寻找在目录中。

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.

推荐答案

蚂蚁插件似乎并不支持多个目标部分的声明。您可以查看生成的Ant脚本来看看我说的:

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

挖掘更深层次的插件文档规定如下:

这不是这个插件的打算提供污染POM的一种手段,因此它鼓励所有的Ant任务移动到一个build.xml文件,只是使用Ant的任务从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.xml file and just call it from the POM using Ant's task.

苛刻的话,但建议十分合理。我建议你​​移动你的ANT逻辑放到一个专门的文件,并调用它,如下所示:

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>

这篇关于在Maven的antrun-插件调用的foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆