This is the 17th day of my participation in Gwen Challenge

introduce

Let’s first introduce some nouns in the title:

  • Spring Boot: Don’t look at me if you don’t know me
  • Maven: The most common Java package manager today
  • Docker: Understood as an integrated environment, can run projects directly, no need to manually install various dependencies
  • Harbor: Private DockerHub

This article focuses on documenting and explaining how to package the Spring Boot project using jiB-Maven-plugin

By default, you are familiar with Spring Boot and Maven configurations and understand Docker concepts

Adding a publishing configuration

Our project will be published in a private repository, so first modify the POM file and add the related configuration, POM.xml

<properties>
    <release.url>http://Houtaroy/repository/releases/</release.url>
    <snapshot.url>http://Houtaroy/repository/snapshot/</snapshot.url>
</properties>

<distributionManagement>
    <repository>
      <id>releases</id>
      <name>Release Repository</name>
      <url>${release.url}</url>
    </repository>
    <snapshotRepository>
      <id>snapshots</id>
      <name>Snapshot Repository</name>
      <url>${snapshot.url}</url>
    </snapshotRepository>
</distributionManagement>
Copy the code

Use the jib – maven plugin

Our ultimate goal is to package the project as a Docker image. After completing the release configuration, jiB-Maven-plugin packaging plug-in needs to be introduced

Modify pom.xml as well:

<properties>
    <maven-jib-plugin.version>2.5.2</maven-jib-plugin.version>
    <docker.from.image>repository.harbor.com/library/java8-cn:latest@sha256:xxxx</docker.from.image>
    <docker.repository>repository.harbor.com/test</docker.repository>
</properties>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>${maven-jib-plugin.version}</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <from>
                        <image>${docker.from.image}</image>
                    </from>
                    <to>
                        <image>${docker.repository}/${project.artifactId}:${project.version}</image>
                    </to>
                    <container>
                        <creationTime>USE_CURRENT_TIMESTAMP</creationTime>
                        <jvmFlags>
                            <jvmFlag>-Duser.timezone=GMT+8</jvmFlag>
                        </jvmFlags>
                        <volumes>
                            <volume>/app/config</volume>
                            <volume>/app/data</volume>
                        </volumes>
                        <workingDirectory>/app</workingDirectory>
                    </container>
                    <extraDirectories>
                        <paths>
                            <path>${project.basedir}/target/extra-directory/</path>
                        </paths>
                    </extraDirectories>
                    <allowInsecureRegistries>true</allowInsecureRegistries>
                    <containerizingMode>packaged</containerizingMode>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
Copy the code

Note the following configuration parameters:


: Basic image, the project is based on Java8 environment, so use java8-cn:latest@sha256: XXXX

Note here that we need to specify the image’s digest, which can be viewed in Harbor:


: Package path to which the project image will be published

Container: Container-related configuration, for example, I have mounted the /app directory

Adding Maven Authentication

After the above configuration, we only need to add Maven private server and Harbor user authentication to the push image

MavenHome%/conf/setting.xml: / MavenHome%/conf/setting.xml:

<servers>
    <server>
        <id>releases</id>
        <username>admin</username>
        <password>123456</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>admin</username>
        <password>123456</password>
    </server>
    <server>
        <id>repository.harbor.com</id>
        <username>admin</username>
        <password>123456</password>
    </server>
</servers>
Copy the code

To do this, simply execute Maven’s deploy command to package Spring Boot as a Docker image and publish it to Harbor

conclusion

Finally, comb through the process:

  1. Have Maven repository and Docker repository, and have the base image of the project to run
  2. Modified POM file to add release configuration
  3. Modified POM files are packaged using jiB-Maven-plugin
  4. Modify Maven setting. XML to add repository user authentication
  5. Run the deploy command for Maven