Friday, July 22, 2016

Setting up development environment with Docker, Maven and IntelliJ

This post walks through the setup of development environment for Docker with IntelliJ using Maven. We will use a Spring boot application and configure IntelliJ for iterative development.

Spring Boot Application

The demo application is a simple one page app which displays a chart of VIX index. We will wire Quandl’s web service to fetch the historical data about VIX index and use CanvasJS charting library to display the chart. You can learn more about Spring Boot application with this Spring GuideHere are some highlights:

Application.java

@SpringBootApplication is the convenience annotation that marks the class as an entry point.

@SpringBootApplication
public class Application extends SpringBootServletInitializer
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}

AppConfig.java

Setup Spring wiring using java in this class. It is any class that is marked with @Configuration annotation.

@Configuration
@ComponentScan(basePackageClasses = Application.class)
public class AppConfig
{
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(httpRequestFactory());
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter jsonConverter = (MappingJackson2HttpMessageConverter) converter;
jsonConverter.setObjectMapper(new ObjectMapper());
jsonConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("application", "json", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET), new MediaType("text", "javascript", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET)));
}
}
return restTemplate;
}
@Bean
public ClientHttpRequestFactory httpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory(httpClient());
}
@Bean
public HttpClient httpClient() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
connectionManager.setMaxTotal(5);
connectionManager.setDefaultMaxPerRoute(5);
return closeableHttpClient;
}
}
view raw AppConfig.java hosted with ❤ by GitHub

View - JSPs

Application’s view pages are under /webapp/WEB-INF/jsp.  application.properties file provided configuration to wire Spring’s view resolver.

Static Content

Application’s static content under src/main/resources/static as below.




You can find entire source code here on github


pom.xml

spring-boot-maven-plugin make executing lifecycle events easier. If you open IntelliJ’s Maven window, you will see all spring-boot-run, which will allow you to run the project. You can also create run configuration for easy access.





Setting up Docker

You can setup Docker environment by following instructions from docker for mac documentation.


Docker, Maven and IntelliJ

We will use docker-maven-plugin from com.spotify

Docker file
Create src/main/docker/Dockerfile. 
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD options-analyzer-1.0-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
view raw Dockerfile hosted with ❤ by GitHub


pom.xml
Configuration for docker plugin

<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.10</version>
<configuration>
<serverId>docker-hub</serverId>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
view raw pom.xml hosted with ❤ by GitHub

ServerId tag specifies the docker repository you want to push to. The credentials to the serverId needs to be provided in your maven settings.xml. You have option of mvn encrypting the password. Follow instructions here

<servers>
<server>
<id>docker-hub</id>
<username>your_user_name</username>
<password>your_password</password>
<configuration>
<email>your_email</email>
</configuration>
</server>
view raw settings.xml hosted with ❤ by GitHub


Run Configuration
To get it running in IntelliJ, you can setup Run Configuration as follows: 
1. Open Run -> Edit Configuration
2. Add Maven Configuration
3. Provide maven command line as clean package docker:build

4. Go to the Runner tab and provide environment variables


     5. Similarly, you can also create docker:push run configuration, if you want to push docker image to docker registry. 




No comments: