search
HomeJavajavaTutorialHow to use the jib plug-in to build an image of a Java application

Introduction

Containerization of Java application development requires the production of docker images. If the user does not have docker installed locally, he can use jib to create a tar file with an image.

Jib provides four ways to use:

  • Maven plug-in: jib-maven-plugin

  • Gradle plug-in: jib -gradle-plugin

  • Java library: Jlib Core

  • Jib CLI

jib build tool It mainly contains four powerful functions.

  • build: Provides the function of creating an image and pushing it to a remote warehouse.

  • buildTar: Provides the function of creating a tar file containing an image.

  • dockerBuild: Provides the function of creating a docker image locally.

  • exportDocker: rContext provides the function of creating dockerfile.

When compiling and building without a docker environment, you cannot use the build command and dockerBuild command to create an image. You can only use the buildTar command to create a tar file containing the image. .

Use the Maven plug-in to build Java applications as docker images

1. Modify the project code that needs to be mirrored: find the pom file to declare the jib plug-in, and
declare the Jib plug-in in the pom.xml file :

<!--使用jib插件-->
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>1.3.0</version>
                <configuration>
                    <!--from节点用来设置镜像的基础镜像,相当于Docerkfile中的FROM关键字-->
                    <from>
                        <!--使用openjdk官方镜像,tag是8-jdk-stretch,表示镜像的操作系统是debian9,装好了jdk8-->
                        <image>openjdk:8-jdk-stretch</image>
                    </from>
                    <to>
                        <!--镜像名称和tag,使用了mvn内置变量${project.version},表示当前工程的version-->
                        <image>lendea/hellojib:${project.version}</image>
                    </to>
                    <!--容器相关的属性-->
                    <container>
                        <!--jvm内存参数-->
                        <jvmFlags>
                            <jvmFlag>-Xms256m</jvmFlag>
                            <jvmFlag>-Xmx256m</jvmFlag>
                        </jvmFlags>
                        <!--要暴露的端口-->
                        <ports>
                            <port>8081</port>
                        </ports>
                    </container>
                </configuration>
            </plugin>
  • from tag: Set the base image, which is equivalent to the FROM keyword in the dockerfile. It is recommended to use the image in SWR. The download speed is fast and stable during construction.

  • to tag: Set the image name and tag of the created image.

  • container tag: Set container-related properties, jvm memory parameters, ports, etc.

2. Create a build task and execute it

# -Dmaven.test.skip=true: 跳过单元测试
# -U: 每次构建检查以来更新,可避免缓存中快照版本依赖不更新问题,但会牺牲部分性能
# -e -X: 打印调试信息,定位疑难构建问题时建议使用此参数构建
# -B: 以batch模式运行,可避免日志打印出现ArrayIndexOutOfBoundsException异常

mvn compile jib:buildTar -Dmaven.test.skip=true -U -e -X -B

In the target directory of the java project, you can see the generated tar image.

3. Use tar image
Execute the docker load -i xxx.tar command to load the tar file image into the local image warehouse, and then use docker run --rm -p 8081:8081 lendea/hellojib:0.0.1 and other commands start the container to test the function.

The above is the detailed content of How to use the jib plug-in to build an image of a Java application. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.