13 Aralık 2023 Çarşamba

spring-boot-maven-plugin repackage Goal - Fat Jar Oluşturur

Giriş
Spring boot uygulamasını paketlemek için gerekir.  Açıklaması şöyle.
Unfortunately, if we are working with a jar package, the basic Maven package goal doesn't include any of the external dependencies.

This means that we can use it only as a library in a bigger project.

To circumvent this limitation, we need to leverage the Maven Spring Boot plugin repackage goal to run our jar/war as a stand-alone application.
Maven 3.2 gerektirir. Açıklaması şöyle.
The Spring Boot Maven Plugin provides Spring Boot support in Maven, allowing you to package executable jar or war archives and run an application “in-place”. To use it you must be using Maven 3.2 (or better).
Eğer repackage goal maven package lifecycle'a bağlı değilse çalıştırmak için şöyle yaparız. Difference Between spring-boot:repackage and Maven package yazısına bakılabilir.
mvn package spring-boot:repackage
Bağlıysa sadece şöyle yaparız
mvn package
Bağlamak için şöyle yaparız. Bu en kullanılabilecek en basit hali.
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>2.4.1</version>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
</plugin>
1. repackage Goal - Fat Jar Yapısı
Eğer fat jar'ı açarsak BOOT-INF dizininde kendi kodlarımızı görebiliriz. org ile başlayan dizinde de spring kodları var. Şeklen şöyle
|__BOOT-INF
|  |__classes //1
|  |__ lib //2
|__META-INF
|  |__ MANIFEST.INF
|__org
   |__ springframework
        |__ loader //3
Açıklaması şöyle
1. Project compiled classes
2. JAR dependencies
3. Spring Boot class-loading classes
Manifest dosyasının için şöyledir
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.foo.executable.jar.ExecutableJarApplication
mainClass Tag
Örnek
Eğer kodumuzda birden fazla main metodu varsa başlangıç olanını belirtmek için kullanırız. configuration tag'i için açıklama şöyle.
Your existing archive will be enhanced by Spring Boot during the package phase. The main class that you want to launch can either be specified using a configuration option, or by adding a Main-Class attribute to the manifest in the usual way. If you don’t specify a main class the plugin will search for a class with a public static void main(String[] args) method.
Şöyle yaparız.
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
      <configuration>
        <mainClass>gpdi.MyApp</mainClass>
      </configuration>
    </execution>
  </executions>
</plugin>
classifier Tag
Örnek

Şöyle yaparız.
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>${spring.boot.version}</version>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
      <configuration>
        <classifier>exec</classifier>
      </configuration>
    </execution>
  </executions>
</plugin>

Hiç yorum yok:

Yorum Gönder