1

We have a project which contains a lot of unchecked and rawtypes codes. For years, we used Eclipse which (I don't know how?) could compile and build a war file from it, but now, we should use jenkins which uses maven which could not skip those codes to success as Eclipse does.

I created a very simple example which describes my issue:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cloudbees</groupId>
  <artifactId>traceability</artifactId>
  <packaging>war</packaging>
  <version>1.1-SNAPSHOT</version>
  <name>traceability Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>traceability</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <compilerArgs>
          <compilerArg>-Xlint</compilerArg>
          </compilerArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

MyGenericClass.java

package com.cloudbees.traceability;

public class MyGenericClass<T> implements MyGenericInterface<T>{
@SuppressWarnings("unchecked")
@Override
public T myGenericMethod() {
    // TODO Auto-generated method stub
    return (T)"String";
}
}

MyClass.java (an example of unchecked and rawtypes codes)

package com.cloudbees.traceability;

public class MyClass {
public <T> T myGenericMethod(MyGenericInterface<T> mgi){
    return mgi.myGenericMethod();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String main(){
    MyClass mc = new MyClass();
    MyGenericClass mgc=new MyGenericClass();
    return mc.myGenericMethod(mgc);
}
}

index.jsp (checks if Eclipse successfully has compiled into war file)

<%@page import="com.cloudbees.traceability.MyClass"%>
<html>
<body>
<h2>Hello World!</h2>
<% MyClass mc=new MyClass(); %>
<%=mc.main() %>
</body>
</html>

The output of Debug on server on Eclipse:

Hello World!
String 

The output of mvn clean compile:

Dynamic source lookup support loaded.
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building traceability Maven Webapp 1.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ traceability ---
[INFO] Deleting C:\Users\user\workspace\traceability-java-sample\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ traceability ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\user\workspace\traceability-java-sample\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ traceability ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 3 source files to C:\Users\user\workspace\traceability-java-sample\target\classes
[INFO] -------------------------------------------------------------
[WARNING] COMPILATION WARNING : 
[INFO] -------------------------------------------------------------
[WARNING] /C:/Users/user/workspace/traceability-java-sample/src/main/java/com/cloudbees/traceability/MyClass.java:[11,35] unchecked conversion
  required: src.main.java.com.cloudbees.traceability.MyGenericInterface<T>
  found:    src.main.java.com.cloudbees.traceability.MyGenericClass
[WARNING] /C:/Users/user/workspace/traceability-java-sample/src/main/java/com/cloudbees/traceability/MyClass.java:[11,34] unchecked method invocation: method myGenericMethod in class src.main.java.com.cloudbees.traceability.MyClass is applied to given types
  required: src.main.java.com.cloudbees.traceability.MyGenericInterface<T>
  found: src.main.java.com.cloudbees.traceability.MyGenericClass
[WARNING] /C:/Users/user/workspace/traceability-java-sample/src/main/java/com/cloudbees/traceability/MyGenericClass.java:[7,19] unchecked cast
  required: T
  found:    java.lang.String
[INFO] 3 warnings 
[INFO] -------------------------------------------------------------
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/user/workspace/traceability-java-sample/src/main/java/com/cloudbees/traceability/MyClass.java:[11,34] incompatible types
  required: java.lang.String
  found:    java.lang.Object
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.558 s
[INFO] Finished at: 2017-08-02T18:03:45+04:30
[INFO] Final Memory: 12M/222M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project traceability: Compilation failure
[ERROR] /C:/Users/user/workspace/traceability-java-sample/src/main/java/com/cloudbees/traceability/MyClass.java:[11,34] incompatible types
[ERROR] required: java.lang.String
[ERROR] found:    java.lang.Object
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

As you see, warning #2 has been raised as error in few lines later. I would like to ask maven to skip and ignore such codes as Eclipse can.

Yasser Zamani
  • 2,380
  • 21
  • 18
  • 2
    Show the code of the Class `MyClass.java` than we may help you – Jens Aug 02 '17 at 13:48
  • @Jens, thanks a lot. I know how to fix `MyClass.java` but that is just an example. We have a project which contains a lot of `unchecked` and `rawtypes` codes. So far, we used eclipse which (I don't know how?) could compile and build a war file from it, but now, we should use jenkins which uses maven which could not! So my question is, while I used `Xlint` compiler argument, why maven does not ignore `unchecked` and `rawtypes` codes?! Actualy it ignores at first step and presents them as warning but then immediately fails with error?! – Yasser Zamani Aug 02 '17 at 18:44
  • It ignores it. That are only warnings. – Jens Aug 02 '17 at 18:46
  • @Jens, please see warning #2. It has raised as an error a few lines later! – Yasser Zamani Aug 02 '17 at 18:49
  • Did you try to remove the `-Xlint`? – Ortomala Lokni Aug 02 '17 at 20:23
  • @OrtomalaLokni, hmm..., I got same result but without those warnings! So `-Xlint` is not about skipping such codes :( I updated my post title, could you please take a look? – Yasser Zamani Aug 03 '17 at 04:41
  • You can not ignore code if you get a warning. Clean your eclipse Project, rebuild it and you will see, that you get the same error in eclipse – Jens Aug 03 '17 at 05:25
  • @Jens, I updated my post. please take a look. `debug on server` of Eclipse successfully compiles, deploys and displays the index.jsp which runs those codes which maven could not! – Yasser Zamani Aug 03 '17 at 06:45
  • @Jens, sorry for bothering you, as I commented above, I find out it's a bug in eclipse :O – Yasser Zamani Aug 03 '17 at 09:21

0 Answers0