在maven-antrun-plugin中调用foreach

2023-12-13

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

  1. Maven 调用 maven-antrun-plugin。
  2. 蚂蚁贡献<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>

我遇到的问题是“主”目标开始执行但随后失败<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 中,因此这就是它应该查找的目录。


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

target/antrun/build-main.xml

深入挖掘插件文档陈述如下:

该插件的目的不是提供污染 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在maven-antrun-plugin中调用foreach 的相关文章

随机推荐