Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to execute a program in maven to help build your project?

I am currently working on a project that uses a tool that takes the following example IDL file and generates about 5 Java classes from it.

struct Example { int x; int y; };

Is there a way to get Maven to use the command line tool that we use to automatically create those Java classes when it builds?

like image 365
Edward Kennedy Avatar asked Jan 10 '12 16:01

Edward Kennedy


People also ask

How Maven is useful in build an application?

Maven is chiefly used for Java-based projects, helping to download dependencies, which refers to the libraries or JAR files. The tool helps get the right JAR files for each project as there may be different versions of separate packages.

What is the command to quickly build your Maven site?

Explanation. mvn site command can quickly builds Maven site.

How do I build a project using mvn clean package?

Let's open the command console, go the C:\MVN\consumerBanking directory and execute the following mvn command. Maven will start building the project. We give maven two goals, first to clean the target directory (clean) and then package the project build output as jar (package).


2 Answers

Here is an example using the Exec Maven Plugin.

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <!-- this execution happens just after compiling the java classes, and builds the native code. -->
                <id>build-native</id>
                <phase>process-classes</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>src/main/c/Makefile</executable>
                    <workingDirectory>src/main/c</workingDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
like image 132
Peter Lawrey Avatar answered Oct 14 '22 17:10

Peter Lawrey


You can use maven-antrun-plugin plugin to run arbitrary Ant tasks or even any command-line program:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <configuration>
                        <target>
                            <exec executable="ls">
                                <arg value="-l"/>
                                <arg value="-a"/>
                            </exec>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

With this configuration your command-line program will be executed before the compilation, so generated Java sources will be available to the rest of the code.

like image 2
Tomasz Nurkiewicz Avatar answered Oct 14 '22 16:10

Tomasz Nurkiewicz