Build Docker Image File of Spring Boot Application - maven

I am trying to build a docker image for my spring boot maven project by using dockerfile-maven-plugin. I am using Docker Tool Box on windows 7 and it is running fine.
I am getting Below Error:
Failed to load Google application default credentials
java.io.IOException: The Application Default Credentials are not
available. They are available if running in Google Compute Engine.
Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS
must be defined pointing to a file defining the credentials. See
https://developers.google.com/accounts/docs/application-default-credentials
for more information.
Pom.XML Build
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.10</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<serverId>docker-hub</serverId>
<repository>${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>D:\spring\MicroServiceOutput</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
I am not able to get that why we need google default credential to build a local image.Please help me out as I am new to Docker World.

It seems that's a bug, by default the plugin set googleContainerRegistryEnabled flag to true, so you have to change that flag to false, try this:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
<docker.image.prefix>prefix</docker.image.prefix>
<docker.image.name>${project.artifactId}</docker.image.name>
<docker.image.tag>${project.version}</docker.image.tag>
<docker.file>Dockerfile</docker.file>
</properties>
...
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.10</version>
<configuration>
<googleContainerRegistryEnabled>false</googleContainerRegistryEnabled>
<repository>${docker.image.prefix}/${docker.image.name}</repository>
<tag>${docker.image.tag}</tag>
<dockerfile>${docker.file}</dockerfile>
</configuration>
<executions>
<execution>
<id>build</id>
<goals>
<goal>build</goal>
<goal>tag</goal>
</goals>
</execution>
<execution>
<id>push</id>
<goals>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>

Related

How to create docker image in Spring Boot project

I have tried using spotify/docker-maven-plugin without any success .
Below is part of my pom.xml file
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>myrepo/maven-docker-spotify</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}-jar-with-dependencies.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
The spotify/docker-maven-plugin you are using is currently inactive. It's recommended using spotify/dockerfile-maven-plugin instead.
So change the plugin section of your pom.xml file to resemble below
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>spotify/foobar</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
Note: You can also try using JIB maven plugin that doesnt require you to have docker installed and works with minimal configuration. With JIB, Running below command in is enough to do the jo
mvn compile com.google.cloud.tools:jib-maven-plugin:0.9.2:dockerBuild

Springboot failsafe multi-module integration test

I have to run integration-test with fail safe and my project is a multi module project, with one module for selenium test and another one with the actual web app.
I want to run the web app then the test over this app and then stopping this app and getting the report of the integration tests.
In order to do this I used this, inside the pom of the selenium test module :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>reserve-tomcat-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>process-resources</phase>
<configuration>
<portNames>
<portName>tomcat.http.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.9.RELEASE</version>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<includes>
<include>
<groupId>com.example</groupId>
<artifactId>web-app</artifactId>
</include>
</includes>
<mainClass>com.example.App</mainClass>
<arguments>
<argument>--server.port=${tomcat.http.port}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<test.server.port>${tomcat.http.port}</test.server.port>
</systemPropertyVariables>
</configuration>
</plugin>
The error I get is that the main class is not found :
java.lang.ClassNotFoundException: App
It feels like inside the selenium test I can't get to the webapp classPath, It seems like its trying to run the selenium test main instead of the webapp's one

ossrh nexus staging - what determines the repository id?

I am deploying my project to maven central and I notice that I have no control on the repository id, which seems to be automatically generated and in my case, seems to be incorrect and hence the deployment fails. I could have done something wrong but I do now know where to look. Any suggestions would be appreciated.
My pom:
<groupId>uk.ac.shef.dcs</groupId>
<artifactId>sti</artifactId>
<version>1.0alpha</version>
<packaging>pom</packaging>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>${maven-gpg-plugin.version}</version>
<configuration>
<skip>false</skip>
</configuration>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
When running 'mvn clean deploy', it generates log like this:
Uploaded: https://oss.sonatype.org:443/service/local/staging/deployByRepositoryId/ukacshefdcsjate-1015/uk/ac/shef/dcs
I do not know how it generates this id: ukacshefdcsjate-1015. I thought it should corresponds to the project package path, but my package should be uk.ac.shef.dcs.sti. and I never had a package called 'jate' in my project. In fact, jate is another project I already released to maven central.
And I think it is this problem that after running 'mvn clean deploy', I could not see anything available from: https://oss.sonatype.org/content/repositories/snapshots/

How to trigger Maven SCM plugin to automatically switch goals based on existing directory?

I'm new to Maven and having an issue where I'm trying to automatically change the SCM plugin goal from checkout to update based on whether the source is already checked out.
Can anyone show me a code example to get this working?
This is the plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>checkout</goal>
</goals>
<configuration>
<connectionType>developerConnection</connectionType>
<scmVersion>master</scmVersion>
<scmVersionType>branch</scmVersionType>
<checkoutDirectory>${project.basedir}/src</checkoutDirectory>
<workingDirectory>${project.basedir}/src</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
change goal:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>update</goal>
</goals>
<configuration>
<connectionType>developerConnection</connectionType>
<scmVersion>master</scmVersion>
<scmVersionType>branch</scmVersionType>
<checkoutDirectory>${project.basedir}/src</checkoutDirectory>
<workingDirectory>${project.basedir}/src</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
Reference
https://maven.apache.org/scm/maven-scm-plugin/
https://maven.apache.org/scm/maven-scm-plugin/update-mojo.html
To change the goal of the SCM plugin was inspired by Đỗ Như Vý (above).
Approach was to
Place the goal in a property called scm.goal set to a default value
ie update.
Use a profile (bootstrap) to change the scm.goal property value from
'update' to 'checkout'.
Activate the bootstrap profile based on missing .gitignore file.
Place the property scm.goal in the SCM plugin
goal element.
Code:
<properties>
<scm.dest.path>${project.basedir}/src</scm.dest.path>
<scm.goal>update</scm.goal>
</properties>
<profiles>
<profile>
<id>bootstrap</id>
<activation>
<file>
<missing>./src/.gitignore</missing>
</file>
</activation>
<properties>
<scm.goal>checkout</scm.goal>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>${scm.goal}</goal>
</goals>
<configuration>
<connectionType>developerConnection</connectionType>
<scmVersion>master</scmVersion>
<scmVersionType>branch</scmVersionType>
<checkoutDirectory>${scm.dest.path}</checkoutDirectory>
<workingDirectory>${scm.dest.path}</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
...

Maven cargo jetty6x: is it possible to provide additional Jetty configuration?

I have application launched using maven cargo plugin with jetty6x. I get HTTP/1.1 413 FULL head on certain requests. I found out, that I need to specify larger headerBufferSize (due to large request header size). Is there a way to provide it to the cargo configuration?
My cargo configuration:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.1.2</version>
<configuration>
<contextPath>/${jetty.admin.context}</contextPath>
<container>
<containerId>jetty6x</containerId>
<type>embedded</type>
</container>
<configuration>
<properties>
<cargo.servlet.port>${jetty.port}</cargo.servlet.port>
</properties>
<deployables>
<deployable>
<properties>
<context>/http</context>
</properties>
<groupId>xxx.xxx.server</groupId>
<artifactId>http</artifactId>
<type>war</type>
</deployable>
</deployables>
</configuration>
<wait>false</wait>
</configuration>
<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
You want to specify the additional configuration files and the directories where they go under
<configuration><configfiles>...
in the configuration of the cargo plugin
see this post or the cargo site.

Resources