Note: This article is original from https://gist.github.com/aslakknutsen/9648594

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!

[转]how to use both JDK 7 and JDK 8 in one build的更多相关文章

  1. linux 下安装jdk及配置jdk环境图解

    linux 下安装jdk及配置jdk环境图解 一:先检測是否已安装了JDK 运行命令: # rpm -qa|grep jdk  或   # rpm -q jdk  或  #find / -name j ...

  2. [译]JDK 6 and JDK 7中的subString()方法

    (说明,该文章翻译自The substring() Method in JDK 6 and JDK 7) 在JDK 6 and JDK 7中的substring(int beginIndex, int ...

  3. 了解JDK 6和JDK 7中substring的原理及区别

    substring(int beginIndex, int endIndex)方法在jdk 6和jdk 7中的实现是不同的.了解他们的区别可以帮助你更好的使用他.为简单起见,后文中用substring ...

  4. Linux环境配置全局jdk和局部jdk并生效

    全局jdk配置: 1.root用户登录 2.进入opt目录,新建java文件夹 cd  /opt mkdir java  上传jdk7u79linuxx64.tar.gz包到java文件夹并解压 jd ...

  5. 在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 ...

  6. centos 7 安装JDK (Linux安装jdk)

    centos 7安装JDK (Linux安装jdk) 第一部分 首先查看centos 7是否有openjdk,如没有就跳过第一部分,直接第二部分. [master@bogon ~]$ java -ve ...

  7. [转帖]【JDK和Open JDK】平常使用的JDK和Open JDK有什么区别

    https://www.cnblogs.com/sxdcgaq8080/p/7487369.html 其实不同的 openjdk的版本也不一样. atlassian说AdoptOpenJDK我们测试充 ...

  8. 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 ...

  9. 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 tha ...

  10. linux下查看已经安装的jdk 并卸载jdk

    一.查看Jdk的安装路径: whereis javawhich java (java执行路径)echo $JAVA_HOME echo $PATH 备注:如果是windows中,可以使用: set j ...

随机推荐

  1. HackSix 为ViewGroup的子视图添加悦目的动画效果

    1.默认情况下他,添加到viewGrop的子视图是直接显示出来的.有一个比较简单的方法可以为这个过程增加动画效果. 2.知识点:     给子视图添加动画效果就用:LayoutAnimationCon ...

  2. struts2中错误提示:There is no Action mapped for namespace / and action name

    当在struts2中运行时出现如上所述错误时: 1.在src目录下创建struts.xml一定要注意拼写 2.struts.xml文件中引入和extend是否正确 3.在web.xml 中<we ...

  3. 【大数据之数据仓库】安装部署GreenPlum集群

    本篇将向大家介绍如何快捷的安装部署GreenPlum测试集群,大家可以跟着我一块儿实践一把^_^ 1.主机资源 申请2台网易云主机,操作系统必须是RedHat或者CentOS,配置尽量高一点.如果是s ...

  4. 七、CommonJS规范和Note.js模块概念的介绍

    在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多 ...

  5. 「CF622F」The Sum of the k-th Powers「拉格朗日插值」

    题意 求\(\sum_{i=1}^n i^k\),\(n \leq 10^9,k \leq 10^6\) 题解 观察可得答案是一个\(k+1\)次多项式,我们找\(k+2\)个值带进去然后拉格朗日插值 ...

  6. loj #6235. 区间素数个数

    #6235. 区间素数个数 题目描述 求 1∼n 1\sim n1∼n 之间素数个数. 输入格式 一行一个数 n nn . 输出格式 一行一个数,表示答案. 样例 样例输入 10 样例输出 4 样例解 ...

  7. CPU 的工作原理

    内部架构 CPU 的根本任务就是执行指令,对计算机来说最终都是一串由 0 和 1 组成的序列.CPU 从逻辑上可以划分成 3 个模块,分别是控制单元.运算单元和存储单元 .其内部架构如下: [1]控制 ...

  8. python3+Django1.11+mysql5.7 MySQL DB API Drivers

    The Python Database API is described in PEP 249. MySQL has three prominent drivers that implement th ...

  9. screen虚拟终端

    screen命令相当于后台执行(虚拟终端) 用法:在一些要执行很久的操作,比如我有个文件有10个G,要传输一天左右,你如果是直接传输,万一你的连接断了.是不是意味着你的操作白费的呢,这时我们可以打开一 ...

  10. ansible基本模块-copy

    ansible   XXX   -m copy  -a  “src=XXX  dest=XXX  owner=root  group=root  mode=0755”