maven etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
maven etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

20 Eylül 2022 Salı

spring-boot-maven-plugin build-info Goal

Giriş
Plugin çalışınca şu dosya yaratılır
/build/resources/main/META-INF/build-info.properties
Eğer bu dosya varsa Spring otomatik olarak org.springframework.boot.info.BuildProperties diye bir bean yaratır. Bu bean'in içi şuna benzer
build.artifact=log-version
build.group=examples.debuggingspring
build.name=log-version
build.time=2022-10-09T14\:51\:08.212010400Z
build.version=latest
Maven
Açıklaması şöyle
To show the version number associated with the application we must configure it through the pom.xml file, the spring-boot-maven-plugin, and the build-info option. This will automatically create the META-INF/build-info.properties file that will transmit the information to the Spring Boot Admin.
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>build-info</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Açıklaması şöyle
We can also add tags to the information of our application that allows us to categorize it, such as the environment where it is deployed. These tags will be transferred through the endpoint info. Configuration example:

#using the info endpoint
info.tags.environment=test
Gradle
Şöyle yaparız. Burada BUILD_NUMBER bir Jenkins ortam değişkeni
plugins {
    id 'org.springframework.boot' version '2.6.6'
}

springBoot {
    buildInfo()
}

version = System.getProperty("BUILD_NUMBER", "latest")
Örnek
Bean'i REST olarak dışarıya açmak için şöyle yaparız
@Service
public class BuildInfoService {

  @Autowired
  BuildProperties buildProperties;

  @Autowired
  Environment environment;

  public BuildInfoResponse getBuildInfo(){
    BuildInfoResponse buildInfoResponse = new BuildInfoResponse();
    buildInfoResponse.setName(this.buildProperties.getName());
    buildInfoResponse.setVersion(this.buildProperties.getVersion());
    buildInfoResponse.setTime(this.buildProperties.getTime());
    buildInfoResponse.setActiveProfile(this.environment.getActiveProfiles());
    buildInfoResponse.setSpringVersion(this.buildProperties.getVersion());
    buildInfoResponse.setGroup(this.buildProperties.getGroup());
    buildInfoResponse.setArtifact(this.buildProperties.getArtifact());
    return buildInfoResponse;
  }
}

@RestController
@RequestMapping("/${build-info.path}")
public class BuildInfoController {

  @Autowired
  BuildInfoService buildInfoService;

  @GetMapping("/build-info")
  public BuildInfoResponse getBuildInfo() {
    return this.buildInfoService.getBuildInfo();
  }
}
Örnek
Burada Spring loglarına versiyon bilgisi eklemekle ilgili bir örnek var.







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>

26 Şubat 2020 Çarşamba

spring-boot-maven-plugin

Giriş
Açıklaması şöyle
Spring Boot Maven Plugin is a Maven plugin that allows you to package executable jar or war archives and run an application in place. It provides a number of features that are useful when developing Spring Boot applications, such as the ability to package executable jar or war files, run an application in place, and use hot swapping with background compilation to make development faster.

Sunulan goal şunlar
1. repackage
2. run
3. start ve stop
4. build-info
5. build-image

repackage goal  - Fat Jar Oluşturur
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).
Çalıştırmak için şöyle yaparız
maven package spring-boot:repackage
run Goal 
Debug içindir. Açıklaması şöyle.
By default the application is executed directly from the Maven JVM. If you need to run in a forked process you can use the 'fork' option. Forking will also occur if the 'jvmArguments', 'systemPropertyVariables', 'environmentVariables' or 'agent' options are specified, or if devtools is present.

If you need to specify some JVM arguments (i.e. for debugging purposes), you can use the jvmArguments parameter,...
Normalde spring uygulamasını çalıştırmak için şöyle yaparız. Ancak bu kullanımda pom içinde tanımladığımız jvm parametreleri kullanılmaz.
$ ./myapp-1.1.0.jar --spring.config.name=application-prod
Bu parametreleri de kullanmak için şöyle yaparız.
maven spring-boot:run
Profile seçmek için şöyle yaparız.
mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=production"
pom Örnekleri

Örnek - executable jar
Şöyle yaparız
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <executable>true</executable>
      </configuration>
    </plugin>
  </plugins>
  <finalName>sba-as-windows-service</finalName>
</build>
Örnek - jvmArguments
Şöyle yaparız.
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <executable>true</executable>
    <jvmArguments>-Xmx256m</jvmArguments>
  </configuration>
</plugin>
Örnek - jvmArguments
Şöyle yaparız.
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
      <mainClass>tech.flexpoint.demo.DemoApplication</mainClass>
      <jvmArguments>--show-module-resolution --add-opens=java.base/java.lang=spring.core
        --add-opens=java.base/java.io=tomcat.embed.core
        --add-opens=java.base/java.lang=ALL-UNNAMED 
        --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED</jvmArguments>
      <workingDirectory>${project.basedir}</workingDirectory>
  </configuration>
</plugin>