Mvn compile before exec

7.5k Views Asked by At

I am trying to set up my POM such that when I do mvn exec:exec or mvn exec:java it will first compile the source and iff successful, execute it.

I have the following and have tried moving the <execution> part about but can't get it to work:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
            <executions>
                <execution>
                    <phase>exec</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>my.main.class</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

When I do either mvn exec:exec ... or mvn exec:java it doesn't first compile. I have tried putting the <execution> part in the exec plugin section but that didn't work either?

3

There are 3 best solutions below

1
Linuslabo On

You can bind the exec plugin to a phase following compile in build lifecycle (verify in the example below):

<profile>
    <id>proxy</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                           <mainClass>my.main.class</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile> 

and than run mvn verify.

I see the answer is very late and you may have found a solution. I'm just leaving as a reference for others who may need it.

0
Gilberto Olimpio On

It's an old topic, but someone else might be interested in an alternative solution for this.

It's not exactly what you were looking for, but you can compile and execute using a single command:

mvn compile exec:exec

This way Maven will always compile the project before executing it.

0
White_King On

This will not quite answer the question but it will help us to accomplish the main objective you have.

Instead of executing a build goal when running the mvn exec:java we can execute the mvn exec:java when calling a 'post-build" phase/goal.

This will effectively make that phase unusable as it was because we will be substituting it by the execution but if we have a "post-build" goal that we are not using (like package or install) we can use it for the execution like so: using package goal will end up like:

...
    <properties>
        ...
        <main.class>${project.groupId}.MainClass</main.class>
        ...
    </properties>
...
    <build>
        <plugins>
...
            <!-- region Forces the app to be executed when using mvn package -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <mainClass>${main.class}</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- endregion -->
...
        </plugins>
    </build>

This way when we execute goal package with: mvn package the plugin will be executed and the application runned.