我的应用程序使用资源文件夹中的一些附加文件。当我使用 Maven 进行常规构建并将我的应用程序打包到 JAR 中时,如果我解压缩此存档,我可以看到所有资源。
但是当我创建 docker 映像时,我使用 spring-boot-maven-plugin 使我的 JAR 存档可执行。由于某种原因,我的资源没有添加到新的重新打包的 JAR 中。此外,我什至无法解压缩它,因为它已损坏。
这是我在 pom.xml 中设置重新打包目标的方法:
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.3.RELEASE</version>
<configuration>
<mainClass>ApplicationName</mainClass>
<executable>true</executable>
<arguments>
<argument>--spring.profiles.active=prod</argument>
</arguments>
<!--<addResources>true</addResources>-->
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<!--<includeSystemScope>true</includeSystemScope>-->
<!--<layout>JAR</layout>-->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
我想补充一点,当我在 docker 中运行我的映像时,它工作正常,但缺少所有所需的资源。
有人遇到同样的问题吗?或者您可以建议如何修复它。
maven docker spring-boot spring-boot-maven-plugin docker-maven-plugin
我从孙子 pom访问parent.parent.version时遇到问题。
这是问题的确切描述:
父pom:
<groupId>com.parent</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.6-SNAPSHOT</version>
.
.
.
<properties>
<child.version>1.3-SNAPSHOT</child.version>
</properties>
Run Code Online (Sandbox Code Playgroud)
子 pom:
<parent>
<groupId>com.parent</groupId>
<artifactId>parent</artifactId>
<version>1.6-SNAPSHOT</version>
</parent>
<groupId>com.child</groupId>
<artifactId>child</artifactId>
<packaging>pom</packaging>
<version>${child.version}</version>
Run Code Online (Sandbox Code Playgroud)
孙子 pom:
<parent>
<groupId>com.child</groupId>
<artifactId>child</artifactId>
<version>${child.version}</version>
</parent>
<groupId>com.grandchild</groupId>
<artifactId>grandchild</artifactId>
<packaging>pom</packaging>
<version>${project.parent.version}</version>
.
.
.
<profiles>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>artifactory:6001/${project.name}:${parent.parent.version}</imageName>
<serverId>docker</serverId>
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</profile>
Run Code Online (Sandbox Code Playgroud)
每当我构建这个项目时,我都会遇到以下异常:
无法在项目孙子上执行目标 com.spotify:docker-maven-plugin:1.0.0:build (default):捕获异常:模板变量 'parent.parent.version' 没有值 -> [帮助 1]
如何从孙子pom.xml中获取parent.parent.version(主项目版本)?
Docker 版本应与主项目版本相同。这就是我的目标!
摘自Spring Microservices in Action一书:我正在尝试使用Docker Maven插件来构建Docker映像,以将Java微服务作为Docker容器部署到云中。
Dockerfile:
FROM openjdk:8-jdk-alpine
RUN mkdir -p /usr/local/configserver
ADD jce_policy-8.zip /tmp/
RUN unzip /tmp/jce_policy-8.zip && \
rm /tmp/jce_policy-8.zip && \
yes | cp -v /tmp/UnlimitedJCEPolicyJDK8/*.jar /usr/lib/jvm/java-1.8-openjdk/jre/lib/security/
ADD @project.build.finalName@.jar /usr/local/configserver/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
Run Code Online (Sandbox Code Playgroud)
与Dockerfile中的第4步相关的输出:
Run Code Online (Sandbox Code Playgroud)... ---> Using cache ---> dd33d4c12d29 Step 4/8 : RUN unzip /tmp/jce_policy-8.zip && rm /tmp/jce_policy-8.zip && yes | cp -v /tmp/UnlimitedJCEPolicyJDK8/*.jar /usr/lib/jvm/java-1.8-openjdk/jre/lib/security/ ---> Running in 1071273ceee5 Archive: /tmp/jce_policy-8.zip …
我有一个包含多个public static void main( psvm) 的 .jar,我希望在对docker run ... -e <class.path.from.env>图像执行操作并传递环境变量来指定类路径时能够调用它们。像这样的东西:
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<name>${project.artifactId}</name>
<build>
<from>java:8-jre</from>
<tags>
<tag>${build.environment}-latest</tag>
<tag>${build.environment}-${build.number}</tag>
</tags>
<entryPoint>
<exec>
<arg>java</arg>
<arg>-Duser.timezone=UTC</arg>
<arg>-cp</arg>
<arg>/opt/${project.artifactId}-${project.version}.jar</arg>
<arg>${class.path.from.env}</arg>
</exec>
</entryPoint>
<assembly>
<basedir>/opt</basedir>
<inline>
<files>
<file>
<source>target/${project.artifactId}-${project.version}.jar</source>
</file>
</files>
</inline>
</assembly>
</build>
</image>
</images>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
尽管我阅读了 的整个文档docker-maven-plugin,但我不确定如何才能完成这项工作。基本上我在哪里声明环境变量class.path.from.env以及如何确保它获得我传递的-e环境变量docker run ...?
我正在尝试使用 Google Cloud Build 来构建我的 Java 应用程序。它允许使用所谓的云构建器 - 不同构建器的 docker 镜像。我正在使用 Maven。所以问题是我必须使用私有存储库(artifactory)来部署工件。该代表受密码保护,我不知道如何将这些凭据传递给 GC maven docker 容器。
我发现唯一可能的方法是:
运行 shell 脚本,该脚本将更新 Maven 容器 settings.xml,如下所示:
<servers>
<server>
<id>myRepoName</id>
<username>${server.username}</username>
<password>${server.password}</password>
</server>
</servers>
Run Code Online (Sandbox Code Playgroud)在 cloudbuild.yml 中设置环境变量
还有其他优雅的方法来实现我想要的目标吗?
maven docker google-cloud-platform docker-maven-plugin google-cloud-build
使用以下插件
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33</version
Run Code Online (Sandbox Code Playgroud)
并使用以下配置(只需在此处发布相关位)
<configuration>
<verbose>build</verbose>
<images>
<image>
<name>${container.imageNameWithTag}</name>
<build>
<labels>
<dummy.label>dummyLabelValue</dummy.label>
</labels>
<contextDir>${project.basedir}/src/main/docker</contextDir>
<assembly>some required assembly </assembly>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>docker-build</id>
<goals>
<goal>build</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
Run Code Online (Sandbox Code Playgroud)
但最终的图像只有这些标签
"Labels": {
"org.label-schema.build-date": "20181204",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
Run Code Online (Sandbox Code Playgroud)
我认为来自centos基础图像,但没有dummy.label
我是否缺少任何配置,或者任何配置错误?
该插件的文档位于Maven Docker Plugin
我在 pom.xml 中配置了 Fabric8 docker-maven-plugin,如下所示:
<build>
...
<plugins>
...
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<images>
<image>
<name>${docker.image.prefix}/${project.artifactId}:%l</name>
<build>
<dockerFile>Dockerfile</dockerFile>
<assembly>
<descriptorRef>artifact</descriptorRef>
</assembly>
</build>
</image>
</images>
</configuration>
</plugin>
...
</plugins>
...
</build>
Run Code Online (Sandbox Code Playgroud)
我使用占位符,如果版本包含 ,则用标签%l标记图像,否则使用 pom 版本。当从 CI 构建时,我想在我的图像中包含一些额外的标签(可能不止一个)(例如构建号/分支名称),但我想保留占位符行为。我认为应该可以从命令行使用 Maven 属性,但我无法从插件文档中找出它(https://dmp.fabric8.io/)latest-SNAPSHOT%l
在执行 docker:build 目标时如何包含附加标签?
我正在使用 spotify 创建 docker 图像。我的 docker 镜像创建成功,但没有 oa 名称。我在控制台下面:
图像将在没有名称的情况下构建
POM文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nv</groupId>
<artifactId>microeurekaserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MicroEurekaServer</name>
<description>Eureka Server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
<docker.image.prefix>nvarshney44/nvarshney</docker.image.prefix>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<forceTags>true</forceTags>
<imageTags>
<imageTag>${project.version}</imageTag>
<imageTag>latest</imageTag>
</imageTags>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
请帮我看看它有什么问题。在 maven 输出中,它显示 dockerfile:null 可能是因为它正在花费一些问题。
docker spring-boot spotify-docker-client docker-maven-plugin
[错误] 无法在项目 Bookstore 上执行目标 com.spotify:docker-maven-plugin:1.0.0:build (default):捕获异常:java.util.concurrent.ExecutionException:com.spotify.docker.client.shaded。 javax.ws.rs.ProcessingException: org.apache.http.conn.HttpHostConnectException: 连接到 localhost:2375 [localhost/127.0.0.1] 失败: 连接被拒绝
为什么这个异常不断发生,我该如何解决它,我正在从 war 文件构建 docker 图像。
pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>net.codejava.javaee.bookstore</groupId>
<artifactId>Bookstore</artifactId>
<version>1.2.1</version>
<packaging>war</packaging>
<properties>
<docker.image.prefix>alesblaze</docker.image.prefix>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<finalName>BookStore</finalName>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>Docker</dockerDirectory>
<dockerHost>https://localhost:3000</dockerHost>
<forceTags>true</forceTags> …Run Code Online (Sandbox Code Playgroud) 将fabric8添加到hello world pom后,在CentOS7上的Jenkins中运行maven“clean install”时,
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.31.0</version>
<configuration>
<filter>${*}</filter>
<images>
<image>
<name>docker.io/myname/${project.artifactId}:${project.version}</name>
</image>
</images>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
[ERROR] Failed to execute goal io.fabric8:docker-maven-plugin:0.31.0:build (default) on project reference-service: Execution default of goal io.fabric8:docker-maven-plugin:0.31.0:build failed: An API incompatibility was encountered while executing io.fabric8:docker-maven-plugin:0.31.0:build: java.lang.UnsatisfiedLinkError: could not load FFI provider jnr.ffi.provider.jffi.Provider
[ERROR] -----------------------------------------------------
[ERROR] realm = plugin>io.fabric8:docker-maven-plugin:0.31.0
Run Code Online (Sandbox Code Playgroud)
...
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import from realm ClassRealm[maven.api, parent: null]]
[ERROR]
[ERROR] …Run Code Online (Sandbox Code Playgroud) docker ×8
maven ×6
fabric8 ×2
java ×2
spring-boot ×2
alpine-linux ×1
centos7 ×1
dockerfile ×1
maven-3 ×1
maven-plugin ×1
parent-pom ×1
pom.xml ×1
unzip ×1