Quarkus: Try to cache maven dependencies using multistage docker build - maven

I have got a simple Quarkus application and I try to build it using the following multistage Dockerfile.
FROM maven:3-jdk-8-slim AS build
WORKDIR /build
# Download Dependencies
COPY pom.xml .
RUN mvn dependency:go-offline
# Build App
COPY src/ /build/src/
RUN mvn -Dmaven.test.skip=true package -Dcheckstyle.skip
# Stage 2 : create the docker final image
FROM adoptopenjdk:8-jre-openj9 AS runtime
COPY --from=build /build/target/*-runner.jar /app/app.jar
COPY --from=build /build/target/lib/* /app/lib/
WORKDIR /app
RUN chgrp -R 0 /app &&\
chmod g=u /app
USER 1001
EXPOSE 8080
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/app.jar"]
and the pom.xml
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>booking-mgr</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus-plugin.version>1.1.1.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>1.1.1.Final</quarkus.platform.version>
<surefire-plugin.version>2.22.1</surefire-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-reactive-messaging-kafka</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemProperties>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemProperties>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
The build works fine, it downloads the maven dependencies, then it creates the .jar and runs the .jar in the final container. But if I change something in the source code and leave the pom.xml untouched, the dependencies are downloaded again. It seems that mvn dependency:go-offline does not download all dependencies.
Is there a way to speed up docker builds that way? For example, I do the same with Spring Boot and everything works great there.
Thank you for your help.

Your pom.xml and src folder are in the build context (the dot in your command).
If you change files in src then you change your build context so you invalidate the COPY cache.
See Docker build is not using cache

Simply, you can split the Dockerfile into two, such as builder-base.dockerfile and final.dockerfile. Then, create a directory builder-base and move pom.xml into builder-base.
directory structure:
.
+-- builder-base
| +-- pom.xml
|-- src
|-- builder-base.dockerfile
|-- final.dockerfile
In the final.dockerfile is :
FROM java-builder AS build
# Build App
COPY src/ /build/src/
RUN mvn -Dmaven.test.skip=true package -Dcheckstyle.skip
# Stage 2 : create the docker final image
FROM adoptopenjdk:8-jre-openj9 AS runtime
COPY --from=build /build/target/*-runner.jar /app/app.jar
COPY --from=build /build/target/lib/* /app/lib/
WORKDIR /app
RUN chgrp -R 0 /app &&\
chmod g=u /app
USER 1001
EXPOSE 8080
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/app.jar"]
First, put this code into builder-base.dockerfile:
FROM maven:3-jdk-8-slim
WORKDIR /build
# Download Dependencies
COPY pom.xml .
RUN mvn dependency:go-offline
So, at first, you should build a image named 'java-builder'
docker build -t java-builder -f builder.dockerfile ./builder-base
Now, you can compile the source code use the command below:
docker build -t app -f final.dockerfile .
The base image in the final.dockerfile is java-builder, if you don't re-build the image, you can always create the docker final image using the cache.

I found mvn package will do the work.
Now I use:
COPY pom.xml .
RUN mvn --batch-mode \
--quiet \
--errors \
dependency:go-offline \
package
COPY src ./src
RUN mvn --batch-mode \
--quiet \
--errors \
--define maven.test.skip=true \
--define java.awt.headless=ture \
clean package
Looks weird, but no errors.

Related

Not able to run Quarkus unit and integration test in gitlab-ci

I'm trying to setup my quarkus project with gitlab-ci and I'm constantly running into the next problem.
Right now I'm trying to figure out how quarkus is running unit test vs integration test.
Both unit test and integration test work if run from the command line like this:
export QUARKUS_PROFILE=test
./mvnw verify
But when running from gitlab-ci it runs in containers and I just don't seem to be able to find out the right environment to setup.
The build stage below (only unit test - #QuarkusTest) runs fine.
When adding integration test (./mvnw verify) it will fail with the database connection failed.
The verify stage below however fails on the unit tests.
If I disable the unit tests (with #Disabled) the integration tests (#QuarkusIntegrationTest) runs fine with below environment variables.
It seems like unit tests needs the default postgress url (QUARKUS_DATASOURCE_REACTIVE_URL - not sure how to figure out what the actual default value is), but in integration test on gitlab this will fail with: Connect Connection refused). This can be solved with using the QUARKUS_DATASOURCE_REACTIVE_URL below.
Here is my application.properties
quarkus.container-image.group=yyyy/slideshow
quarkus.container-image.insecure=true
quarkus.container-image.registry=xxxx:5555
quarkus.container-image.build=true
quarkus.rest-client.piwigo-api.url=https://<hostname>
quarkus.datasource.devservices.enabled=true
quarkus.datasource.db-kind=postgresql
quarkus.hibernate-orm.database.generation=drop-and-create
%prod.quarkus.http.port=9090
%test.quarkus.http.port=9080
%test.quarkus.http.test-port=9081
%test.quarkus.hibernate-orm.sql-load-script=import.sql
Here is my gitlab-ci (quarkus-dev is my own image: eclipse-temurin:17 with docker-ce):
image: xxxx:5555/yyyy/util/quarkus-dev:0.1.0
variables:
IMAGE: "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"
stages:
- build
- verify
- docker
unit-test:
stage: build
script:
- ./mvnw test
integration-test:
stage: verify
variables:
QUARKUS_PROFILE: test
QUARKUS_DATASOURCE_REACTIVE_URL: "vertx-reactive:postgresql://<ip docker host>:5432/quarkus?loggerLevel=OFF"
TEST_URL: "http://<ip docker host>:9081"
script:
- ./mvnw verify
docker-build:
stage: docker
when: manual
variables:
QUARKUS_CONTAINER_IMAGE_PUSH: "true"
script:
- ./mvnw install -DskipTests
A minimal example:
I based this example based on https://github.com/quarkusio/quarkus-quickstarts/tree/main/hibernate-reactive-panache-quickstart.
Dockerfile for the image used to run the pipeline in gitlab (/quarkus-dev:0.1.0)
FROM eclipse-temurin:17 as builder
RUN apt-get update && \
apt-get install -y apt-transport-https ca-certificates curl software-properties-common && \
apt-get clean
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - && \
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && \
apt-get clean
RUN apt-get install -y docker-ce && \
apt-get clean
Updated dockerfile src/main/docker/Dockerfile.jvm (changed to java 17):
FROM registry.access.redhat.com/ubi8/openjdk-17:1.14
ENV LANGUAGE='en_US:en'
# We make four distinct layers so if there are application changes the library layers can be re-used
COPY --chown=185 target/quarkus-app/lib/ /deployments/lib/
COPY --chown=185 target/quarkus-app/*.jar /deployments/
COPY --chown=185 target/quarkus-app/app/ /deployments/app/
COPY --chown=185 target/quarkus-app/quarkus/ /deployments/quarkus/
EXPOSE 8080
USER 185
ENV AB_JOLOKIA_OFF=""
ENV JAVA_OPTS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"
Updated pom file (similar to my failing project)
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>hibernate-reactive-panache-quickstart</artifactId>
<version>0.1.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.10.1</compiler-plugin.version>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>2.15.2.Final</quarkus.platform.version>
<surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
<assertj.version>3.24.2</assertj.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-reactive-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-pg-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-container-image-docker</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<skipITs>false</skipITs>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
Updated Application properties:
quarkus.container-image.group=<group-name>
quarkus.container-image.insecure=true
quarkus.container-image.registry=<registery url>
quarkus.container-image.build=true
quarkus.datasource.devservices.enabled=true
quarkus.datasource.db-kind=postgresql
quarkus.hibernate-orm.database.generation=drop-and-create
%test.quarkus.http.port=9080
%test.quarkus.http.test-port=9081
%test.quarkus.hibernate-orm.sql-load-script=import.sql
Updated unit test:
package org.acme.hibernate.orm.panache;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;
#QuarkusTest
public class FruitsEndpointTest {
#Test
#Disabled
public void testListAllFruits() {
//List all, should have all 3 fruits the database has initially:
Response response = given()
.when()
.get("/fruits")
.then()
.statusCode(200)
.contentType("application/json")
.extract().response();
assertThat(response.jsonPath().getList("name")).containsExactlyInAnyOrder("Cherry", "Apple", "Banana");
}
}
Updated integration test:
package org.acme.hibernate.orm.panache;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;
#QuarkusIntegrationTest
public class FruitsEndpointIT {
#Test
#Disabled
public void testListAllFruits() {
//List all, should have all 3 fruits the database has initially:
Response response = given()
.when()
.get("/fruits")
.then()
.statusCode(200)
.contentType("application/json")
.extract().response();
assertThat(response.jsonPath().getList("name")).containsExactlyInAnyOrder("Cherry", "Apple", "Banana");
}
}
Minimal gitlab-ci.yml:
image: <xxx>/quarkus-dev:0.1.0
stages:
- verify
unit-test:
stage: verify
script:
- ./mvnw test
integration-test:
stage: verify
variables:
QUARKUS_PROFILE: test
QUARKUS_DATASOURCE_REACTIVE_URL: "vertx-reactive:postgresql://<docker host ip>:5432/quarkus?loggerLevel=OFF"
TEST_URL: "http://<docker host ip>:9081"
script:
- ./mvnw verify
Running on the cmdline the following works:
export QUARKUS_PROFILE=test
./mvnw verify
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
The above example also works on the gitlab runner.
However, as you se the unit test is disabled.
After enabling it still works locally (mvnw verify), but it will fail on the gitlab runner.
The unit-test job will succeed, but the integration-test job will fail on the unit tests.
2023-01-28 12:34:45,902 ERROR [org.hib.rea.errors] (vert.x-eventloop-thread-3) HR000057: Failed to execute statement [select fruit0_.id as id1_0_, fruit0_.name as name2_0_ from Fruit fruit0_ order by fruit0_.name]: could not execute query: java.util.concurrent.CompletionException: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: /<ip>:5432
.
.
.
[ERROR] Failures:
[ERROR] FruitsEndpointTest.testListAllFruits:22 1 expectation failed.
Expected status code <200> but was <500>.

How do you configure a Maven/SpringBoot Project's pom.xml for docker?

I am attempting to dockerize my SpringMVC application via Maven. My intent is to have an image that I can then proceed to expose and display via my web browser.
Unfortunately, in following this guide, I appear to still lack a critical piece of understanding concerning the pom.xml edits I must make to achieve this, and the Dockerfile.
======
Here is the Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
======
Here is the source code's pom.xml in its latest revision.
======
Here is my latest attempt at revision, in following the example pom.xml of the SpringIO guide I referenced above (dependencies section not included).
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.davidonus</groupId>
<artifactId>davidonusSpringDemo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- tag::packaging[] -->
<packaging>jar</packaging>
<name>davidonusSpringDemo1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- tag::docker[] -->
<docker.image.prefix>springio</docker.image.prefix>
</properties>
<profiles>
<profile>
<id>DavidSpringTime</id>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- tag::plugin[] -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.9</version>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
</configuration>
</plugin>
<!-- end::plugin[] -->
<!-- tag::unpack[] -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<!-- end::unpack[] -->
</plugins>
</build>
Here are my present results, using the command mvn install build:docker
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.050 s
[INFO] Finished at: 2019-06-15T13:24:01-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:3.1.1:unpack (unpack) on project davidonusSpringDemo1: Unable to update Marker timestamp: /home/david/Desktop/DevOps2019/springBoot/teluskoSpringBoot/target/dependency-maven-plugin-markers/com.davidonus-davidonusSpringDemo1-jar-0.0.1-SNAPSHOT.marker: Unable to update last modified timestamp on marker file /home/david/Desktop/DevOps2019/springBoot/teluskoSpringBoot/target/dependency-maven-plugin-markers/com.davidonus-davidonusSpringDemo1-jar-0.0.1-SNAPSHOT.marker -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
======
In summary, given my original pom.xml, what changes would you implement to make my SpringBoot + Maven project deployable as a docker image and container?
Furthermore, are there adaptions to my Dockerfile that you'd make? Your consultation is appreciated. Thank you.
If you have your springboot code ready with pom.xml. Then follow below steps to containerize your application.
git clone https://github.com/dnmorris7/teluskoSpringBoot (I'm cloning your springboot code)
git checkout module5 (checked out module5 branch)
Created Dockerfile in your git codebase with following contents:
FROM maven:3.6-jdk-8-slim AS build
COPY . /usr/src/app/
WORKDIR /usr/src/app/
RUN mvn -f /usr/src/app/pom.xml clean package
FROM java:8-alpine
WORKDIR /
COPY --from=build /usr/src/app/target/*.jar /app.jar
CMD java -jar app.jar
NOTE: I'm using docker multi-stage build where in first stsage maven builds the jar and in the second stage we copy that jar in java image.
Now build your docker image docker build -t appimage:v1 .
Run your docker container docker run -it -d -p 9090:9090 appimage:v1
Hit the api to check if its working fine.
$ curl localhost:9090/home
{"timestamp":"2019-06-16T05:34:26.655+0000","status":404,"error":"Not Found","message":"/pages/home.jsp","path":"/home"}
Please hit the correct base url, I tried with /home
NOTE: If you want to provide your own custom application.properties then change the java -jar command in Dockerfile to CMD java -jar app.jar --spring.config.additional-location=application.properties and change the docker run command to docker run -it -d -v application.properties:/application.properties -p 9090:9090 appimage:v1 where application.properties is the one which you provide from outside.
I think not much is missing. Or even better there may be even too many things.
First, you need to tell the docker maven plugin to run. The maven lifecycle defines which plugins run at what phase. So all other plugins need an execution configuration somewhere (in a parent pom or in yours). Second, there is no need to unpack the created jar file. Spring Boot will create an executable jar file automatically. You only need to tell the docker maven plugin about it (where it is created)
This would be the Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD target/${JAR_FILE} /usr/share/myapp.jar
ENTRYPOINT ["java","-jar","/usr/share/myapp.jar"]
And this your pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-spring-boot-docker</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<name>Spring Boot Docker</name>
<description>Getting started with Spring Boot and Docker</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath />
</parent>
<properties>
<docker.image.prefix>springio</docker.image.prefix>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- tag::plugin[] -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.9</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
<!-- end::plugin[] -->
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The unpack goal of the dependency plugin can be removed. This way the jar is added into the image and run directly. Hope it works!

Error: automatic module cannot be used with jlink: - Maven with JavaFX

I have selected Apache Commons IO, JSerialComm and Ini4J libraries via Maven repository.
But when I try to create an image via mvn javafx:jlink I get this errors:
[INFO] --- javafx-maven-plugin:0.0.2:jlink (default-cli) # JUSBPlotter ---
[WARNING] Required filename-based automodules detected. Please don't publish this project to a public artifact repository!
Error: automatic module cannot be used with jlink: ini4j from file:///root/.m2/repository/org/ini4j/ini4j/0.5.4/ini4j-0.5.4.jar
[ERROR] Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
at org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:447)
I seems it have something to do with this:
Error: automatic module cannot be used with jlink:
My module file looks like this:
module org.openjfx.JUSBPlotter {
requires javafx.controls;
requires javafx.fxml;
requires com.fazecast.jSerialComm;
requires ini4j;
requires org.apache.commons.io;
opens org.openjfx.JUSBPlotter to javafx.fxml;
exports org.openjfx.JUSBPlotter;
}
And my pom.xml looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openjfx</groupId>
<artifactId>JUSBPlotter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>0.5.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<stripDebug>true</stripDebug>
<compress>2</compress>
<noHeaderFiles>true</noHeaderFiles>
<noManPages>true</noManPages>
<launcher>JUSBPlotter</launcher>
<jlinkImageName>JUSBPlotter</jlinkImageName>
<jlinkZipName>JUSBPlotterZip</jlinkZipName>
<mainClass>org.openjfx.JUSBPlotter.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
So can it be that Apache Commons IO, JSerialComm and Ini4J is to old for Maven and Jlink?
How should I solve this problem?
I'm using Eclipse IDE with OpenJDK 11.
The jlink requires all dependencies to be modular. After generation, it generates a custom JRE image including the required modules. The ini4j seems non-modular.
For non-modular dependencies, you can go with the old Classpath approach after getting the custom JRE which has been generated without non-modular ones.
Briefly, run jlink excluding the non-modulars than add the jar files of non-modulars to the generated JRE image. The modules method and Classpath method can be combined this way.
A bit of fiddling with maven plugins should do this automatically.
Example for ini4j
Define some properties for convenience.
pom.xml
<properties>
<jlink-image-name>JUSBPlotter</jlink-image-name>
<ini4j-jar-name>ini4j.jar</ini4j-jar-name>
</properties>
Disable ini4j from module-info.java (It should be enable during development, only do this when you want to package the project)
module org.openjfx.JUSBPlotter {
requires javafx.controls;
requires javafx.fxml;
requires com.fazecast.jSerialComm;
//requires ini4j;
requires org.apache.commons.io;
opens org.openjfx.JUSBPlotter to javafx.fxml;
exports org.openjfx.JUSBPlotter;
}
Configure maven-dependency-plugin to copy the jar file of ini4j into the lib/ folder in jlink image.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<!-- Copy ini4j jar into the jlink image -->
<artifactItem>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>0.5.4</version>
<type>jar</type>
<destFileName>${ini4j-jar-name}</destFileName>
</artifactItem>
</artifactItems>
<!-- Set output directory to lib folder in jlink image -->
<outputDirectory>${project.build.directory}/${jlink-image-name}/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</plugin>
Configure jlink launcher option in the javafx-maven-plugin in order to add the jar file of non-modular ini4j to the Classpath.
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<stripDebug>true</stripDebug>
<compress>2</compress>
<noHeaderFiles>true</noHeaderFiles>
<noManPages>true</noManPages>
<launcher>JUSBPlotter</launcher>
<jlinkImageName>JUSBPlotter</jlinkImageName>
<mainClass>org.openjfx.JUSBPlotter.Main</mainClass>
<!-- ini4j jar file will be copied to the {image-folder}/lib/ folder. The launcher script should have this option to add it to the classpath -->
<options>-cp ../lib/${init4j-jar-name}</options>
</configuration>
</plugin>
Run:
mvn clean javafx:jlink
mvn package
cd target/JUSBPlotter/bin
./JUSBPlotter
maven-dependeny-plugin will copy the jar file when you run mvn package. But the jlink image must be already generated. So run the mvn javafx:jlink first. Then run mvn package.
Refer here to see how I applied for sqlite-jdbc in my project.

maven Error creating shaded jar: error in opening zip file

I am trying to build my dropwizard project using the following command :
mvn package
I am getting the following error :
Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.3:shade (default) on project rest-api: Error creating shaded jar: error in opening zip file /Users/ldelaney/.m2/repository/mysql/mysql-connector-java/5.1.36/mysql-connector-java-5.1.36.jar
I have checked, the jar is there, right where maven is looking.
I have also tried blowing it away, and running :
mvn clean
mvn package
But the error just will not go away. My IDE is not showing me any errors.
Also, here is my dependency in the POM :
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
Here is the entire POM file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.test</groupId>
<artifactId>rest-api</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<dropwizard.version>1.0.5</dropwizard.version>
<mongodriver.version>3.4.2</mongodriver.version>
<mysqlConnector.version>5.1.36</mysqlConnector.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<!-- Drop Wizard -->
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-auth</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-db</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-hibernate</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<!-- MYSQL and Hibernate -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysqlConnector.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
<!-- Log4j dependencies -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>application.ServerApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
Anyone have any ideas?
From the OP response, it looks like in .m2/repository/mysql-connector-java/5.1.36 there is only pom.xml file. Please do the following,
if you are using either nexus or artifactory repositories in your organization. delete mysql-connector coordinates and corresponding pom.xml file.
Ensure that your firewall is configured such that you can download file from here. Central repository is by default configured with maven installation.
Upgrade to maven 3 if you are on maven 2.
I just copied your pom and i was able to successfully build. I have no artifactory or nexus as well. Could you try following,
Go to your maven installation and specify a different repository location. You should go to /conf/settings.xml and uncomment and change <localRepository>/path/to/local/repo</localRepository> to a path. Make sure you do chmod +777 to that path.
If the problem persists, then manually download mysql-connector-java-5.1.36.jar and place it there. See if it works.
if 2 does not work, then try step 2 again but run mvn with -o flag. This is offline flag and maven will not download dependency.
If it does not work, can you show your maven output with -X flag,
mvn -U -X -e install
Is had this issue: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.1:shade (default) on project xxx: Error creating shaded jar: error in opening zip file /Users/username/.m2/repository/org/codehaus/groovy/groovy/2.2.0/groovy-2.2.0.jar -> [Help 1]
Resolution was rm -rf /Users/username/.m2/repository/org/codehaus/groovy/groovy/2.2.0 then it redownloaded it and started working again. If that helps.

Why does maven not copy the properties files during the build process?

Nothing I've found has been able to help me solve this one specific case. I recently switched from a plain old java web app project (which was working) to a maven web project. I get the following runtime exception:
java.util.MissingResourceException: Can't find bundle for base name com.myapp.config, locale en
I am using Netbeans to create a JSF 2.0, Spring, and Hibernate web app. I have the following directory structure:
src\main\java\com\myapp Contains config.properties
src\main\resources Empty
target\myapp\WEB-INF\classes\com\myapp Contains compiled class files without config.properties
src\main\java\com\myapp Contains config.properties
Inspection of the WAR file in the target folder does not show any sign of the properties file so it's as if the Maven build plug-in is not copying over properties files. I know there is a tag you can place inside the pom but it didn't work for me. The link below mentions that the resources folder (empty for me) has its contents included during the build but if that is the case, how do you do it from Netbeans? I just want the properties file to be packaged with my war so it is accessible when it is deployed to the server.
http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>java.net</id>
<name>Repository hosting the Java EE 6 artifacts</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>net.authorize</groupId>
<artifactId>java-anet-sdk</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
<finalName>${artifactId}</finalName>
</build>
<profiles>
<profile>
<id>endorsed</id>
<activation>
<property>
<name>sun.boot.class.path</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- javaee6 contains upgrades of APIs contained within the JDK itself.
As such these need to be placed on the bootclasspath, rather than classpath of the
compiler.
If you don't make use of these new updated API, you can delete the profile.
On non-SUN jdk, you will need to create a similar profile for your jdk, with the similar property as sun.boot.class.path in Sun's JDK.-->
<compilerArguments>
<bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties>
<netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
</properties>
Maven doesn't copy resources from the java source tree by default, but you can get it do that by adding this to your pom.xml:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes><exclude>**/*.java</exclude></excludes>
</resource>
</resources>
</build>
Make sure you exclude the java source files.
From https://rogerkeays.com/how-to-change-mavens-default-resource-folder
What is your project's build path configured to be in Netbeans? You might try changing it to src/main/webapp/WEB-INF/classes. This way class files compiled from your src/main/java folder and any resources you have under src/main/resources should get included in the generated WAR. You would then be able to access your config.properties file if you place it under the src/main/resources folder.
You might also review any includes sections in your pom.xml and ensure you're not accidentally excluding something (if you explicitly include some things, you're likely implicitly excluding everything else).
By default maven will include all files under resources folder. If your properties files are not in the resource folder, then you need to include the following in the pom.xml file under the build section.
<build>
/* other tags like <plugins> goes here */
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
/* other tags like <plugins> goes here */
</build>
Try putting your config.properties under src\main\resources\com\myapp. I was able to test this on a local project. I'm running Maven 3.0.2.
Created a mvn sample project with the webapp archetype:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
Created a directory at src/main/resources/com/foo and put a foo.properties file under it.
Ran a build:
mvn clean install
Then, when looking in the resulting target directory, the foo.properties file appears:
ls -al target/my-webapp/WEB-INF/classes/com/foo/
-rw-r--r-- 1 sblaes staff 4 Apr 2 22:09 foo.properties
You might try those steps on your machine. If that works, then start trying to simplify your POM above by removing things from it to see if it starts working. Trial and error is no fun, but I just don't see anything above that should be breaking it.
Huge gotcha for this:
when your resources are in "test/resources" (e.g. .properties files for tests)
maven doesn't copy them to target, so they're not in the classpath
Check whether your "packaging" is set to "pom" in the pom.xml:
<packaging>pom</packaging>
Fix is:
change your packaging to "jar" or "war" instead

Resources