30 Ekim 2022 Pazar

fabric8 plugin - Docker Image Oluşturur

Giriş
Docker image oluşturur 

build goal
Şöyle yaparız
mvn docker:build
push goal
Şöyle yaparız
mvn docker:push

Konfigurasyon
assembly  ile jar kopyalanır
from ile kullanılacak image belirtilir
entryPoint tag ile container başlarken çalıştıracak komut belirtilir

Örnek
Şöyle yaparız
<configuration>
  <pushRegistry>localhost:5000</pushRegistry>
  <containerNamePattern>%n</containerNamePattern>
  <images>
    <image>
      <alias>maven-demo</alias>
      <name>my1795/docker-demo:${project.version}</name>
      <build>
        <from>openjdk:17-alpine</from>
        <assemblies>
          <assembly>
            <mode>dir</mode>
            <descriptorRef>artifact</descriptorRef>
          </assembly>
        </assemblies>
        <entryPoint>java -jar maven/${project.artifactId}-${project.version}.jar</entryPoint>
      </build>
      <run>
        <ports>
          <port>8080:8080</port>
        </ports>
      </run>
    </image>
  </images>
</configuration>
Örnek
Şöyle yaparız
<configuration>
  <verbose>true</verbose>
  <dockerHost>${env.DOCKER_PORT}</dockerHost>
  <images>
    <image>
      <name>${env.CI_REGISTRY_IMAGE}/${project.artifactId}</name>
      <build>
        <shell>
          <exec>
            <arg>/bin/sh</arg>
            <arg>-c</arg>
            <arg>mkdir -p /mnt/backend</arg>
          </exec>
        </shell>
        <from>openjdk:11</from>
        <ports>
          <port>8084</port>
          <port>9091</port>
        </ports>
        <volumes>
          <volume>/mnt/backend</volume>
        </volumes>
        <entryPoint>
          <exec>
            <args>java</args>
            <args>-jar</args>
            <args>/maven/${project.artifactId}-${project.version}.jar</args>
          </exec>
        </entryPoint>
        <assembly>
          <descriptorRef>artifact</descriptorRef>
        </assembly>
      </build>
    </image>
  </images>
</configuration>

27 Ekim 2022 Perşembe

resource tag

directory Alanı
resource directory belirtilir.
Örnek 
Şöyle yaparız. Gerekirse resource directory içindeki dosya uzantıları da ayrı ayrı belirtilebilir.
<build>
  ...
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.xsl</include>
        <include>**/*.xml</include>
        <include>**/*.xsd</include>
        <include>**/*.properties</include>
      </includes>
    </resource>
    <resource>
      <directory>src/main/resources</directory>
    </resource>
    <resource>
      <directory>src/main/avro</directory>
    </resource>
  </resources>
</build>
exclude Alanı
Örnek
Şöyle yaparız
<resources>
<resource> <directory>[your directory]</directory> <excludes> <exclude>[non-resource file #1]</exclude> <exclude>[non-resource file #2]</exclude> <exclude>[non-resource file #3]</exclude> <exclude>[non-resource file #n]</exclude> </excludes> </resource> ... </resources>

Resource Filtering
Açıklaması şöyle
The ${project.version} and ${project.name} are examples of placeholders that Maven provides out of the box. You can use these placeholders to refer to project-related information such as the project version and name. You can also define your custom placeholders in the <properties> section of the pom.xml file and use them in your resource files.

By leveraging resource filtering, you can generate customized resource files for different environments or configurations without manually modifying the files each time. Maven will handle the filtering based on the configuration in the pom.xml file.

Note: Remember to use proper filtering directives (@, @{}, or ${}) based on the type of placeholder you are using and the file format you're working with (e.g., properties, XML, JSON, etc.).

mvn package komutu çalıştırılır. Projenin paketleme tipine göre dosya target/classes veya target/classes/META-INF dizinine kopyalanır

Örnek
src/main/resources/config.properties şöyle olsun
# config.properties
app.version=${project.version}
app.name=${project.name}
Şöyle yaparız
<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>





avro plugin - Avro Dosyalarından Java Kodu Üretir

Giriş
Avro için şu bağımlılığı eklemek gerekir
<dependency>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro</artifactId>
    <version>1.11.1</version>
</dependency>
IntelliJ
IntelliJ'in üretilen kodu derlemesi için çıktı dizini Source Folder olarak tanıması gerekir. Şeklen şöyle


configuration Alanına
Kod üretirken şu tagler tanımlanabilir
- sourceDirectory ve outputDirectory belirtilir
- stringType
- enableDecimalLogicalType
- fieldVisibility
- createSetters : Üretilen kodun getter/setter kullanacağı belirtilebilir. Eğer false ise Builder örüntüsü kullanılır 
Örnek
Builder kullanan kod şöyledir
private InsuranceClaim generateAvroClaimRequest() {
    Metadata metadata = Metadata.newBuilder()
            .setCorrelationId(UUID.randomUUID().toString())
            .setTimestamp(Instant.now())
            .build();

    return InsuranceClaim.newBuilder()
            .setClaimAmount(Precision.round(RandomUtils.nextDouble(200, 7000), 2))
            .setPriority(Priority.HIGH)
            .setProduct(Product.MEDICAL)
            .setMetadata(metadata)
            .build();
}


goal idl-protocol
IDL tabanlı avdl uzantılı dosyalardan Java kodu üretir
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro-maven-plugin</artifactId>
  <version>1.11.0</version>
  <executions>
    <execution>
      <id>avro-from-resources</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>idl-protocol</goal>
        <goal>schema</goal>
      </goals>
      <configuration>
        <sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
        <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
        <stringType>String</stringType>
        <enableDecimalLogicalType>true</enableDecimalLogicalType>
        <fieldVisibility>private</fieldVisibility>
      </configuration>
    </execution>
  </executions>
</plugin>
goal schema
JSON tabanlı avsc uzantılı dosyalardan Java kodu üretir
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro-maven-plugin</artifactId>
  <version>${avro.version}</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>schema</goal>
      </goals>
      <configuration>
        <sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
        <outputDirectory>${build.directory}/generated-sources</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro-maven-plugin</artifactId>
  <version>${avro.version}</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>schema</goal>
      </goals>
      <configuration>
        <sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
        <outputDirectory>${project.build.directory}/generated/avro</outputDirectory>
        <createSetters>false</createSetters>
        <enableDecimalLogicalType>true</enableDecimalLogicalType>
        <fieldVisibility>private</fieldVisibility>
      </configuration>
    </execution>
  </executions>
</plugin>
Java kodu üretmek için şöyle yaparız
mvn generate-sources

26 Ekim 2022 Çarşamba

developers tag

Giriş
Bu tag bence çok gereksiz bir şey ama bazı projelerde kullanılıyor

Örnek
Şöyle yaparız
<!-- List the core committers -->
<developers>
  <developer>
    <id>karianna</id>
    <name>Martijn Verburg</name>
    <organization>Ikasan</organization>
     <organizationUrl>http://www.ikasan.org</organizationUrl>
      <roles>
        <role>developer</role>
      </roles>
    <timezone>0</timezone>
  </developer>
  ...
</developers>

<!-- Contributors -->
<contributors>
  <contributor>
    <name>Cae Fernandes</name>
    <roles>
      <role>developer</role>
    </roles>
    <timezone>-3</timezone>
  </contributor>
  ...
</contributors>



19 Ekim 2022 Çarşamba

Dependency Management with BOM

Giriş
Açıklaması şöyle
BOM stands for "Bill Of Materials". A BOM is a special kind of POM that is used to control the versions of a project’s dependencies and provide a central place to define and update those versions.
Açıklaması şöyle
The Bill Of Material is a special POM file that groups dependency versions that are known to be valid and tested to work together. This will reduce the developers’ pain of having to test the compatibility of different versions and reduce the chances to have version mismatches.
1. BOM Yazmak
Açıklaması şöyle
BOM elements:

1. a pom packaging type: <packaging>pom</packaging>.
2. a dependencyManagement the section that lists the dependencies of a project.
Inside the dependencyManagement we will set all of the dependencies needed and specific versions of the dependency because it is mandatory.
Örnek
Şöyle yaparız
<project ...>
  <modelVersion>4.0.0</modelVersion>
  <groupId>reflectoring</groupId>
  <artifactId>reflectoring-bom</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <name>Reflectoring Bill Of Material</name>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.reflectoring</groupId>
        <artifactId>logging</artifactId>
        <version>2.1</version>
      </dependency>
      <dependency>
        <groupId>io.reflectoring</groupId>
        <artifactId>test</artifactId>
        <version>1.1</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>
2. BOM'u Kullanmak
İki yöntem var.
1.BOM As A Parent Yöntemi
2.BOM as a Dependency Yöntemi

2.1 BOM As A Parent Yöntemi
Bir projede tek bir parent BOM olabilir. <parent>...</parent> tagleri içine yazılır
Örnek
Şöyle yaparız
<project ...>
  <modelVersion>4.0.0</modelVersion>
  <groupId>baeldung</groupId>
  <artifactId>Test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Test</name>
  <parent>
    <groupId>baeldung</groupId>
    <artifactId>Baeldung-BOM</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
</project>
Örnek
Şöyle yaparız
<project ...>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>reflectoring</groupId>
    <artifactId>reflectoring-bom</artifactId>
    <version>1.0</version>
  </parent>
    
  <groupId>reflectoring</groupId>
  <artifactId>new-project</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>New Project</name>
    
  <dependency>
    <groupId>io.reflectoring</groupId>
    <artifactId>logging</artifactId>
  </dependency>
</project>

2.2 BOM as a Dependency
Bir projede tek bir parent BOM olabilir, ancak bazen bir başka projeden de parent BOM gelebiliyor. Bu durumda BOM kendi pom.xml dosyamıza <dependencyManagement>..</dependencyManagement> olarak kopyalanabilir. Kullanırken şöyle yaparız
<type>pom</type>
<scope>import</scope>
Örnek
Şöyle yaparız. Burada SpringBoot Starter Parent projesini Bill Of Materials olarak kullandık
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.5.4</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
Örnek
Şöyle yaparız
<project ...>
  <modelVersion>4.0.0</modelVersion>
  <groupId>baeldung</groupId>
  <artifactId>Test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Test</name>
    
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>baeldung</groupId>
        <artifactId>Baeldung-BOM</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>
Örnek
Şöyle yaparız
<project ...>
  <modelVersion>4.0.0</modelVersion>
  <groupId>reflectoring</groupId>
  <artifactId>new-project</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>New Project</name>
    
  <dependency>
    <groupId>io.reflectoring</groupId>
    <artifactId>logging</artifactId>
  </dependency>
    
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>reflectoring</groupId>
        <artifactId>reflectoring-bom</artifactId>
        <version>1.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

11 Ekim 2022 Salı

xolstice protobuf plugin

Giriş
protobuf için birkaç tane plugin var. Sanırım en çok kullanılanı bu. 

Goals
Şöyle
compile
compile-custom

Her iki goal da kullanılmalı

Ayarlar
Bu kısımlar her zaman şöyle

Ortak Ayarlar
protocArtifact : com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}

compile Ayarları
pluginId : grpc-java

compile-custom Ayarları
pluginArtifact : io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}


protoSourceRoot alanını belirtilmemişse src/main/proto altındaki "*.proto" dosyalarını işler

Ayrıca şu dependency de lazım
<dependency>
<groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.12.2</version> </dependency>
Bu plugini kullanabilmek için şu lazım
<build>
  <!-- protobuf extension-->
  <extensions>
    <extension>
      <groupId>kr.motd.maven</groupId>
      <artifactId>os-maven-plugin</artifactId>
      <version>1.7.1</version>
    </extension>
  </extensions>
...
</build>
compile goal
Tam hali şöyle
<build>
  <extensions>
    <extension>
      <groupId>kr.motd.maven</groupId>                   
      <artifactId>os-maven-plugin</artifactId>
      <version>1.7.1</version>
    </extension>
  </extensions>
  <plugins>
    <plugin>
      <groupId>org.xolstice.maven.plugins</groupId>      
      <artifactId>protobuf-maven-plugin</artifactId>
      <version>${protobuf-plugin.version}</version>
      <configuration>
        <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
        <pluginId>grpc-java</pluginId>
        <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
            <goal>compile-custom</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Örnek
Şöyle yaparız
<plugin>
  <groupId>org.xolstice.maven.plugins</groupId>
  <artifactId>protobuf-maven-plugin</artifactId>
  <version>0.6.1</version>
  <configuration>
    <protocArtifact>
      com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}
    </protocArtifact>
    <pluginId>grpc-java</pluginId>
    <pluginArtifact>
      io.grpc:protoc-gen-grpc-java:1.52.1:exe:${os.detected.classifier}
    </pluginArtifact>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
        <goal>compile-custom</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.xolstice.maven.plugins</groupId>
  <artifactId>protobuf-maven-plugin</artifactId>
  <version>0.5.1</version>
  <configuration>
    <protocArtifact>
      com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}
    </protocArtifact>
    <pluginId>grpc-java</pluginId>
    <pluginArtifact>
      io.grpc:protoc-gen-grpc-java:1.22.1:exe:${os.detected.classifier}
    </pluginArtifact>
    <protoSourceRoot>
      ${basedir}/src/main/proto/
    </protoSourceRoot>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
        <goal>compile-custom</goal>
      </goals>
    </execution>
  </executions>
</plugin>
compile-custom Goal


Örnek
proto dosyalarından dokümantasyon üretmek için şu eklentiler kullanılabilir. 
protoc-gen-doc
gRPC-docs
GenDocu Cloud
Buf

protoc-gen-doc kullanmak için şöyle yaparız. Burada hem compile hem de compile-custom goal belirtiliyor. Ancak compile-custom iki defa belirtiliyor. İkinci belirtimde  protoc-gen-doc plugin kullanılarak dokümantasyon üretmesi isteniyor

<plugin>
  <groupId>org.xolstice.maven.plugins</groupId>
  <artifactId>protobuf-maven-plugin</artifactId>
  <version>0.6.1</version>
  <configuration>
    <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
    </protocArtifact>
  </configuration>
  <executions>
    <execution>
      <id>protoc-java</id>
      <goals>
       <goal>compile</goal>
      </goals>
     </execution>
     <execution>
      <id>protoc-grpc</id>
      <goals>
       <goal>compile-custom</goal>
      </goals>
      <configuration>
       <pluginId>grpc-java</pluginId>
       <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
       </pluginArtifact>
      </configuration>
     </execution>
     <execution>
      <id>protoc-doc</id>
      <goals>
       <goal>compile-custom</goal>
      </goals>
      <configuration>
       <pluginId>doc</pluginId>
       <pluginArtifact>
        io.github.pseudomuto:protoc-gen-doc:${protoc-gen-doc.version}:exe:${os.detected.classifier}
       </pluginArtifact>
       <pluginParameter>markdown,grpc-api.md</pluginParameter>
      </configuration>
     </execution>
    </executions>
   </plugin>

  </plugins>
 </build>






settings.xml Dosyası

Giriş
Dosyanın yolu şöyle
~/.m2/settings.xml
Türkçe Windows'ta yol şöyle
C:\Users\user\.m2
Örnek - Boş Dosya
Şöyle yaparız
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository/>
  <interactiveMode/>
  <offline/>
  <pluginGroups/>
  <servers/>
  <mirrors/>
  <proxies/>
  <profiles/>
  <activeProfiles/>
</settings>

Örnek
Properties vermek için şöyle yaparız
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <servers/>
  <profiles>
    <profile>
      <id>default</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        ...
      </properties>
    </profile>
  </profiles>
</settings>



7 Ekim 2022 Cuma

help plugin

Giriş
Açıklaması şöyle
The effective pom is the POM that results from the application of interpolation, inheritance and active profiles. To generate the effective POM, run the following command in your terminal: ./mvnw help:effective-pom.

When running this command on the the example project, the result will be fairly small, because it is a relatively small project. For a large project, the effective pom will be much bigger. If needed, you can write the effective POM into a log file using the following command: ./mvnw help:effective-pom --log-file log.txt.

Local Snapshot Kullanmak

Örnek Şöyle yaparız <repository> <id>snapshot-repository</id> <name>Maven2 Snapshot Repository</name> &l...