Spring Boot 兼容 JDK1.6 运行遗留项目
最近有几个遗留项目需要改造成 Rest API,为了统一想采用和新项目一致的 Spring Boot 框架。原型验证的阶段发现老项目用了一些 Java6 的独有API,主要是数字证书那部分API,Java7之后去除了这些sun的包。为了平稳过渡,第一阶段考虑先 Spring Boot 1.5 + Java 6,然后逐步重写老的业务逻辑,逐步去除Java 6依赖。
先看 Spring Boot 官方文档 对于Java 6的说法
85.11 How to use Java 6
If you want to use Spring Boot with Java 6 there are a small number of configuration changes that you will have to make. The exact changes depend on your application’s functionality.
85.11.1 Embedded servlet container compatibility
If you are using one of Boot’s embedded Servlet containers you will have to use a Java 6-compatible container. Both Tomcat 7 and Jetty 8 are Java 6 compatible. See Section 73.16, “Use Tomcat 7.x or 8.0” and Section 73.18, “Use Jetty 8” for details.
85.11.2 Jackson
Jackson 2.7 and later requires Java 7. If you want to use Jackson with Java 6 you will have to downgrade to Jackson 2.6.
Spring Boot uses the Jackson BOM that was introduced as of Jackson 2.7 so you can’t just override the jackson.version property. In order to use Jackson 2.6, you will have to define the individual modules in the dependencyManagement section of your build, check this example for more details.
主要限制如下:
- Tomcat支持到7.x
- Maven支持到3.2.x
- Jackson支持到2.6.x
Maven配置如下
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo2o.example</groupId>
<artifactId>spring-boot-legacy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-legacy</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot legacy support with Java 6</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.21.RELEASE</version>
</parent>
<properties>
<java.version>1.6</java.version>
<tomcat.version>7.0.70</tomcat.version>
<jackson.version>2.6.7</jackson.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- tomcat替换为7.0.70 -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
<!-- jackson 替换为2.6.7 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 解决Java 6不支持TLS1.2的问题 -->
<profiles>
<profile>
<id>java6</id>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.21.RELEASE</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<jvm>${env.JAVA_HOME}/bin/java</jvm>
</configuration>
</plugin>
</plugins>
</build>
<!-- 使用aliyun镜像 -->
<repositories>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
</repositories>
</project>
除了修改部分依赖版本,增加了一个 java6 的 profile,强制使用http协议访问中央库(Java6 不支持 TLS1.2,Maven 2018年开始不支持TLS1.0/1.1)。
通过上述配置,项目已经可以在Java 6环境下正常运行。为了方便编译和运行,做了2个bat脚本。指定编译时使用特定的JDK和Maven。
set JAVA_HOME=C:\home\dev\java\jdks\jdk1.6.0_45
set M2_HOME=C:\home\dev\java\tools\apache-maven-3.2.5
C:\home\dev\java\tools\apache-maven-3.2.5\bin\mvn.bat -e -Pjava6 clean package spring-boot:repackage
pause