我们有一个包含大量unchecked
和rawtypes
代码的项目。多年来,我们使用Eclipse(我不知道如何?)可以从中编译和构建war文件,但是现在,我们应该使用jenkins,它使用maven,它不能像Eclipse那样跳过这些代码成功。
我创建了一个非常简单的例子来描述我的问题:
的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(unchecked
和rawtypes
代码的示例)
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(检查Eclipse是否已成功编译成war文件)
<%@page import="com.cloudbees.traceability.MyClass"%>
<html>
<body>
<h2>Hello World!</h2>
<% MyClass mc=new MyClass(); %>
<%=mc.main() %>
</body>
</html>
Eclipse上Debug on server
的输出:
Hello World!
String
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
如您所见,警告#2在以后的几行中被引发为错误。我想请求maven跳过并忽略Eclipse这样的代码。