What is the difference between maven compiler plugin and maven toolchains plugin?

2k views Asked by At

I had to integrate some legacy code into my maven build, so I used the maven-recommended toolchains plugin to change the java version:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-toolchains-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>toolchain</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <toolchains>
            <jdk>
                <version>1.5</version>
            </jdk>
        </toolchains>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.10.0</version>
    <configuration>
        <compilerArgs>
            <arg>-Xmaxerrs</arg>
            <arg>1000</arg>
        </compilerArgs>
    </configuration>
</plugin>

Then I ran into the max 100 compile errors problem which required passing special options to javac and found I was able to do it just with maven compiler:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.10.0</version>
    <configuration>
        <fork>true</fork>
        <compilerVersion>1.5</compilerVersion>
        <executable>C:\Java\jdk-1.5.0_22\bin\javac.exe</executable>
        <source>1.5</source>
        <target>1.5</target>
        <compilerArgs>
            <arg>-Xmaxerrs</arg>
            <arg>1000</arg>
        </compilerArgs>
    </configuration>
</plugin>

Both snippets produce the same result: the java compiler is changed from the maven default to java 1.5. Both run in about the same amount of time, so there's no visible performance difference. I'd like to know if there are any benefits of one over the other so I know when to use each.

1

There are 1 answers

0
Stephen C On BEST ANSWER

They do different things:

  • The compiler plugin specifically configures how your Java code is compiled (and only that).
  • The toolchains plugin just ensures that other plugins are all using the same Java tool chain (i.e. the same JDK) to compile, run, test, generate javadocs and so on.

This is explained in the respective plugins' documentation.

Note that not all plugins are "tool chain aware", but the compiler plugin is.

... are any benefits of one over the other

Well there there are things you can do with one and not the other and vice versa. For example, you can't set Java compiler options using the toolchain plugin.

However, they are not mutually exclusive. You can use both in the same POM file.