加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Example of how to use both JDK 7 and JDK 8 in one build.--re

发布时间:2020-12-14 06:19:27 所属栏目:Java 来源:网络整理
导读: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 othe

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:

   
      1.7
      1.7
      1.8
      1.8
   

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:

   
      
...
         
            org.apache.maven.plugins
            maven-compiler-plugin
            3.1
            
               
                  default-compile
                  
                     true
                     true
                     
                        ${maven.compiler.target}
                        ${maven.compiler.source}
                     
                  
               
               
                  default-testCompile
                  
                     true
                     true
                     
                        ${maven.compiler.testTarget}
                        ${maven.compiler.testSource}
                     
                  
               
            
         
...
      
   

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:

         
            org.apache.maven.plugins
            maven-enforcer-plugin
            1.3.1
            
               
                  enforce-java
                  
                     enforce
                  
                  
                     
                        
                           ${maven.compiler.testTarget}
                        
                     
                  
               
            
         

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:

         
            org.codehaus.mojo
            animal-sniffer-maven-plugin
            1.7
            
               
                  signature-check
                  verify
                  
                     check
                  
               
            
            
               
                  org.codehaus.mojo.signature
                  java17
                  1.0
               
            
         

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 call) throws Exception {
return call.call();
}

public List 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读