Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify the order of plugins (bound to the same phase) in Maven 3?

I have a multi-module Scala project using Maven 3. I want to make sure that the Scala compiler runs before the Java compiler, both for compile and test-compile phases.

I have tried, in both my parent POM and module POM, listing the plugins in the build section so that the Scala compiler plugin is first, and yet Maven still insists on running the Java compilation first, which fails in some cases where I have mixed source.

I know I can solve this by binding the Scala compilation to process-resources instead of compile, but I'd really prefer to know how I can tell Maven to order the plugins (or whether this is possible).

Here is the part of my parent POM that defines these two plugins:

        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <version>2.15.2</version>
            <configuration>
                <scalaVersion>${scala.version}</scalaVersion>
            </configuration>
            <executions>
                <execution>
                    <id>compile</id>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <phase>compile</phase>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                    <phase>test-compile</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>

The child module has its own <plugins> section, but doesn't re-define either of these plugins (just the exec-maven-plugin).

like image 938
Graham Lea Avatar asked May 07 '11 06:05

Graham Lea


3 Answers

The Maven development team has confirmed that currently it is not possible to completely control the order of plugin execution within a phase, except by writing your own custom Lifecycle and using a custom Package Type.

I've created an enhancement request in JIRA for them to consider fixing this. Please vote for it if this is an issue for you.

It seems for the moment that the 'best practice' workaround for this specific problem (Mixed Java/Scala compilation) is to bind the scala:compile to process-resources and scala:testCompile to process-test-resources, as specified at http://scala-tools.org/mvnsites/maven-scala-plugin/example_java.html

like image 126
Graham Lea Avatar answered Sep 28 '22 05:09

Graham Lea


you can run the 2 separate goals together and maven will execute the goals in sequence:

mvn scala:compile compiler:compile
like image 45
happymeal Avatar answered Sep 28 '22 07:09

happymeal


To compile a mixed Java/Scala project, it's actually a bit more complicated than just compiling one then the other. Amongst other things, the Scala compiler is able to parse Java files in order to match up signatures for any Java classes that you're using from Scala. The compiler can essentially do the right thing for you here, but you must first tell it to do so.

Your best bet is to use SBT instead of Maven, as it'll offer many benefits beyond just getting the compilation order right.

If moving away from Maven is not an option, then follow the configuration instructions for the maven-scala-plugin here: http://scala-tools.org/mvnsites/maven-scala-plugin/example_java.html

like image 33
Kevin Wright Avatar answered Sep 28 '22 07:09

Kevin Wright