3 Kasım 2022 Perşembe

build-helper plugin

add-source goal
Herhangi bir kod üretici plugin tarafından üretilen kodları source olarak projeye ekler.

Ö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>${project.build.directory}/generated-sources/java</outputDirectory>
  </configuration>
    </execution>
  </executions>
</plugin>

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>3.3.0</version>
  <executions>
    <execution>
  <id>add-source</id>
  <phase>generate-sources</phase>
  <goals>
    <goal>add-source</goal>
  </goals>
  <configuration>
    <sources>
      <source>${project.build.directory}/generated-sources/java/</source>
    </sources>
  </configuration>
    </execution>
  </executions>
</plugin>
attach-artifact goal
Örnek - shaded
shade yapılan projeyi bir başka modülde kullanabilmeyi sağlar
Örnek
github örneği burada
shade için şöyle yaparız
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.3.0</version>
  <configuration>
    <relocations>
      <relocation>
        <pattern>org.apache</pattern>
      <shadedPattern>three.nine.org.apache</shadedPattern>
    </relocation>
  </relocations>
  <createDependencyReducedPom>false</createDependencyReducedPom>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Daha sonra yine aynı projede şöyle yaparız
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>3.3.0</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>attach-artifact</goal>
      </goals>
      <configuration>
        <artifacts>
          <artifact>
            <file>${project.build.directory}/${project.build.finalName}.jar</file>
            <type>jar</type>
            <classifier>shaded</classifier>
          </artifact>
        </artifacts>
      </configuration>
     </execution>
  </executions>
</plugin>
parse-version go
parse-version goal
version plugin tarafından üretilen ve pom dosyasına yazılan versiyon numarasını kullanır.

Örnek
Şöyle yaparız
<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>build-helper-maven-plugin</artifactId>
   <version>3.1.0</version>
   <executions>
      <execution>
         <id>parse-version</id>
         <goals>
            <goal>parse-version</goal>
         </goals>
      </execution>
   </executions>
</plugin>

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>versions-maven-plugin</artifactId>
   <version>2.8.1</version>
</plugin>

liquibase plugin

Giriş
Çalıştırmak için şöyle yaparız
$ mvn liquibase:update
Elimizde şöyle bir şey olsun
<properties>
  <java.version>17</java.version>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>${java.version}</maven.compiler.source>
  <maven.compiler.target>${java.version}</maven.compiler.target>

  <!-- Liquibase properties -->
  <liquibase-maven-plugin.version>4.16.1</liquibase-maven-plugin.version>
  <mysql.version>8.0.30</mysql.version>
  <db.url>jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=UTF-8&amp;createDatabaseIfNotExist=true</db.url>
  <db.username>root</db.username>
  <db.password></db.password>
</properties>
Şu satırı dahil ederiz. changeLogFile ile sql dosyalarının yerini belirtiriz.
<plugin>
  <!-- Liquibase plugin -->
  <groupId>org.liquibase</groupId>
  <artifactId>liquibase-maven-plugin</artifactId>
  <version>${liquibase-maven-plugin.version}</version>
  <configuration>
    <changeLogFile>${project.basedir}/db/changelog-master.yml</changeLogFile>

    <!-- DB connection config -->
    <driver>com.mysql.jdbc.Driver</driver>
    <url>${db.url}</url>
    <username>${db.username}</username>
    <password>${db.password}</password>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</plugin>
db dizinindeki changelog-master.yml şöyledir
databaseChangeLog:
  - includeAll:
      - path: changes
      - relativeToChangelogFile: true
changes dizininde şu dosyalar vardır
001_createTablePerson.yml
002_addColumnUsername.yml
003_addLookupTableState.yml



profile tag

Giriş
profile tag tag başına maven projesi gibi. İçine bir sürü farklı alt tag alabiliyor

dependency Tanımlama
Örnek
Şöyle yaparız
<dependencies>
  <!-- Common dependencies -->
    
  <profiles>
    <profile>
      <id>dev</id>
      <dependencies>
        <!-- Dev-specific dependencies -->
      </dependencies>
    </profile>
    <profile>
      <id>prod</id>
      <dependencies>
        <!-- Prod-specific dependencies -->
      </dependencies>
    </profile>
  </profiles>
</dependencies>
module Tanımlama
Açıklaması şöyle
You can apply profile-specific configurations by using the <activation> section within each profile. 

Örnek
Şöyle yaparız
<profile>
  <id>mymodule-tests</id>
  <activation>
    <property>
      <name>!quick</name>
    </property>
    <jdk>[9,)</jdk>
  </activation>
  <modules>
    <module>modulepath-tests</module>
  </modules>
</profile>
plugin Tanımlama
Örnek
Şöyle yaparız
<profiles>
   <profile>
      <id>unit-test</id>
      <build>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <configuration>
                  <includes>
                     <include>**/Test*.java</include>
                  </includes>
               </configuration>
            </plugin>
         </plugins>
      </build>
   </profile>
   <profile>
      <id>integration-test</id>
      <build>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <configuration>
                  <includes>
                     <include>**/IntegrationTest*.java</include>
                  </includes>
               </configuration>
            </plugin>
         </plugins>
      </build>
   </profile>

</profiles>
property Tanımlama
Örnek
Şöyle yaparız
<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <env>development</env>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <env>production</env>
    </properties>
  </profile>
</profiles>
Ö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>

Maven Wrapper

Maven Wrapper Kullanıyorsak
Tüm komutlarda mvn yerine mvnw kullanılır. Maven Wrapper projede kullanılan maven sürümünü otomatik olarak indirir ve kullanır.  Açıklama Gradle için ancak Maven için de geçerli. Açıklama şöyle
The idea behind Gradle’s wrapper is to keep the exact Gradle version along with the project and just enough code to download the full version over the Internet.
Örnek
Şöyle yaparız
mvnw     <-- Linux için
mvnw.cmd <-- Windows için
Örnek
Şöyle yaparız. Burada maven wrapper'a maven olarak 3.6.3 kullanılması belirtiliyor
$ mvn wrapper:wrapper -Dmaven=3.6.3
$ ./mvnw clean package
Maven Wrapper Kullanmıyorsak
Eğer maven wrapper kullanmıyorsak maven-wrapper kullanmaya başlamak için şöyle yaparız. 
mvn -N wrapper:wrapper
Artık sadece maven-wrapper.properties dosyasındaki maven sürümünü değiştirerek derleyebiliriz.

maven-wrapper.properties Dosyası
Yolu şöyle
.mvnw/wrapper
  maven-wrapper.properties
İçi şuna benzer
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.3/apache-maven-3.9.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar


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

mvnup komutu

check Şöyle yaparız mvnup check apply Şöyle yaparız mvnup apply