Java: 使用Jib Maven插件打包Docker镜像

java标志
image-3690

参考:

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
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <!-- 拉取所需的基础镜像 - 这里的jdk8,是用于运行springboot项目 -->
                    <!--from节点用来设置镜像的基础镜像,相当于Docerkfile中的FROM关键字-->
                    <from>
                        <!-- 建议使用swr公开镜像,下载速度快,更稳定 -->
                        <image>openjdk:19-jdk</image>
                    </from>
                    <!-- 最后生成的镜像配置 -->
                    <to>
                        <!-- push docer-hub官方仓库。用户名/镜像名:版本号 -->
                        <image>xxx.aliyuncs.com/xxx</image>
                        <!-- 如果是阿里云的容器镜像仓库,则使用容器的配置 前缀/命名空间/仓库名 -->
                        <tags>
                            <!--版本号-->
                            <tag>${project.version}</tag>
                        </tags>
                        <auth>
                            <username>xxx@xxx.com</username>
                            <password>xxx</password>
                        </auth>
                    </to>
                    <container>
                        <!-- springboot项目的入口类 -->
                        <mainClass>cn.test.spring3demo.SpringDemoApplication</mainClass>
                        <ports>
                            <!-- 指定镜像端口 , 这里没用docfile的操作 -->
                            <port>8082</port>
                        </ports>
                    </container>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>19</source>
                    <target>19</target>
                    <compilerArgs>--enable-preview</compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>