⬆️ ⬇️

Java 8 in IaaS InfoboxCloud in one command. We use java 8 in tests

Most recently, Java 8 was released. This is a long-awaited event for all developers on the JVM platform and will certainly affect the code in enterprise projects.



We at InfoboxCloud love Java technology and appreciate Java developers. For the release of Java 8, we have written scripts that allow us to install JRE 8 or JDK 8 into the InfoboxCloud Cloud Infrastructure on any Linux OS in one command. Simply select the script, paste it into the console and press Enter. It is much better to spend time writing good code than installing java.

Java 8 InfoboxCloud IaaS

On the Cloud platform InfoboxCloud Jelastic , Java 8 support will appear in the next update of the software stack pretty soon.



In this article, installing Java 8, we will look at how to test your development software with Java 8, building it under Java 7.



')

Installing Java 8 into one command



To install the appropriate version of Java, simply log in to your InfoboxCloud cloud infrastructure machine and paste the specified command. Of course, there is no vendor-lock in the scripts and they can be used on your server, but they were tested in InfoboxCloud. Tested on machines with CentOS 6, Ubuntu 12.04, Ubuntu 13.10, Debian 7, openSuse 13.1 for i686 and x86_64.



JDK 8


wget repository.jelasticloud.com/scripts/jvm/8/jdk8 && chmod +x jdk8 && ./jdk8 && rm -rf jdk8 




JRE 8


 wget repository.jelasticloud.com/scripts/jvm/8/jre8 && chmod +x jre8 && ./jre8 && rm -rf jre8 




Install Java 7 in one command



Of course, we understand that in a production environment JDK7 and JRE7 will be used for a long time, so we prepared installation scripts for one command for Java 7 as well.

Java 7 and 8 can be installed in parallel: it is recommended to install 7, then 8.



JDK 7 update 51


 wget repository.jelasticloud.com/scripts/jvm/7u51/jdk7u51 && chmod +x jdk7u51 && ./jdk7u51 && rm -rf jdk7u51 




JRE 7 update 51


 wget repository.jelasticloud.com/scripts/jvm/7u51/jre7u51 && chmod +x jre7u51 && ./jre7u51 && rm -rf jre7u51 




We write tests for Java 8, and the application code for Java 7



Java 8 is completely new. It will take many more updates to finally enterprise became confident in its quality. However, right now you can write tests on java 8, and application code on java 7.

Maven Compiler plugin runs in two modes: compile and testCompile. They can be customized.



Add the following section to your pom file:

 <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 for java 7, and src / main / test is compiled for 8.



If you have a parent pom configured for the project, you need to reload its configuration:





This code snippet was loaded with a picture, because habrahabr incorrectly interprets the source tag in the maven code. Github Gist is available here .



Now you can test your project using JDK8. You may want to tell other developers to raise the level of the entire project to what is used in your tests.

Add to build section:

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


Note that even when compiling the project itself in 7, the compiler does not understand the difference between APIs 7 and 8. This means that the project will continue to compile successfully even if you use API 8 in src / main / java. Let's configure the check, preventing such a situation:

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


The project is successfully configured. Let's now use JDK8 in tests:



JDK 7 source code:



 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"); } } 




Tests with JDK 8:



 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()); } } 


Thanks for the idea of ​​this kind of testing with the JDK8 Aslak Knutsen.



Successful programming with InfoboxCloud !

Source: https://habr.com/ru/post/216563/



All Articles