gpt4 book ai didi

maven - 在maven-antrun-plugin中调用foreach

转载 作者:行者123 更新时间:2023-12-02 02:06:08 33 4
gpt4 key购买 nike

我正在尝试设置 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>

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

深入挖掘plug-in documentation声明如下:

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 逻辑移至专用文件中并按如下方式调用它:

        <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 - 在maven-antrun-plugin中调用foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10728645/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com