JAVA_HOME gets mangled by Maven

Spread the love

Question Description

I’m retrofitting bunch of existing Java projects with unified Maven build. Since each project is mature and has established Ant based build all I’m using maven-antrun-plugin to execute existing build.xml as follows:

        
            maven-antrun-plugin
            
                
                    compile
                    
                        
                            
                        
                    
                    
                        run
                    
                
            
        

When I run mvn compile build fails with this message:

[INFO] An Ant BuildException has occured: The following error occurred 
       while executing  this line:
build.xml:175: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Java\jdk1.6.0_13\jre"

What puzzles me is

  1. I have JAVA_HOME=C:\Java\jdk1.6.0_13 as part of my environment setup and when mvn.bat is executed that is exactly value I’m getting, however as you see in the error message it comes up as C:\Java\jdk1.6.0_13\jre
  2. If I run ant compile everything compiles just fine

Does it mean that perhaps maven-antrun-plugin does something like set JAVA_HOME=%JAVA_HOME%\jre? I searched my batch/build files I can’t find where that change occurs

Practice As Follows

Thats the down side of external links in an accepted answer. Codehaus shut down and thus the solution is gone. For reference here’s the content behind the link – you basically only need to copy the ... block to your antrun plugin…

The maven-antrun-plugin runs ant with JAVA_HOME set to the jre subdirectory of the JDK, even if the JAVA_HOME for the overall run is a JDK.
There is documentation elsewhere about how to create a dependency at the project level for the JDK’s tools.jar, but this does not help out antrun, which is a plugin.
The following profile does the job. The ‘..’ in the path hauls up past the ‘jre’ directory to the lib directory.


      
          tools.jar
          
            
            
              org.apache.maven.plugins
              maven-antrun-plugin
              
                
                  com.sun
                  tools
                  1.5.0
                  system
                  ${java.home}/../lib/tools.jar
                
              
            
            
          
      

Leave a Comment