【问题标题】:Maven antrun plugin. Passing all files in a directory as an argumentMaven antrun 插件。将目录中的所有文件作为参数传递
【发布时间】:2011-07-21 18:43:15
【问题描述】:

我正在使用 maven-antrun-plugin 使用 Apache Thrift 生成类。当我将一个 thrift 文件指定为参数时,该插件有效,但当我尝试使用通配符 (*) 为所有 thrift 文件生成代码时失败。我从命令行执行了 thrift:

thrift --gen java:beans src/main/resources/*.thrift

这行得通。

但是当我在 pom.xml 中定义这个插件时

            <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <tasks>
                            <mkdir dir="target/generated-sources" />
                            <exec executable="${thrift.executable}" failonerror="true">
                                <arg value="--gen" />
                                <arg value="java:beans" />
                                <arg value="-o" />
                                <arg value="target/generated-sources" />
                                <arg value="${basedir}/src/main/resources/*.thrift" />
                            </exec>
                            <copy todir="src/main/java" overwrite="true">
                                <fileset dir="target/generated-sources/gen-javabean" />
                            </copy>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

上述操作失败并出现错误“无法使用真实路径打开输入文件”。

如何使用 maven-antrun-plugin 指定通配符?

【问题讨论】:

    标签: java ant maven


    【解决方案1】:

    您应该使用maven thrift plugin。 我假设 arg 转义 * 并按原样传递。您的第一个命令有效,因为 shell 确实为您扩展了 *。 Thrift 无法扩展通配符本身。

    除此之外,目录使用非常错误。

    编辑您的文件应为:

    <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <tasks>
                            <!-- always use properties if available -->
                            <mkdir dir="${build.directory}/generated-sources" />
                            <exec executable="${thrift.executable}" failonerror="true">
                                <arg value="--gen" />
                                <arg value="java:beans" />
                                <arg value="-o" />
                                <arg value="${build.directory}/generated-sources/thrift" />
                                <!-- since this is a special type of source, it has to be in its own dir -->
                                <arg value="src/main/thrift/*.thrift" />
                            </exec>
                                <!-- You never ever copy generated stuff back into src/* -->
                                <!-- use Build Helper Maven Plugin to add the generated source -->
                                <copy todir="src/main/java" overwrite="true">
                                <fileset dir="target/generated-sources/gen-javabean" />
                            </copy>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    

    【讨论】:

    • 我没有明白你所说的“目录使用非常错误”的意思
    【解决方案2】:

    @Michael-O 当我尝试在 arg 中使用星号 (*) 时,Maven 抱怨:

    [INFO] --- maven-antrun-plugin:1.3:run (generate-sources) @ ---
    [INFO] Executing tasks
         [exec] 
         [exec] [FAILURE:arguments:1] Could not open input file with realpath:
    src/main/thrift/*.thrift
         [exec] Result: 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多