Maven antrun插件。将目录中的所有文件作为参数传递

时间:2011-07-21 18:43:16

标签: java ant maven

我使用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>

上述操作失败,错误“无法打开带有realpath的输入文件”。

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

2 个答案:

答案 0 :(得分:4)

您应该使用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>

答案 1 :(得分:0)

@ 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