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

时间: 2024-11-03 01:18:24

Example of how to use both JDK 7 and JDK 8 in one build.--reference的相关文章

The substring() Method in JDK 6 and JDK 7

The substring() Method in JDK 6 and JDK 7 By X Wang The substring(int beginIndex, int endIndex) method in JDK 6 and JDK 7 are different. Knowing the difference can help you better use them. For simplicity reasons, in the followingsubstring() represen

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

linux 下安装jdk及配置jdk环境图解 一:先检测是否已安装了JDK 执行命令: # rpm -qa|grep jdk  或   # rpm -q jdk  或  #find / -name jdk* /soft/openfire_java/jdk-7u40-linux-x64.rpm /usr/java/jdk1.7.0_15 /usr/java/jdk1.7.0_15/jre/lib/servicetag/jdk_header.png /usr/java/jdk1.7.0_15/lib

mac x Yosemide(10.10) 下安装 jdk 1.7 (jdk 1.8)

注意: 双击jdk8后, 将 Java 8 Update 05.pkg包拖到Teminal中,可以获得正确的地址, 然后copy该地址替换下文对应的地址,空格等都不能出错.本人已经按照教程跌跌撞撞实验成功. mac x Yosemide(10.10) 下安装 jdk 1.7 (jdk 1.8) 在mac x yosemide 系统中不能正常更新jdk到1.7(1.8)的问题,会弹出上面的错误 提示.很多人就在这里会选择放弃他的jdk升级之旅,或者是还原他的mac系统 .其实没那么复杂.来看看我是

在JDK 6和JDK 7的substring()方法的区别?

原文链接:https://www.programcreek.com/2013/09/the-substring-method-in-jdk-6-and-jdk-7/ 在JDK 6和JDK 7中substring(int beginIndex,int endIndex)的实现是不同的,下面将会做出详细的解释.为简单起见,substring()方法表示的substring(int beginIndex,int endIndex)在这篇文章中的方法. 1.substring()方法 substring

【JDK和Open JDK】平常使用的JDK和Open JDK有什么区别

注意到这个问题,是在CentOS7上安装JDK的时候,查找相关的资料,发现安装JDK之前都需要检查或卸载系统上原生的Open JDK,这才引起了注意. 到了这里,引用查到的一篇说明. 转自:http://fgh2011.iteye.com/blog/1771649 历史上的原因是,openjdk是jdk的开放原始码版本,以GPL协议的形式放出.在JDK7的时候,openjdk已经成为jdk7的主干开 发,sun jdk7是在openjdk7的基础上发布的,其大部分原始码都相同,只有少部分原始码被

解决 Linux 终端 wget 命令下载jdk的问题,jdk在linux下的配置问题

最近在用Linux搭服务器,在下载jdk时取oracle官网找到下载地址,然后用wget + 下载地址 去下载,2秒之后,文件下载好了, 然而查看文件大小,只有800多k,显然有错误,后来查资料才发现这样获取到的网址是不行的.最终琢磨出个办法,管不管用,试一试. 打开要下载的jdk资源的那个页面http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk7-downloads-1880260.html 2.按F12打开浏览器调试界面

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.oracle.com/technetwork/java/javase/downloads/index.html 2.打开下载好的DMG .然后会出现下面的界面 右击拷贝JDK7 Update 60.pkg,然后保存到任意目录,并把JDK7 Update 60.pkg重命名为JDK7.pkg(这里也可以不

【原创】Mac OS X 下同时安装多个版本的JDK(JDK 1.5 ~ JDK 1.8)

虽然 Java 8 的正式版已经发布了两年有余,但目前 Java 企业级应用的主打版本还是 Java 6 和 Java 7,更惨的是公司的一些早期项目还必须在 Java 5 下开发运行,而我还想在工作之余体验+学习 Java 8 的新特性.于是,我需要在我的 Mac 上同时安装 JDK 1.5,JDK 1.6, JDK 1.7 和 JDK 1.8. 过去 Mac 上的 Java 都是由 Apple 自己提供的,但只支持到 Java 6,并且从 OS X 10.7 开始系统不再默认安装了(可选安装

二 、在 JDK 6 and JDK 7中 substring() 方法

在JDK6 和JDK 7 里面substring(int beginIndex, int endIndex)的方法是不同的.知道这种区别会帮助你更好用它们.为了简单期间,下面用substring() 来表示 substring(int beginIndex,Int endIndex) 方法. 1. substring()方法做什么substring(int beginIndex,int endIndex)方法返回一个从beginIndex开始,到endIndex-1结束的字符串. String