Example of how to use both JDK 7 and JDK 8 in one build.--reference
JDK 8 Released
Most of us won’t be able to use/deploy JDK 8 in production for a looong time. But that shouldn’t stop us from using it, right?
It should be possible to sneak in JDK 8 in the back way, the same way we snuck in Groovy and other libraries we wanted to use.
The Test Suite to the rescue
The Maven compiler plugin run in two separate lifecycles, compile and testCompile. Those can be configured separately.
The Maven Compiler even comes with support out of the box to separate them.
If you’re lucky and don’t have some elaborate parent pom setup that sets up most of the plugins for you, the only thing you need to do is add the following to your pom:
<properties>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.testTarget>1.8</maven.compiler.testTarget>
<maven.compiler.testSource>1.8</maven.compiler.testSource>
</properties>
Now your src/main/java is compiled with target 1.7, and src/main/test compiled with target 1.8.
If you happen to have a parent pom that dominates your world, you might have to override the configuration a bit deeper. Something similar to this should work:
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<compilerArguments>
<source>${maven.compiler.target}</source>
<target>${maven.compiler.source}</target>
</compilerArguments>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
<configuration>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<compilerArguments>
<source>${maven.compiler.testTarget}</source>
<target>${maven.compiler.testSource}</target>
</compilerArguments>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
To be able to test your project you’re now forced to use JDK 8. We probably want to tell the other developers that by enforcing the same level as our tests.
Under the build section add:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>enforce-java</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>${maven.compiler.testTarget}</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
With that mind, even tho we compile with target 1.7, the compiler doesn’t know the difference between the API’s available in 1.7 and 1.8. Which means it will still compile just fine if your src/main/java classes contain calls to APIs new in 1.8. We would want to avoid JDK 8 sneaking into production, so we need to setup a API verifier that fail the build if non 1.7 API’s are found by adding this to our build section:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>signature-check</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java17</artifactId>
<version>1.0</version>
</signature>
</configuration>
</plugin>
With the project setup, we can now enjoy JDK 8 in our test suite.
Our boring JDK 1.7 source:
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
public class DoSomething {
public String execute(Callable<String> call) throws Exception {
return call.call();
}
public List<String> list() {
return Arrays.asList("a", "b", "c", "d");
}
}
And the cool new JDK 8 enabled Test Suite:
import java.util.Optional;
import org.junit.Assert;
import org.junit.Test;
public class DoSomethingTestClase {
public static final String TEST = "ABCD";
@Test
public void shouldReturnString() throws Exception {
String result = new DoSomething().execute(()-> TEST);
Assert.assertEquals(TEST, result);
}
@Test
public void shouldFilterResult() throws Exception {
Optional<String> result = new DoSomething().list()
.stream()
.map((a)-> a.toUpperCase())
.reduce((a, b)->a+b);
Assert.assertTrue(result.isPresent());
Assert.assertEquals(TEST, result.get());
}
}
Enjoy!
reference from:https://gist.github.com/aslakknutsen/9648594
Example of how to use both JDK 7 and JDK 8 in one build.--reference的更多相关文章
- linux 下安装jdk及配置jdk环境图解
linux 下安装jdk及配置jdk环境图解 一:先检測是否已安装了JDK 运行命令: # rpm -qa|grep jdk 或 # rpm -q jdk 或 #find / -name j ...
- [译]JDK 6 and JDK 7中的subString()方法
(说明,该文章翻译自The substring() Method in JDK 6 and JDK 7) 在JDK 6 and JDK 7中的substring(int beginIndex, int ...
- 了解JDK 6和JDK 7中substring的原理及区别
substring(int beginIndex, int endIndex)方法在jdk 6和jdk 7中的实现是不同的.了解他们的区别可以帮助你更好的使用他.为简单起见,后文中用substring ...
- Linux环境配置全局jdk和局部jdk并生效
全局jdk配置: 1.root用户登录 2.进入opt目录,新建java文件夹 cd /opt mkdir java 上传jdk7u79linuxx64.tar.gz包到java文件夹并解压 jd ...
- 在JDK 6和JDK 7的substring()方法的区别?
原文链接:https://www.programcreek.com/2013/09/the-substring-method-in-jdk-6-and-jdk-7/ 在JDK 6和JDK 7中subs ...
- centos 7 安装JDK (Linux安装jdk)
centos 7安装JDK (Linux安装jdk) 第一部分 首先查看centos 7是否有openjdk,如没有就跳过第一部分,直接第二部分. [master@bogon ~]$ java -ve ...
- [转帖]【JDK和Open JDK】平常使用的JDK和Open JDK有什么区别
https://www.cnblogs.com/sxdcgaq8080/p/7487369.html 其实不同的 openjdk的版本也不一样. atlassian说AdoptOpenJDK我们测试充 ...
- mac x Yosemide(10.10) 下安装 jdk 1.7 (jdk 1.8)的方法
当我们想在mac x yosemide 系统中更新jdk到1.7(1.8)的时候,会弹出下面的错误提示 解决这个问题的办法如下: 1.下载 好jdk 1.7(1.8) 地址:http://www.or ...
- linux下查看已经安装的jdk 并卸载jdk
一.查看Jdk的安装路径: whereis javawhich java (java执行路径)echo $JAVA_HOME echo $PATH 备注:如果是windows中,可以使用: set j ...
随机推荐
- [个人原创]关于java中对象排序的一些探讨(一)
有的时候我们需要将自己定义的对象,有序输出.因为一般我们程序的中间结果需要存储在容器里,那么怎样对容器中的对象按照一定次序输出就是程序员经常需要考虑的问题.本片文章探讨了怎样有序化输出容器中的对象的问 ...
- 【USACO 2.4.3】牛的旅行
[描述] 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...
- K-means聚类
聚类算法,无监督学习的范畴,没有明确的类别信息. 给定n个训练样本{x1,x2,x3,...,xn} kmeans算法过程描述如下所示: 1.创建k个点作为起始质心点,c1,c2,...,ck 2.重 ...
- Thinkphp 文本编辑器
文本编辑器:可以从网上下载---ueditor文件夹里面包含php和utf8-php两个文件夹 平时使用时主要用到获取内容和写入内容两个按钮 获取内容: <!DOCTYPE html PUBLI ...
- 利用路由修改thinkphp框架开发的后台地址
一般我们写前台是home 后台是 admin那么我们的后台是 域名/admin 那我们要随时修改这个地址是非常麻烦的事情开始可能会想到,就是把模块名称改掉,或者分组(3.1版)这样的话不但要改配置,连 ...
- JS浮点数运算Bug
JS浮点数运算Bug的解决办法(转) 37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.0849999 ...
- JS 单击复制,复制后变为已复制
这段代码是在新浪网站上找到的.先放出CSS代码: .focus a.arrow,.card_con4 li i,.cm1_menu_wrap a.cm1_menu_box,.cm1_img span, ...
- C语言对数组取地址
#include <stdio.h> main() { ] = {,,,,}; printf("a=%p\n" , a); printf("a=%p\n&qu ...
- 求数列的和 AC 杭电
求数列的和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- IDA6.6调试安卓程序配置教程
1.把ida 目录下android_server传到设备的 /data/local/tmp/ cmd执行adb shell 进入模拟器命令行 su cd /data/local/tmp/ chmod ...