在 maven-antrun-plugin 中调用 foreach [英] Calling foreach in maven-antrun-plugin
问题描述
我正在尝试设置 maven 以在我的 Web 项目中的目录上执行 LESS CSS 预处理器.基本流程是:
I'm trying to set up maven to execute the LESS CSS preprocessor on a directory in my web project. The basic flow is:
- Maven 调用 maven-antrun-plugin.
- Ant-contrib
循环遍历一个目录并找到所有 .less 文件并调用一个目标. - 目标执行 Rhino,后者执行 LESS 以转换文件.
- Maven calls maven-antrun-plugin.
- Ant-contrib
<foreach/>
loops through a directory and finds all the .less files and calls a target. - 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>
我遇到的问题是主要"目标开始执行但随后在
中失败:
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]
看起来 Ant 中的某些东西导致它尝试读取 POM 文件,可能是在寻找目标.我看到这两个目标都生成到文件 target/antrun/build-main.xml 和 target/antrun/build-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 插件似乎不支持多个目标部分的声明.您可以检查生成的 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-plugin 中调用 foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!