Jenkins vs SonarQube : Run with specific JDK - java

I have a project which is built and compiled with JDK 1.7 and Sonarqube 6.0 which only runs with JDK 1.8.
On Jenkins dashboard, I set Goal: :org.codehaus.mojo:sonar-maven-plugin:LATEST:sonar and on wrapper.conf on sonarqube folder, i changed wrapper.java.command=C:\Program Files\Java\jdk1.8.0_91\bin\java, Jenkins JDK is set 1.7....but Sonar doesn't work with JDK 1.8.
Please help give me advise.

I had a similar problem.
The solution was to set JDK8 to be used in the Job-Configuration in Jenkins and set JDK7 to be used for the compilation of the sources, the test-sources and surefire plugin.
Something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testSource>1.7</testSource>
<testTarget>1.7</testTarget>
<verbose>true</verbose>
<fork>true</fork>
<executable>C:\java\jdk1.7.0_25\bin\javac</executable>
<compilerVersion>1.7</compilerVersion>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<fork>true</fork>
<executable>C:\java\jdk1.7.0_25\bin\javac</executable>
<source>1.5</source>
<target>1.5</target>
<compilerVersion>1.7</compilerVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<jvm>C:\java\jdk1.7.0_25\bin\java</jvm>
<forkMode>once</forkMode>
</configuration>
</plugin>
If it works for you, you can set the Path to the JDK in settings.xml and use that setting in you pom.xml so each environment/developer can use their own JDK.
<profile>
<id>jdk7</id>
<properties>
<JDK_1_7_HOME>C:\java\jdk1.7.0_25</JDK_1_7_HOME>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
And your pom.xml
...
<executable>${JDK_1_7_HOME}/bin/javac</executable>
...

Related

Maven How to show warnings of codes when compiling

I'm using maven to build a new project. There are some warnings in my codes which are underlined by yellow line. I wish maven could report these warnings in console. How can I accomplish that? Can I just add some parameters in the command such as mvn -XXX clean compile?
<showDeprecation>
Sets whether to show source locations where deprecated APIs are used.
Default value is: false
User property is: maven.compiler.showDeprecation
<showWarnings>
Set to true to show compilation warnings.
Default value is: false
User property is: maven.compiler.showWarnings
-- Maven Doku
As simple as mvn clean install -Dmaven.compiler.showDeprecation=true -Dmaven.compiler.showWarnings=true all in one line.
You could even remove the =true part because, when you add a maven parameter with -D it automatically sets its value to true, if not set to something else.
With maven 3.3.9, using the maven-compiler-plugin and Java 1.8, you need a <configuration> section like the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
I got this answer from here when none of the above worked for me: http://frequal.com/java/EnableWarningsInMaven.html
I am guessing you are using the compiler plugin. Have you tried this?
Use the maven compiler plugin in your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-Xlint:all</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
To get to see detailed logs in maven you can use the command line option
-X, --debug Produce execution debug output
e.g this can be used as
mvn clean install -X
Also if what you want to get in the logs is the compiler warnings (The value of the local variable a is not used). You can try modifying your project's pom.xml with the following -
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
... other plugins
</plugins>
</build>

How to set custom version of jdk in maven?

I am new in Maven.
I work in Windows and when I try to do the next instructions mvn clean install in folder with pom.xml file it throw me errors like this:
CLASS_NAME.java error: diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)
As I can understand from the message it occurs because maven use jdk version 1.5 (Actually I even didn't install it). In maven settings folder I found toolchains.xml file. If I understood right, it is possible to set custom version of jdk for user using this file. So I add this code to my pom.xml file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<configuration>
<toolchains>
<jdk>
<version>[1.8]</version>
</jdk>
</toolchains>
</configuration>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
</plugin>
and this to toolchains.xml
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>C:/Program Files/Java/jdk1.8.0_45</jdkHome>
</configuration>
</toolchain>
If somebody know can you tell me how to fix this? I will appreciate any help, idea or explanation.
P.S. JAVA_HOME is C:\Program Files\Java\jdk1.8.0_45
Try configuring the maven-compiler-plugin instead of the maven-toolchains-plugin in your pom.xml.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Source

Android project build with native libraries inside using maven

I have an Android project with Maven that uses internal libraries. When I run a maven install the so library is not inside the jar file generated and in consequence is not inside the apk generated. I'm using Eclipse with maven and android maven plugin v.3.7.0.
My pom.xml is:
<build>
<sourceDirectory>src</sourceDirectory>
<finalName>../bin/${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<nativeLibrariesDirectory>${project.basedir}/libs</nativeLibrariesDirectory>
<sdk>
<path>${env.ANDROID_HOME}</path>
<platform>15</platform>
</sdk>
</configuration>
<extensions>true</extensions>
</plugin>
<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>
It seems that nativeLibrariesDirectory tag doesn't works for me.
Any idea to fix this problem?
Thanks
Here is how to include native libraries in your apk:
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>com.simpligility.maven.plugins</groupId>
<artifactId>android-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<manifest>
<debuggable>true</debuggable>
</manifest>
<!-- Where the native files are located; in the future we should use src/main/libs -->
<nativeLibrariesDirectory>${project.basedir}/libs</nativeLibrariesDirectory>
</configuration>
<executions>
<execution>
<id>manifestUpdate</id>
<phase>process-resources</phase>
<goals>
<goal>manifest-update</goal>
</goals>
</execution>
<execution>
<id>alignApk</id>
<phase>package</phase>
<goals>
<goal>zipalign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The Android for Maven Eclipse project doesn't yet support native libraries. If you'd like to see this supported please send a pull request.

Maven WebApp tomcat7 system properties

We are using a maven dependency to add embedded tomcat on our webapplication. It works fine, but I need to add systemProperties to embedded tomcat, so that our webapp can use this systemProperties.
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<path>/html5</path>
<enableNaming>true</enableNaming>
<finalName>html5.jar</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I tried to add system properties like this, but it didn't work. I added it
<build>
<plugins>
<plugin>
<configuration>
<systemProperties>
<dashboard.oracle.host>1.1.1.1</dashboard.oracle.host>
<dashboard.oracle.port>1521</dashboard.oracle.port>
<dashboard.oracle.sid>orcl</dashboard.oracle.sid>
<dashboard.oracle.url>
jdbc:oracle:thin:#${dashboard.oracle.host}:${dashboard.oracle.port}:${dashboard.oracle.sid}
</dashboard.oracle.url>
<dashboard.oracle.username>username</dashboard.oracle.username>
<dashboard.oracle.password>password</dashboard.oracle.password>
</systemProperties>
</configuration>
...
</plugin>
</plugins>
</build>
In general the way you have added the system properties to the tomcat plugin is correct:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<systemProperties>
<example.value.1>alpha</example.value.1>
<example.value.2>beta</example.value.2>
</systemProperties>
</configuration>
</plugin>
Taken from the Apache Docu.
the system properties in the Maven Plugin are only for when running the tomcat7:run mojo ... in order to pass in the system properties to the executable war (jar), you must do it on the command line:
java -DsysProp1=value -DsysProp2=value -jar exec-war.jar

Maven project dependency against JDK version

I have projects that need to be build with a specific version of the JDK.
The problem isn't in the source and target parameters but in the jars of the runtime used during compilation.
In some cases I get a compilation error if I try to compile with the wrong JDK, but sometimes the build is successful and I get runtime errors when using the jars.
For example in eclipse I have the ability to establish the execution enviroment for the project in the .classpath file.
Is there a way to handle such situation in maven?
What I would like to have is the ability to handle JRE dependency like other dependencies of the project in the POM file.
UPDATE:
The accepted solution was the best one when I asked this question, so I won't change it. Meanwhile a new solution to this kind of problems has been introduced: Maven Toolchain. Follow the link for further details.
I've found this article:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_1_4_HOME}/bin/javac</executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
I have projects that need to be build with a specific version of the JDK.
You can use the Maven Enforcer plugin to enforce the use of a particular version of the JDK:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>1.5</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
But I'm not sure I really understood the question. If this is not what you want, maybe you could declare your JDK specific dependencies in profiles and use an activation trigger based on the JDK version. For example:
<profiles>
<profile>
<activation>
<jdk>1.5</jdk>
</activation>
...
</profile>
</profiles>
This configuration will trigger the profile when the JDK's version starts with "1.5".
I believe that this can be solved with following plugin in your pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Here you target version 1.6 , or write your own version

Categories