Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven antrun plugin. Passing all files in a directory as an argument

Tags:

java

maven

ant

I am using maven-antrun-plugin to generate classes using Apache Thrift. The plugin works when I specify one thrift file as an argument but fails when I try using a wildcard (*) to generate code for all thrift files. I executed thrift from the command line:

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

And this works.

But when I define this plugin in my 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>

The above fails with error "Could not open input file with realpath".

How do I specify wildcards using maven-antrun-plugin?

like image 248
Prasanna Avatar asked Jan 22 '26 10:01

Prasanna


1 Answers

You should rather use the maven thrift plugin. I assume that arg escapes the * and passes as is. Your first command works because the shell does expand the * for you. Thrift ist not able to expand the wildcard itself.

Besides that, the directories use are tremendously wrong.

Edit your file should read:

<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>
like image 96
Michael-O Avatar answered Jan 24 '26 23:01

Michael-O



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!