【问题标题】:Calling foreach in maven-antrun-plugin在 maven-antrun-plugin 中调用 foreach
【发布时间】:2012-05-30 12:21:37
【问题描述】:

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

  1. Maven 调用 maven-antrun-plugin。
  2. Ant-contrib <foreach/> 循环遍历目录并找到所有 .less 文件并调用目标。
  3. 目标执行 Rhino,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>

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

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

【问题讨论】:

  • 你的意思是 ant-contrib 而不是 ant-collab,不是吗?
  • 谢谢,我一直打错字。

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


【解决方案1】:

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

【讨论】:

  • 会,但我正在尝试在我公司的内部 Maven 存储库中使用软件,然后再添加新的依赖项。
【解决方案2】:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 2012-07-19
    • 1970-01-01
    相关资源
    最近更新 更多