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 ...
随机推荐
- Objective-C学习篇05—Foundation框架简介
iOS中所谓的框架,说到底就是一个目录,iOS提供了很多我们可以在应用程序中调用的框架.许多应用程序都使用了如Foundation.UIKit和Core Graphics这些框架.根据你为应用程序选择 ...
- JavaScript--垃圾回收器
垃圾回收: 释放不再被任何变量引用的对象 垃圾回收器: 专门记录对象的引用次数,并回收不再被引用的对象的程序. 垃圾回收器和主程序并行在后台执行 垃圾回收器会为每个对象创建一个引用计数器(counte ...
- [转载]C++中声明与定义的区别
C++学了这么多年你知道为什么定义类时,类的定义放在.h文件中,而类的实现放在cpp文件中.它们为什么能够关联到一起呢?你知道什么东西可以放在.h文件中,什么不能.什么东西又可以放在cpp文件中.如果 ...
- mybatis 学习笔记(4) —— 批量新增数据
1.业务是从前台传入List<T> ,在controller层接受参数,并进行批量新增操作. 2.需要处理的细节 a) mybatis可以支持批量新增,注意数据表需要将主键设置成自增列. ...
- thinkphp action.class.php 学习
控制器类(Action) 描述 Description ThinkPHP Action控制器基类 抽象类 位置:ThinkPHP/Lib/Core/Action.class.php 声明: abstr ...
- WEB编码事项
标准 WEB开发标准是一系列标准的集合, 包含HTML结构标准.CSS表现标准.JS行为标准.代码标准.标准测试. 目标 WEB开发流程统一标准化,实现页面结构.表现.行为适当分离,提高页面易维护性, ...
- 海园帮忙写的JQUERY功能,实现了我们想要的,我觉得有点屌哟~~
需求就是为了可以在WEB在线更新代码期间,如果执行时间较长的话, 就在提交按钮之后,按钮变为灰色. 同时,一个DIV里每隔两秒刷新输出. 当更新完成之后(检测文档中的关键字串),按钮变为可提交状态~~ ...
- Delphi 缩放图像代码 - 支持PNG透明通道(利用了Windows的windowscodecs.dll)
要求Delphi2007或者更高版本, 系统要求至少XP-SP2以上 实际上是利用了Windows的windowscodecs.dll这个文件的功能 在VCL里已经封装为TWICImage类 proc ...
- new Thread的弊端(转)
new Thread的弊端如下: a. 每次new Thread新建对象性能差.b. 线程缺乏统一管理,可能无限制新建线程,相互之间竞争,及可能占用过多系统资源导致死机或oom.c. 缺乏更多功能,如 ...
- Power Network (最大流增广路算法模板题)
Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 20754 Accepted: 10872 Description A p ...