mvn clean install using java 1.5 or 1.6 - maven

When I do mvn clean install, I get this error:
annotations are not supported in -source 1.3
(try -source 1.5 to enable annotations)
But where do I put this -source 1.5 command? I tried all permutations with mvn clean install and couldn't get it to work. So I tried putting compilation in my pom, like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
But that didn't work either.. What am I missing?
Thanks!

The latest version of the compiler plugin is 2.3.2. This version seems to support 1.6 as both version and target parameter.

Instead of configuring the compiler plugin, which requires supplying a version, you can
set maven.compiler.source and maven.compiler.target properties in your pom.xml:
<project>
....
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
</properties>

Related

Specify maven install plugin version

I am using maven to build a project which requires java 1.6. But the building process uses maven-install-pluging version 3.0.0-M1 which is not compatible with java 1.6. I have not configured maven-install-plugin version anywhere in the project. How do I say to use maven-install-pluging version 2.4. I use maven 3.2.2 version
You can set the plugin version by defining it in pluginManagement section :
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</pluginManagement>
...
</build>

Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin: 3.2:sonar

Can anyone help me in getting solution for the below error.
Below are the version of the components to configure
SonarQube 5.1.2
Soanr-Runner 2.4
Java 1.7 [ I have to use 1.7 only since my code supports only 1.7]
mavn 3.3.9
sonar-cobertura-plugin-1.6.3
sonar-findbugs-plugin-3.3
cobertura 2.6
Execution command
mvn -fn -e org.sonarsource.scanner.maven:sonar-maven-plugin:RELEASE:sonar -Dsonar.jdbc.url="jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance" -Dsonar.host.url=http://localhost:9000 -DskipTests
In Console Window I am getting error
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:
3.2:sonar (default-cli) on project NWT_Core: Execution default-cli of goal org.s
onarsource.scanner.maven:sonar-maven-plugin:3.2:sonar failed: Unable to load the
mojo 'sonar' in the plugin 'org.sonarsource.scanner.maven:sonar-maven-plugin:3.
2' due to an API incompatibility: org.codehaus.plexus.component.repository.excep
tion.ComponentLookupException: org/sonarsource/scanner/maven/SonarQubeMojo : Unsupported major.minor version 52.0
Since the 3.2, the SonarQube maven plugin requires Java 8.
You have to use the 3.0.2 version for Java 7.
You have to explicitely add this statement to your pom :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.0.2</version>
</plugin>
Because if you do not do so, by default this plugin uses the LATEST version of the plugin (3.2), hence your error.
See http://docs.sonarqube.org/display/HOME/Frequently+Asked+Questions#FrequentlyAskedQuestions-ShouldIlockversionofSonarQubeMavenplugininmypom.xml?
Regardless of what you compile your code with, the SonarQube analysis should be run with a specific Java version.
You simply need to use different JDK versions for the compilation and analysis.
For SonarQube 6.* compatibility], make sure the JAVA_HOME=/path/to/java8
For SonarQube 9.* compatibility], make sure the JAVA_HOME=/path/to/java11
In my case I had a parent pom with
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.5</version>
</plugin>
</plugins>
</pluginManagement>
</build>
I've added my own version also within pluginManagement in my child pom but this didn't work I had to add the plugin to the <build><plugins> nodes instead of <build><pluginManagement><plugins>. Only then new newer version had been used.
Maybe this helps someone.
Recently, install Sonorqube.5.12 image in docker and push the project into Sonorqube. Initially we were facing some maven console errors like major.minor version 52.0.
Later, has been fixed by below step's for me.
Add this plugs in maven.
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
Next, Add default DB setup in ~/.m2/settings.xml file
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>jdbc:h2:tcp://localhost:9092/sonar</sonar.jdbc.url>
<sonar.host.url>http://localhost:9000</sonar.host.url>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
<sonar.pdf.username>admin</sonar.pdf.username>
<sonar.pdf.password>admin</sonar.pdf.password>
</properties>
</profile>
It worked for me after using Maven->update project , mvn clean install, mvn clean compile

How can I configure Maven to use JDK6 by default, but JDK7 as needed

I have both JDK 1.6 and 1.7 installed on my system (Linux, under the /opt directory).
I've added the bin directory for JDK 1.6 in my path, so that's the version of Java used by default.
I'm working on a project that requires JDK 1.7 and some that require 1.6. Previously I had set JDK 1.7 settings within Eclipse, but I wanted to convert this project to Maven so that everyone could use their preferred editor.
Is it possible to specify the 1.7 location on a Maven installation/configuration (and not in the POM file) such that it uses 1.6 by default and 1.7 when specifying the project requires it in the POM? As far as I am aware, everyone working on a project should have the same contents in their POM files, so I am reluctant to set the location of the Java 7 directory here (as it'll be different on everyone's machines).
$ mvn --version
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 13:51:28+0000)
Maven home: /opt/apache-maven-3.0.5
Java version: 1.6.0_45, vendor: Sun Microsystems Inc.
Java home: /opt/jdk1.6.0_45/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "linux", version: "3.8.0-27-generic", arch: "amd64", family: "unix"
Adding the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${project.build.outputDirectory}/resources</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
results in:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project golemlite: Fatal error compiling: invalid target release: 1.7 -> [Help 1]
Take a look here. You can set a property in your settings.xml and use it in the pom. Be aware that everybody would have to define that property.
As for your example:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<fork>true</fork>
<executable>${JAVA_1_7_HOME}/bin/javac</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${project.build.outputDirectory}/resources</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
and in the settings:
<settings>
[...]
<profiles>
[...]
<profile>
<id>compiler</id>
<properties>
<JAVA_1_7_HOME>/path/to/jdk7</JAVA_1_7_HOME>
</properties>
</profile>
</profiles>
[...]
<activeProfiles>
<activeProfile>compiler</activeProfile>
</activeProfiles>
</settings>
You can place a file named mvn.sh near your pom.xml in which you set JAVA_HOME to anything you want. Then, build project like ./mvn clean install. And be sure not to check it to VCS.
Maven uses whatever version is in your JAVA_HOME system variable. So I like to add to my .bash_profile functions to swap between them just in my current shell, so you could leave Java 6 by default but allow yourself to trivially switch to Java 7 or even 8 when you need to build a newer project.
Gist to Change Java Versions on Demand
If your terminal is not bash then you may need to tweak this solution.
Personally I prefer this to setting it in your settings.xml even though it is slightly more work as it allows you to use the same solution across all build tools and even just for running jars built targeting other versions of Java.
So a solution for you might be adding something like this to your .bash_profile (clearly tweaking the paths appropriately to where ever the Java versions are installed on your machine):
function java6
{
JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0_65-b14-462.jdk/Contents/Home
export JAVA_HOME
}
function java7
{
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home
export JAVA_HOME
}

Where is the JDK version to be used by Maven compiler specified?

When I do not define something as follows in my pom.xml file, where on my system is it defined for Maven which version of Java JDK to use while compiling (I have several versions installed on my system, JAVA_HOME points to one of them)?
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
Maven doc says
The Compiler Plugin is used to compile the sources of your project. The default compiler is javac and is used to compile Java sources. Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.
ref: http://maven.apache.org/plugins/maven-compiler-plugin/index.html
There is this interesting thread on Maven's Jira Change default source level to 1.5
EDIT:
Update for Maven 3.0 and later:
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. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.
Source: http://maven.apache.org/plugins/maven-compiler-plugin/index.html
Thanks nachteil for pointing it out.
simply use properties
<properties>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.test.skip>true</maven.test.skip>
</properties>
From maven compiler plugin doucemntation:
Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.
I found this post via search engine and I think it is worth updating.
Also: the -target and -source options do not affect the compiler itself, but rather the way it handles source code and produces output byte code.
You must define a property in your maven setting.xml file. The property is your second javac path.(D:\dev\java\ibm\java1.6.0\bin\javac)After use this property for maven-compiler-plugin in your pom file.
setting.xml
<settings>
<profiles>
<profile>
<id>IBM_JAVA</id>
<properties>
<IBM_JAVA_1_6_JAVAC>D:\dev\java\ibm\java1.6.0\bin\javac</IBM_JAVA_1_6_JAVAC>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>IBM_JAVA</activeProfile>
</activeProfiles>
</settings>
pom.xml
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<executable>${IBM_JAVA_1_6_JAVAC}</executable>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>

Maven Install: "Annotations are not supported in -source 1.3"

When running mvn install on my project, i see it fail due to the following errors:
C:\Repositories\blah\src\test\java\com\xxx\qm\testrunner\test\ATest.java:[11,5] annotations are not supported in -source 1.3
(use -source 5 or higher to enable annotations)
#Test
C:\Repositories\blah\src\test\java\com\xxx\qm\common\test\BTest.java:[11,5] annotations are not supported in -source 1.3
(use -source 5 or higher to enable annotations)
#Test
My Maven dependency includes jUnit 4.8, however and has no reference to 1.3 anything.
What would cause these errors? Please advise
You need to specify the source version of your maven project through the use of the maven-compiler-plugin. Add the following to your pom build element and set the appropriate java source and target levels.
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
http://maven.apache.org/plugins/maven-compiler-plugin/
A shorter version:
<project>
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
</properties>
....
You are most likely using OpenJDK where the source level is 1.3 when not explicitly set - as opposed to Oracle JDK where the source level is 1.5.
Since most modern Java projects target newer code than Java 5 you most likely need to set this anyway.
Also note that if you need a lower target than source (e.g. for compiling with Java 6 but deploying to Java 5) you can do this by using the Eclipse compiler instead of Javac.
Bu default, the maven tries to compile using Java 1.3 version. I hope most of them hit this error because of missing to tell maven that "Hey Maven, Dont use 1.3 and use "whatever_version_I_give"
This can be mentioned in pom.xml as below :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
In my above example, I have used 1.7. Please replace with whatever version you wish to.
Add this in your pom
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<!-- put your configurations here -->
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

Resources