Search code examples
mavenantjava-homemaven-antrun-plugin

Wrapping Ant in Maven - JAVA_HOME points to the JRE but works with just Ant


I have an Ant project that builds just fine on its own. I'm now trying to wrap it in a Maven build that will kick off the Ant build using maven-antrun-plugin. When I do this the build fails and I get this error,

[ERROR] C:\Users\bobby\workspace\libraries\build-targets\common-targets.xml:170: Unable to find a javac compiler;
[ERROR] com.sun.tools.javac.Main is not on the classpath.
[ERROR] Perhaps JAVA_HOME does not point to the JDK.
[ERROR] It is currently set to "C:\Java\jdk1.8.0_65\jre"

There are a lot of SOF posts on this error but I feel like mine is unique since it only happens when I'm wrapping the Ant build in Maven i.e., I do not get this error on the same project when I just say $ ant build.

This is part of my pom.xml file

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <configuration>
                        <tasks>
                            <ant antfile="build.xml" target="build" />
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <id>add-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attach-artifact</goal>
                    </goals>
                    <configuration>
                        <artifacts>
                            <artifact>
                                <file>build/bin/myWarFile.war</file>
                                <type>war</type>
                            </artifact>
                        </artifacts>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

My JAVA_HOME Environment Variable is set to C:\Java\jdk1.8.0_65.

The file that is mentioned in the error is from a library my work maintains where we keep all of our Jars. In that file here is what's on line 170

<target name="compile-src">
    <!-- Compile source -->
    <javac srcdir="${java.src.dir}"
        destdir="${class.dir}"
        debug="${debug.flag}"
        deprecation="${deprecation.flag}"
        nowarn="${warnings.flag}"
        optimize="off"
        source="${source.value}">
        <classpath refid="compile.classpath"/>
    </javac>
</target>

The line with source= is line 170.


Solution

  • It's a common issue. Try with this configuration:

    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        ...
        <!-- Add this dependency to your ant-run configuration -->
        <dependencies>
            <dependency>
                <groupId>com.sun</groupId>
                <artifactId>tools</artifactId>
                <version>1.5.0</version>
                <scope>system</scope>
                <systemPath>${java.home}/../lib/tools.jar</systemPath>
            </dependency>
        </dependencies>
        ...
    </plugin>
    

    Maven uses Java's system property java.home, which is not the same as the environment variable JAVA_HOME, but it is using it to compute its java.home by tacking on the jre sub-directory, as witnessed. Consequently, stuff needed by Ant is simply not available in the jre directory.

    However, this configuration ensures that Ant's plugin dependencies are correctly satisfied.