# 010基础镜像重构之Java11Maven编译镜像

# 构建工具选型(来自章节d008)

# kenaito-maven3:jdk11

镜像大小:708MB 构建耗时:17.8s(不包括下载环境包的时间)

# 下载环境
if [ ! -f "Alibaba_Dragonwell_Standard_11.0.27.23.6_x64_linux.tar.gz" ];then
  curl -O https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.27.23.6/Alibaba_Dragonwell_Standard_11.0.27.23.6_x64_linux.tar.gz
fi
if [ $? -ne 0 ]; then
  echo "下载 Alibaba_Dragonwell_Standard_11.0.27.23.6_x64_linux.tar.gz 文件失败"
  exit 1
fi

if [ ! -f "apache-maven-3.9.11-bin.tar.gz" ];then
  curl -O https://dlcdn.apache.org/maven/maven-3/3.9.11/binaries/apache-maven-3.9.11-bin.tar.gz
fi
if [ $? -ne 0 ]; then
  echo "下载 apache-maven-3.9.11-bin.tar.gz 文件失败"
  exit 1
fi

# 释放文件,优化大小
rm -rf build
mkdir build
tar -xvf Alibaba_Dragonwell_Standard_11.0.27.23.6_x64_linux.tar.gz
if [ $? -ne 0 ]; then
  echo "初始化失败"
  exit 1
fi
tar -xvf apache-maven-3.9.11-bin.tar.gz
if [ $? -ne 0 ]; then
  echo "初始化失败"
  exit 1
fi
rm -f dragonwell-11.0.27.23+6-GA/lib/src.zip
mv dragonwell-11.0.27.23+6-GA build/jdk
mv apache-maven-3.9.11 build/maven
cd build && pwd

# maven配置(使用单引号 'EOF' 可以防止变量被提前解释)
cat > maven/conf/settings.xml << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<settings
        xmlns="http://maven.apache.org/SETTINGS/1.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>/home/admin/repository</localRepository>
    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>snapshots</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </pluginRepository>
                <pluginRepository>
                    <id>snapshots</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <mirrors>
        <!-- 阿里云仓库 -->
        <mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        </mirror>
        <!-- 中央仓库1 -->
        <mirror>
            <id>repo1</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo1.maven.org/maven2/</url>
        </mirror>
        <!-- 中央仓库2 -->
        <mirror>
            <id>repo2</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo2.maven.org/maven2/</url>
        </mirror>
        <mirror>
            <id>mirror</id>
            <mirrorOf>!rdc-releases,!rdc-snapshots</mirrorOf>
            <name>mirror</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
    </mirrors>
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
</settings>
EOF

# 压缩成品
tar -czvf jdk.tgz jdk
tar -czvf maven.tgz maven
rm -rf jdk
rm -rf maven

## Maven环境配置
#cat > maven.sh << 'EOF'
#export MAVEN_HOME=/home/admin/maven
#export PATH=${MAVEN_HOME}/bin:$PATH
#EOF
#
## Jdk环境配置
#cat > jdk.sh << 'EOF'
#export JAVA_HOME=/home/admin/jdk
#export PATH=${JAVA_HOME}/bin:$PATH
#EOF

# Dockerfile
## CMD用于定义默认命令
cat > Dockerfile << 'EOF'
FROM alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3:latest
LABEL MAINTAINER="tianjun@odboy.cn"
ENV TimeZone="Asia/Shanghai"
ENV JAVA_HOME="/home/admin/jdk"
ENV MAVEN_HOME="/home/admin/maven"
ENV PATH="$PATH:${JAVA_HOME}/bin:${MAVEN_HOME}/bin"
WORKDIR /home/admin
ADD jdk.tgz .
ADD maven.tgz .
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone; \
sed -i 's/mirrors.cloud.aliyuncs.com/mirrors.aliyun.com/' /etc/yum.repos.d/*.repo; \
mkdir /home/admin/repository; \
chmod +x /home/admin/jdk/bin/*; \
chmod +x /home/admin/maven/bin/*;
VOLUME /home/admin/repository
CMD ["sleep","365d"]
EOF

# 构建镜像(docker可换成buildah)
docker build --no-cache -f Dockerfile -t kenaito-maven3:jdk11 .
if [ $? -ne 0 ]; then
  echo "构建镜像失败"
  exit 1
fi

# 运行测试
docker run kenaito-maven3:jdk11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
最近更新: 2025-09-04
010基础镜像重构之Java11Maven编译镜像

2017 - 武林秘籍   |