title: Maven: javac: source release 1.6 requires target release 1.6
body: <p>NOTE:  This appears to be a limit in the "javac" program.</p>
  
  <p>I have Java 6 code that needs to be built for a Java 5 JVM.  My previous work with the javac ant target (both with the JDK compiler and with ecj) led me to believe that it would simply be a matter of setting source and target for javac.  Hence this pom.xml fragment:</p>
  
  <pre><code>&lt;plugin&gt;
      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
      &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
      &lt;version&gt;2.3.2&lt;/version&gt;
      &lt;configuration&gt;
          &lt;source&gt;1.6&lt;/source&gt;
          &lt;target&gt;1.5&lt;/target&gt;
      &lt;/configuration&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>which works as expected from within Eclipse 3.7 with Maven support.  Unfortunately, running Maven directly from the command line give me</p>
  
  <pre><code>javac: source release 1.6 requires target release 1.6
  </code></pre>
  
  <p>which is the same as generated by <code>javac -source 1.6 -target 1.5</code>. To clarify, this is the official OpenJDK 6 for Ubuntu</p>
  
  <pre><code>x@JENKINS:~$ javac -version
  javac 1.6.0_20
  x@JENKINS:~$ javac -source 1.6 -target 1.5
  javac: source release 1.6 requires target release 1.6
  x@JENKINS:~$
  </code></pre>
  
  <p>The official Oracle Java 7 JDK for Windows show the same behavior.</p>
  
  <p>Note:  I do not want to build against Java 5 libraries or anything.  Just that the active javac generates Java 5 <em>compatible</em> bytecode.</p>
  
  <p>How do I get what I want while still being compatible with the Eclipse Maven plugin?  </p>
  
  <p>(EDIT: In addition to the @Override I also want to compile against the JAX-WS libraries in Java 6 when used, but still generated Java 5 byte code - I can then add the JAX-WS libraries deliberately in the web container when deploying to a Java 5 installation)</p>
  
  <hr>
  
  <p>EDIT:  It turns out that maven-compiler-plugin can be told to use another compiler, and the Eclipse compiler can do this:</p>
  
  <pre><code>        &lt;plugin&gt;
              &lt;!-- Using the eclipse compiler allows for different source and target, 
                  which is a good thing (outweighing that this is a rarely used combination, 
                  and most people use javac) This should also allow us to run maven builds 
                  on a JRE and not a JDK. --&gt;
  
              &lt;!-- Note that initial experiments with an earlier version of maven-compiler-plugin 
                  showed that the eclipse compiler bundled with that gave incorrect lines in 
                  the debug information. By using a newer version of the plexus-compiler-eclipse 
                  plugin this is hopefully less of an issue. If not we must also bundle a newer 
                  version of the eclipse compiler itself. --&gt;
  
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
              &lt;version&gt;3.0&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;source&gt;1.6&lt;/source&gt;
                  &lt;target&gt;1.5&lt;/target&gt;
                  &lt;debug&gt;true&lt;/debug&gt;
                  &lt;optimize&gt;false&lt;/optimize&gt;
                  &lt;fork&gt;true&lt;/fork&gt;
                  &lt;compilerId&gt;eclipse&lt;/compilerId&gt;
              &lt;/configuration&gt;
              &lt;dependencies&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.codehaus.plexus&lt;/groupId&gt;
                      &lt;artifactId&gt;plexus-compiler-eclipse&lt;/artifactId&gt;
                      &lt;version&gt;2.1&lt;/version&gt;
                  &lt;/dependency&gt;
              &lt;/dependencies&gt;
          &lt;/plugin&gt;
  </code></pre>
  
  <p>which compiles the class to Java 1.5 bytecode without complaints.  This is also supported "out of the box" for m2e for Eclipse Java EE 4.2.2.</p>
  
  <p>EDIT:  I found that of all things the <em>javadoc</em> tool dislikes the output from the Eclipse compiler.</p>
  
  <p>EDIT 2015-06-28:  I did a quick test recently and the latest ecj (corresponding to Eclipse 4.4) worked fine with javadoc. </p>