20 Eylül 2021 Pazartesi

spring-boot-maven-plugin build-image Goal - Docker Image

Giriş
Açıklaması şöyle
Since Spring Boot 2.3, Spring Boot Maven plugin has introduced a build-image goal which is using Cloud Native Buildpacks to build a Docker image in OCI image format.
İki tane yöntem var
1.Buildpacks Yöntemi
2. Layered Jar Yöntemi

1. Buildpacks Yöntemi
Açıklaması şöyle
The easiest way to containerize a Spring Boot app is to use buildpacks. Since Spring Boot 2.3.0.M1, buildpack support is built directly into the framework. You do not need to create a Dockerfile. Make sure you have the docker daemon running locally.

Simply run the command:

./mvnw spring-boot:build-image
A container image gets created where the image name will be the application name from the pom.xml file and the image version will be the version from the pom.xml file.
Yani şöyle yaparız
$ ./mvnw spring-boot:build-image
spring-boot.build-image.imageName Tag
Örnek
Açıklaması şöyle
By default, Spring Boot Maven plugin will use our project artifactId as the image name and version as the image tag. ... Spring Boot Maven plugin allows us to easily change the build image name using a spring-boot.build-image.imageName property.
Şöyle yaparız
<properties>
  <java.version>11</java.version>
  <spring-boot.build-image.imageName>andylke/${project.artifactId}:${project.version}</spring-boot.build-image.imageName>
</properties>
image/name tag
Örnek
Şöyle yaparız. Burada image ismi veriliyor.
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <image>
      <name>ayazmustafa/student-project</name>
    </image>
  </configuration>
</plugin>
2. Layered Jar Yöntemi
Layered Jar yapısı şöyle
META-INF/
  MANIFEST.MF
org/
  springframework/
    boot/
      loader/
        ...
BOOT-INF/
  layers/
    <name>/
      classes/
        ...
      lib/
        ...
    <name>/
      classes/
        ...
      lib/
        ...
  layers.idx
Açıklaması şöyle
You still see the bootstrap loader classes (you can still run java -jar) but now the lib and classes folders have been split up and categorized into layers. There’s also a new layers.idx file that provides the order in which layers should be added.

Initially, we’re providing the following layers out-of-the box:

 -dependencies (for regular released dependencies)

 -snapshot-dependencies (for snapshot dependencies)

 -resources (for static resources)

 -application (for application classes and resources)

This layering is designed to separate code based on how likely it is to change between application builds. Library code is less likely to change between builds, so it is placed in its own layers to allow tooling to re-use the layers from cache. Application code is more likely to change between builds so it is isolated in a separate layer.
Örnek - layered jar
Şöyle yaparız
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <layout>LAYERED_JAR</layout>
  </configuration>
</plugin>
Örnek - Customer layer
Şöyle yaparız
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <excludes>
      <exclude>
        <groupId>org.projectlombok</groupId>
        <artifacdId>lombok</artifacdId>
      </exclude>
    </excludes>
    <layers>
      <enabled>true</enabled>
      <configuration>${project.basedir}/src/layers.xml</configuration>
    </layers>
  </configuration>
</plugin>

Hiç yorum yok:

Yorum Gönder