maven 打包配置
本文是项目 spring-boot 项目. 使用 spring-start 生成的事例项目
修改 maven 中的 build 节点, 增加 antrun 插件, 如下:
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 解压fat jar到target/${project-name}目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<unzip
src="${project.build.directory}/${project.build.finalName}.${project.packaging}"
dest="${project.build.directory}/${project.artifactId}"/>
<zip destfile="${project.build.directory}/${project.artifactId}.zip">
<fileset dir="${project.build.directory}/${project.artifactId}"/>
</zip>
<tar compression="gzip" destfile="${project.build.directory}/${project.artifactId}.tgz">
<fileset dir="${project.build.directory}/${project.artifactId}"/>
</tar>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 注:
unzip
主要是把打包的fat jar
解压到target/${project.build.finalName}
目录下zip
将第一步解压出来的再重新打包成 zip 压缩文件tar
将第一步解压出来的重新打包成 tgz 压缩文件
如果Dockerfile 文件使用的 COPY 文件把文件加入到 Docker中, 所以用不到2,3步骤.仅需要第1步.
编写 Dockerfile 文件内容
FROM openjdk:8-jdk-alpine
ARG MIRROR=true
ARG APP=项目名称
ARG APP_PATH=target/${APP}
ENV MAVEN_HOST=https://repo1.maven.org/maven2 \
ALPINE_HOST=dl-cdn.alpinelinux.org \
MIRROR_MAVEN_HOST=https://maven.aliyun.com/repository/public \
MIRROR_ALPINE_HOST=mirrors.aliyun.com
# if use mirror change to aliyun mirror site
RUN if $MIRROR; then MAVEN_HOST=${MIRROR_MAVEN_HOST} ;ALPINE_HOST=${MIRROR_ALPINE_HOST} ; sed -i "s/dl-cdn.alpinelinux.org/${ALPINE_HOST}/g" /etc/apk/repositories ; fi && \
# https://github.com/docker-library/openjdk/issues/76
apk add --no-cache tini && \
apk add --no-cache busybox-extras && \
apk add --no-cache curl && \
apk add --no-cache bash && \
apk add --no-cache jq && \
wget "https://arthas.aliyun.com/arthas-boot.jar" -qO /opt/arthas-boot.jar && \
echo "java -jar /opt/arthas-boot.jar --repo-mirror aliyun" > /usr/local/bin/arthas && \
chmod a+x /usr/local/bin/arthas
COPY ${APP_PATH} /app/${APP}
WORKDIR /app/${APP}
ENTRYPOINT ["/sbin/tini", "--", "java","org.springframework.boot.loader.JarLauncher"]
- 说明:
- jq 是 json 格式化工具
- 下载 arthas-boot.jar 到指定目录中, 并生成 启动脚本到 bin目录下
/sbin/tini --
主要是不让 java 使用进程pid=1. 要不然就没办法使用了. 详见, 原理是一样的.
创建镜像
docker build -t aaa .
运行
交互方式运行
docker run -it --rm --name test aaa /bin/sh
后台方式运行
docker run -d --name test aaa /bin/sh
测试 arthas
运行成功, 可以愉快的玩arthas了.