Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: javac: source release 1.6 requires target release 1.6

Tags:

java

maven

javac

NOTE: This appears to be a limit in the "javac" program.

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:

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-compiler-plugin</artifactId>     <version>2.3.2</version>     <configuration>         <source>1.6</source>         <target>1.5</target>     </configuration> </plugin> 

which works as expected from within Eclipse 3.7 with Maven support. Unfortunately, running Maven directly from the command line give me

javac: source release 1.6 requires target release 1.6 

which is the same as generated by javac -source 1.6 -target 1.5. To clarify, this is the official OpenJDK 6 for Ubuntu

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:~$ 

The official Oracle Java 7 JDK for Windows show the same behavior.

Note: I do not want to build against Java 5 libraries or anything. Just that the active javac generates Java 5 compatible bytecode.

How do I get what I want while still being compatible with the Eclipse Maven plugin?

(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)


EDIT: It turns out that maven-compiler-plugin can be told to use another compiler, and the Eclipse compiler can do this:

        <plugin>             <!-- 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. -->              <!-- 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. -->              <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-compiler-plugin</artifactId>             <version>3.0</version>             <configuration>                 <source>1.6</source>                 <target>1.5</target>                 <debug>true</debug>                 <optimize>false</optimize>                 <fork>true</fork>                 <compilerId>eclipse</compilerId>             </configuration>             <dependencies>                 <dependency>                     <groupId>org.codehaus.plexus</groupId>                     <artifactId>plexus-compiler-eclipse</artifactId>                     <version>2.1</version>                 </dependency>             </dependencies>         </plugin> 

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.

EDIT: I found that of all things the javadoc tool dislikes the output from the Eclipse compiler.

EDIT 2015-06-28: I did a quick test recently and the latest ecj (corresponding to Eclipse 4.4) worked fine with javadoc.

like image 845
Thorbjørn Ravn Andersen Avatar asked Jan 23 '12 11:01

Thorbjørn Ravn Andersen


People also ask

How do I change target release in Intellij?

To fix this, open up . idea/compiler. xml and change the “target” parameter to the release that you're targeting. Your build should now compile just fine.

What is Maven compiler source?

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax. tools. JavaCompiler (if you are using java 1.6) and is used to compile Java sources.

What is Maven compiler version?

Apache Maven Compiler Plugin/ compiler:compile. | Last Published: 2022-03-08. Version: 3.10.1.

What is Maven compiler plugin in POM XML?

The compiler plugin is used to compile the source code of a Maven project. This plugin has two goals, which are already bound to specific phases of the default lifecycle: compile – compile main source files. testCompile – compile test source files.


2 Answers

The limitation is in javac. The solution is to tell maven to use another compiler. See question for details.

like image 126
Thorbjørn Ravn Andersen Avatar answered Sep 18 '22 03:09

Thorbjørn Ravn Andersen


It seems if you want to do cross compilation you need to supply a couple of extra arguments -bootclasspath and -extdirs, although I believe you only need the first. For using Javac and example can be found here with an explanation of the additional options here (Cross-Compilation Options section).

You would then need to configure these options for your maven-compiler-plugin. From what I understand you need to set to plugin to fork so that it will use the compiler arguments rather than the built in compiler. You can find a listing of all the options here

 <build>         <plugins>             <plugin>                 <artifactId>maven-compiler-plugin</artifactId>                 <configuration>                     <source>1.6</source>                     <target>1.5</target>                     <fork>true</fork>                     <compilerArguments>                         <bootclasspath>${1.5.0jdk}\lib\rt.jar</bootclasspath>                    </compilerArguments>                </configuration>            </plugin>         ....        </plugins>    </build> 
like image 35
Kingamajick Avatar answered Sep 17 '22 03:09

Kingamajick