5

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

  1. Maven 调用 maven-antrun-plugin。
  2. Ant-contrib<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 中,所以这是它应该查找的目录。

4

2 回答 2

6

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>
于 2012-05-24T19:27:32.910 回答
1

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

于 2012-05-24T06:36:14.867 回答