Docker与Maven的整合

作者: 一曲畔上 | 来源:发表于2019-03-05 09:41 被阅读17次

目前docker与maven的使用都是非常广泛的,所以二者结合使用,将是极大的提高效率。借助maven组件dockerfile-maven-plugin可以实现二者的结合。配置代码如下:

1,参数定义

<properties>
    <dockerfile.skip>false</dockerfile.skip>
    <docker.repostory>xxxxxx.tencentyun.com</docker.repostory>
    <docker.repostory.user>xxxxxxxx</docker.repostory.user>
    <docker.repostory.password>xxxxxxxxx</docker.repostory.password>
    <docker.registry.name>demotest</docker.registry.name>
    <docker.image.name>demo</docker.image.name>
</properties>

2,时间戳定义

由于maven自身的问题,无法实现带有时区的时间戳,因此需要借助其他组件实现

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>timestamp-property</id>
            <goals>
                <goal>timestamp-property</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <name>current.time</name>
        <pattern>yyyyMMdd.HHmmss</pattern>
        <timeZone>GMT+8</timeZone>
    </configuration>
</plugin>

我们定义了一个名称是current.time格式是yyyyMMdd.HHmmss的东八区时间戳。
3,定义docker配置

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
        <!-- 在build阶段创建image -->
        <execution>
            <id>build-image</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
            <configuration>
                <tag>latest</tag>
            </configuration>
        </execution>
        <!-- 在build和package阶段创建tag -->
        <execution>
            <id>tag-image</id>
            <phase>package</phase>
            <goals>
                <goal>tag</goal>
            </goals>
            <configuration>
                <tag>${current.time}</tag>
            </configuration>
        </execution>
        <!-- 在deploy阶段发布image到远程仓库 -->
        <execution>
            <id>push-image-version</id>
            <phase>deploy</phase>
            <goals>
                <goal>push</goal>
            </goals>
            <configuration>
                <tag>${current.time}</tag>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <skip>${dockerfile.skip}</skip>
        <username>${docker.repostory.user}</username>
        <password>${docker.repostory.password}</password>
        <useProxy>false</useProxy>
        <buildArgs>
            <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
        <repository>${docker.repostory}/${docker.registry.name}/${docker.image.name}</repository>
        <contextDirectory>${project.build.directory}/${project.name}</contextDirectory>
    </configuration>
</plugin>

4,使用方式

4.1,本地创建image和tag:

    mvn clean install

或者

    mvn clean package

二者均可

4.2,发布image到远端仓库

    mvn clean deploy

4.3,如果在maven任何阶段都不想执行docker操作可以添加参数

-Ddockerfile.skip=true

mvn clean package -Ddockerfile.skip=true

-End-

相关文章

网友评论

    本文标题:Docker与Maven的整合

    本文链接:https://www.haomeiwen.com/subject/inaguqtx.html