Gradle构建Java使用了Sun的专用API异常解决
1. 项目信息
项目为Struts2+Spring+iBatis架构,JDK1.6,多处使用了Sun的专用API的老项目,要进行持续集成重构。
2. 构建脚本
- 第一版脚本如下
import org.gradle.plugins.ide.eclipse.model.Facet
plugins {
id "org.sonarqube" version "2.0.1"
}
apply plugin: 'java'
apply plugin: "jacoco"
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
webAppDirName = 'WebRoot'
repositories {
mavenCentral();
flatDir {
dirs 'WebRoot/WEB-INF/lib'
}
}
dependencies {
compile fileTree(dir: 'WebRoot/WEB-INF/lib', include: ['*.jar'])
testCompile fileTree(dir: 'WebRoot/WEB-INF/lib', include: ['*.jar'])
}
compileJava.options*.compilerArgs = [
"-Xlint:serial", "-Xlint:varargs", "-Xlint:cast", "-Xlint:classfile",
"-Xlint:dep-ann", "-Xlint:divzero", "-Xlint:empty", "-Xlint:finally",
"-Xlint:overrides", "-Xlint:path", "-Xlint:processing", "-Xlint:static",
"-Xlint:try", "-Xlint:fallthrough", "-Xlint:rawtypes", "-Xlint:deprecation",
"-Xlint:unchecked", "-Xlint:-options", "-Werror","-XDignore.symbol.file","-Xdiags:verbose"
]
compileTestJava.options*.compilerArgs = [
"-Xlint:serial", "-Xlint:-varargs", "-Xlint:cast", "-Xlint:classfile",
"-Xlint:dep-ann", "-Xlint:divzero", "-Xlint:empty", "-Xlint:finally",
"-Xlint:overrides", "-Xlint:path", "-Xlint:processing", "-Xlint:static",
"-Xlint:try", "-Xlint:-fallthrough", "-Xlint:-rawtypes", "-Xlint:-deprecation",
"-Xlint:-unchecked", "-Xlint:-options","-XDignore.symbol.file","-Xdiags:verbose"
]
compileJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
compileTestJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
sourceSets{
main{
java.srcDirs = ['src']
resources.srcDirs = ['src']
}
test{
java.srcDirs = ['test/utest']
resources.srcDirs = ['test/utest']
}
}
jacoco {
toolVersion = "0.7.7.201606060606"
}
test {
include '**/UTest*.class'
}
sonarqube {
properties {
property "sonar.host.url", "http://192.168.1.18:19000"
property "sonar.login", "admin"
property "sonar.password", "admin123"
property "sonar.projectKey", "tax_settle"
property "sonar.projectName", "tax_settle"
property "sonar.projectVersion", "39.17.0"
property "sonar.sources", ["src","WebRoot"]
property "sonar.exclusions", ["WebRoot/download","WebRoot/images","WebRoot/model","WebRoot/META-INF","WebRoot/rrshFile","WebRoot/WEB-INF/lib","WebRoot/WEB-INF/classes","coverage-report","builder"]
property "sonar.sourceEncoding", "UTF-8"
}
}
eclipse {
wtp {
component {
contextPath = "/tax_settle"
deployName = "tax_settle"
resource sourcePath: 'src/', deployPath: "/WEB-INF/classes"
resource sourcePath: 'src/', deployPath: "/WEB-INF/classes"
resource sourcePath: 'WebRoot/', deployPath: "/"
}
facet {
facet name: 'jst.web', type: Facet.FacetType.fixed
facet name: 'wst.jsdt.web', type: Facet.FacetType.fixed
facet name: 'jst.java', type: Facet.FacetType.fixed
facet name: 'jst.web', version: '3.0'
facet name: 'jst.java', version: '1.6'
facet name: 'wst.jsdt.web', version: '1.0'
}
}
}
3. 解决过程
- 执行上面的脚本报如下异常:
错误: 程序包com.sun.image.codec.jpeg不存在 import com.sun.image.codec.jpeg.JPEGImageEncoder; 错误: FtpClient是抽象的; 无法实例化 ftpClient = new FtpClient()
- 各种百度,有说没有引入依赖,于是把JDK加入依赖,执行脚本,依然报如下错误:
错误: FtpClient是抽象的; 无法实例化 ftpClient = new FtpClient();
- 各种百度,发现JDK7和JDK8中FtpClient的用法和JDK6的已经不一样了,于是查看系统环境变量发现是JDK8。 然后让Gradle使用指定的JDK编译,在用户目录下.gradle目录中新建文件gradle.properties内容如下:
org.gradle.java.home=D:\Program Files (x86)\Java\jdk1.6.0_10
- 再次运行脚本,我擦,还错,如下:
FAILURE: Build failed with an exception.
*What went wrong: java.lang.UnsupportedClassVersionError: org/sonarqube/gradle/SonarQubePlugin : Unsupported major.minor version 51.0 >org/sonarqube/gradle/SonarQubePlugin : Unsupported major.minor version 51.0
- 奥~,原来是sonarqube scanner for gradle plugin不支持JDK6,那好把,只能修改脚本啦,如下换用sonar runner:
import org.gradle.plugins.ide.eclipse.model.Facet
apply plugin: 'java'
apply plugin: "jacoco"
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
apply plugin: "sonar-runner"
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
webAppDirName = 'WebRoot'
repositories {
mavenCentral();
flatDir {
dirs 'WebRoot/WEB-INF/lib'
}
}
dependencies {
compile fileTree(dir: 'WebRoot/WEB-INF/lib', include: ['*.jar'])
testCompile fileTree(dir: 'WebRoot/WEB-INF/lib', include: ['*.jar'])
}
compileJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
compileTestJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
sourceSets{
main{
java.srcDirs = ['src']
resources.srcDirs = ['src']
}
test{
java.srcDirs = ['test/utest']
resources.srcDirs = ['test/utest']
}
}
jacoco {
toolVersion = "0.7.7.201606060606"
}
test {
include '**/UTest*.class'
}
sonarRunner {
sonarProperties {
property "sonar.host.url", "http://192.168.1.18:19000"
property "sonar.login", "admin"
property "sonar.password", "admin123"
property "sonar.projectKey", "tax_settle"
property "sonar.projectName", "tax_settle"
property "sonar.projectVersion", "39.17.0"
property "sonar.sources", ["src","WebRoot"]
property "sonar.exclusions", ["WebRoot/download","WebRoot/images","WebRoot/model","WebRoot/META-INF","WebRoot/rrshFile","WebRoot/WEB-INF/lib","WebRoot/WEB-INF/classes","coverage-report","builder"]
property "sonar.sourceEncoding", "UTF-8"
}
}
eclipse {
wtp {
component {
contextPath = "/tax_settle"
deployName = "tax_settle"
resource sourcePath: 'src/', deployPath: "/WEB-INF/classes"
resource sourcePath: 'src/', deployPath: "/WEB-INF/classes"
resource sourcePath: 'WebRoot/', deployPath: "/"
}
facet {
facet name: 'jst.web', type: Facet.FacetType.fixed
facet name: 'wst.jsdt.web', type: Facet.FacetType.fixed
facet name: 'jst.java', type: Facet.FacetType.fixed
facet name: 'jst.web', version: '3.0'
facet name: 'jst.java', version: '1.6'
facet name: 'wst.jsdt.web', version: '1.0'
}
}
}
- 再次执行脚本,哈哈哈,终于成功啦!!!
BUILD SUCCESSFUL
Total time: 3.434 secs
[sts] -----------------------------------------------------
[sts] Build finished succesfully!
[sts] Time taken: 0 min, 3 sec
[sts] -----------------------------------------------------