dockerfile-maven 打包
pom.xml
加到 这一段代码后面
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<bootclasspath>${java.home}/lib/rt.jar:${java.home}/lib/charsets.jar:${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.5</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
<prefix>git</prefix>
<verbose>false</verbose>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
<format>json</format>
<gitDescribe>
<skip>false</skip>
<always>false</always>
<dirty>-dirty</dirty>
</gitDescribe>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<excludeProperties>
<excludeProperty>git.user.*</excludeProperty>
<excludeProperty>git.remote.*</excludeProperty>
<excludeProperty>git.build.*</excludeProperty>
</excludeProperties>
<injectAllReactorProjects>true</injectAllReactorProjects>
</configuration>
</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>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>register-domain-name/group-name/project-name</repository>
<tag>${project.version}.${git.commit.id.abbrev}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
<WAR_FILE>${project.build.finalName}.war</WAR_FILE>
</buildArgs>
</configuration>
</plugin>
mvn package # 打包 war/jar ,并打包 docker image
docker push register-domain-name/group-name/project-name:tag-name # push image
## mvn deploy # 需要提前配置 install
注意:
- 镜像名称要全部小写,不然会报错 I/O exception https://github.com/spotify/dockerfile-maven/issues/123
- 需要在项目中准备Dockfile
ref:
https://itnext.io/tag-your-docker-images-while-building-with-maven-915c8043d2e0
jib 打包
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>0.10.0</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<from><image>openjdk:8-jre-alpine</image></from>
<to>
<image>domain/group-name/project-name</image>
<auth>
<username>${env.REGISTRY_USERNAME}</username>
<password>${env.REGISTRY_PASSWORD}</password>
</auth>
<tags>
<tag>${project.version}-${git.commit.id.abbrev}</tag>
<tag>latest</tag>
</tags>
</to>
<allowInsecureRegistries>true</allowInsecureRegistries>
<container>
<!-- <args>WAR_FILE,JAR_FILE</args> -->
<appRoot>/usr/local/tomcat/webapps/${project.build.finalName}</appRoot>
<ports>
<port>8080</port>
<port>8089</port>
</ports>
<entrypoint>
<arg>/bin/sh</arg>
<arg>-c</arg>
<arg>catalina.sh run</arg>
</entrypoint>
</container>
</configuration>
</plugin>
jar 包自定义 entrypoint
<entrypoint>
<arg>/bin/sh</arg>
<arg>-c</arg>
<arg>java ${JAVA_OPTS} -cp /app/resources/:/app/classes/:/app/libs/*
com.example.JibDemoApplication</arg>
</entrypoint>
以上配置中, <phase>install</phase> ,只有在 mvn install 时才进行docker 镜像打包及上传。 mvn package 只进行war/jar 的打包。
#mvn compile jib:dockerBuild # 打包本地镜像
#mvn clean compile package jib:dockerBuild # 打包本地镜像并上传到仓库
mvn install # 打包本地镜像并上传到仓库
网友评论