I can only use java 1.6 for my project. So maven version also has to be 2.2.1 . Cannot use maven 3.0. But while building I'm getting following error :
Error resolving version for 'org.apache.maven.plugins:maven-jar-plugin':Plugin requires Maven version 3.0
this is the pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
what are the version that we can use?
Changes the source and target to 1.6. If your code does not make use of java8 features then it should work just fine. This will generate a jar that is compatible with any java6 codebase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Related
I have created new maven java project in eclipse by clicking File->New->Other->Maven->New Project. I found that projects is using java 1.5. In my PC exist only java 1.4 and java 8. I need to compile project using java 1.4 JDK. I go to Project->Properties-> JRE System Library and change to java 1.4. When I run main class I have error:
java.lang.UnsupportedClassVersionError: arr/ff (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
How to make project java 1.4 compatible?
First, I'd define a property to control the value. Something like,
<properties>
<java.version>1.4</java.version>
</properties>
And then add a build stanza, like
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<debug>false</debug>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
Specify source and target Java version by this way:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.5</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Read offical document.
Read about Java version compatibility.
Usually IDEs use maven pom.xml file as a source of project configuration. Changing the compiler settings in the IDE not always has effect on the maven build. The best way to keep a project always manageable with maven is edit the pom.xml files and instruct the IDE to sync with maven.
Configure the maven compiler plugin in the pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
...
OR Set these properties (always in the pom)
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.4</maven.compiler.target>
</properties>
When I was building my project like :-
mvn clean install -DskipTests
, then it was giving some error.
After that, I just added, -Djdk.version=1.8, then it works fine.
Can someone tell what is the reason for this?
Try something like this in your pom:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
....
</build>
It may be because you might be missing the maven-compiler-plugin.
Try the following:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
....
</build>
From Maven 3, it defaults to JDK 1.5. So if you do not include the version, it will take JDK 1.5 as default compiler version.
Since, you mentioned that you used JDK 1.8, and the error disappeared. So if you had maven-compiler-version defined in the pom.xml, the error might be because the version would had not been defined.
So by default, it pointed to JDK 1.5 and it was trying to compile the code which would be defined for JDK 1.8 and not for JDK 1.5.
So, it is better to define the correct java version in the pom.xml
Why to specify java version in maven build?
Because Maven won't try to guess the Java version your project was created with (the one you probably only configured in your IDE).
You need to specify the earliest version supported in your pom.xml with:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
I am using Eclipse as IDE. When I right click on the project, then click "Maven Update", my Java version changes to 1.5. Here is what I did so far:
I changed "Java build path" to "workspace default jre 1.8.0_25"
Then changed "java compiler" to 1.8
Then changed "project facets">java>1.8
Lastly Changed pom.xml java version to 1.8
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.1.3.v20140225</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugin</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
How can I keep it to 1.8 version?
There's a typo in the groupId of the maven-compiler-plugin in your pom.xml file: it should be org.apache.maven.plugins.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Note that the groupId actually defaults to org.apache.maven.plugins so you could also omit it.
By default, the JRE used by m2e is 1.5, see this question : Eclipse JRE System Library [J2SE-1.5]
I have a source code written for java 1.7. I would like to compile it for java 1.6. If I understood correctly, I need to use the options -source 1.7 -target 1.6
I am using Maven2 and Netbeans (8.0). So, I tried :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.6</target>
<compilerArguments>
<bootclasspath>/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/rt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
But I get a javacTask: source release 1.7 requires target release 1.7
I read that some people use eclipse compiler to make it work, but how can I do this if I'm using Netbeans ?
Thanks a lot for your help
EDIT Sorry it's the other way around. You can compile source 1.6 and target 1.7 but you can't compile it when source version > target version because then it couldn't handle new features of the language.
For more detail about this see Cross-Compilation Options in javac documentation http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html.
If you would have 1.6 sources you could compile them for target version 1.7 like this:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>>
I have two JDK - 1.5 and 1.6.
Default JDK is 1.6 (JAVA_HOME sets to JDK 1.6)
and have env. variable JAVA_HOME_1_5 sets to JDK 1.5
I want compile maven project with JDK 1.5, so I added to pom.xml next:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<executable>${env.JAVA_HOME_1_5}/bin/javac.exe</executable>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<optimize>true</optimize>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
But maven is compiling my project with JDK 1.6!
What is wrong?
Thanks in advance!
%> JAVA_HOME=$JAVA_HOME_1_5 mvn install
Maven by default use JAVA_HOME so you can set it to jdk_1.5 just for it.
For pom way, you should add this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<jvm>${JAVA_HOME_1_5}/bin/java</jvm>
<forkMode>once</forkMode>
</configuration>
</plugin>
Could it be because, as documented here, you are missing the <compilerVersion>1.5</compilerVersion> in your configuration?
can you try this. It works for me.
http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html