30 Kasım 2022 Çarşamba

jooq-codegen plugin

Giriş
configuration/jdbc tag ile JDBC bağlantısı belirtilir
configuration/jdbc tag ile JDBC bağlantısı belirtilir

configuration/generator Tag
configuration/generator tag ile üretilecek kod için ayarlar belirtilir

Örnek - POJO Üretmek
Şöyle yaparız
<plugin>
  <groupId>org.jooq</groupId>
  <artifactId>jooq-codegen-maven</artifactId>
  <executions>
    <execution>
      <id>generate-postgres</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <!-- JDBC connection parameters -->
          <jdbc>
            <driver>${datasource.driver}</driver>
            <url>${datasource.jdbcUrl}</url>
            <user>${datasource.username}</user>
            <password>${datasource.password}</password>
          </jdbc>
          <!-- Generator parameters -->
        <generator>
          <database>
            <name>
              org.jooq.meta.postgres.PostgresDatabase
            </name>
            <includes>.*</includes>
            <excludes/>
            <inputSchema>public</inputSchema>
          </database>
          <generate>
            <pojos>true</pojos>
            <pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
            <javaTimeTypes>true</javaTimeTypes>
            <fluentSetters>true</fluentSetters>
          </generate>
          <target>
            <packageName>com.example.springbootjooq.model</packageName>
            <directory>target/generated-sources/jooq</directory>
          </target>
        </generator>
      </configuration>
    </execution>
  </executions>
</plugin>
Çalıştırmak için şöyle yaparız. Çıktı target/generated-sources/jooq dizinindedir
mvn clean generate-sources
Örnek - DAO
Şöyle yaparız. Burada DAO üretiliyor ve DAO'lar Spring anotasyonları kullanıyor.
<generate>
  <pojos>true</pojos>
  <pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
  <javaTimeTypes>true</javaTimeTypes>
  <fluentSetters>true</fluentSetters>
  <!-- Generate the DAO classes -->
  <daos>true</daos>
  <!-- Annotate DAOs (and other types) with spring annotations, 
  such as @Repository and @Autowired for auto-wiring the Configuration instance, 
  e.g. from Spring Boot's jOOQ starter -->
  <springAnnotations>true</springAnnotations>
  <!-- Generate Spring-specific DAOs containing @Transactional annotations -->
  <springDao>true</springDao>
</generate>
Örnek - Kotlin
Şöyle yaparız
<plugin>
  <groupId>org.jooq</groupId>
  <artifactId>jooq-codegen-maven</artifactId>                         <!--1-->
  <executions>
    <execution>
      <id>jooq-codegen</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>org.postgresql</groupId>                               <!--2-->
      <artifactId>postgresql</artifactId>
      <version>42.6.0</version>
    </dependency>
  </dependencies>
  <configuration>
    <generator>
      <name>org.jooq.codegen.KotlinGenerator</name>                   <!--3-->
      <database>
        <inputSchema>people</inputSchema>                             <!--4-->
      </database>
      <target>
        <packageName>ch.frankel.blog.reactivedata.jooq</packageName>
      </target>
    </generator>
    <jdbc>                                                            <!--4-->
      <driver>org.postgresql.Driver</driver>
      <url>jdbc:postgresql://localhost:5432/postgres</url>
      <user>postgres</user>
      <password>root</password>
    </jdbc>
  </configuration>
</plugin>
Açıklaması şöyle
1. The version is defined in the parent Spring Boot Starter parent POM
2. Set the necessary database driver(s). Note that one should use the non-reactive driver
3. There's a Kotlin generator!
4. Configure database configuration parameters



9 Kasım 2022 Çarşamba

openapi-generator plugin

Giriş
Open API içeren yaml dosyasından kod üretir. Yaml dosyasını Swagger Editor ile düzenleriz.
Kod üretmek için şöyle yaparız
mvn clean compile
generatorName Alanı
Spring için kod üreteceksek spring değeri verilir.
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>5.1.0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <id>buildApi</id>
      <configuration>

        <!-- specify the location of specification file here -->
        <inputSpec>${basedir}/src/main/resources/open-api.yaml</inputSpec>
                
        <generatorName>spring</generatorName>
        <generateApis>true</generateApis>
        <generateModels>true</generateModels>

        <configOptions>
          <generateSupportingFiles>true</generateSupportingFiles>
          <library>spring-boot</library>
          <interfaceOnly>true</interfaceOnly>
          <useBeanValidation>true</useBeanValidation>
          <sourceFolder>/src/main/java</sourceFolder>
          <implFolder>/src/main/java</implFolder>
          <serializableModel>true</serializableModel>
          <java8>true</java8>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>
Örnek open-api.yaml dosyası şöyle
openapi: 3.0.3 # version of the specification
info:
  version: '1'
  title: Open API Generator Spring Boot Example

servers:
  - url: http://localhost:8080

paths:
  /parents:
    get:
      summary: return parents information
      operationId: getParents
      responses:
        200:
          description: General parents information
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ParentResponse'

components:
  schemas:
    ParentResponse:
      discriminator:
        propertyName: ParentResponseType
        mapping:
          Parent: '#/components/schemas/Parent'
      type: object
      properties:
        status:
          type: string
        errorMessage:
          type: string
      description: Response of the parent.

    Parent:
      allOf:
        - $ref: '#/components/schemas/ParentResponse'
        - type: object
          properties:
            parentId:
              type: integer
              description: The ID of the parent
              format: int64
            parentName:
              type: string
              example: 'Stephen'
Üretilen kod şöyle
org.openapitools.api
 ApiUtil
  ParentsApi
org.openapitools.model
  Parent
  ParentAllOf
  ParentResponse

inputSpec Alanı
yaml dosyasının yerini belirtir

Örnek
Şöyle yaparız
<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>6.1.0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>
          ${project.basedir}/src/main/resources/employee.yaml
        </inputSpec>
        <generatorName>spring</generatorName>
        <apiPackage>org.SwaggerCodeGenExample.api</apiPackage>
        <modelPackage>org.SwaggerCodeGenExample.model</modelPackage>
        <configOptions>
          <interfaceOnly>true</interfaceOnly>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>
üretilen kodun derlenmesi için şu dependency listesi gerekir
<dependencies>
  <dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>2.2.2</version>
  </dependency>
  <dependency>
    <groupId>org.openapitools</groupId>
    <artifactId>jackson-databind-nullable</artifactId>
    <version>0.2.2</version>
  </dependency>
  <dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
  </dependency>
</dependencies>
Üretilen kod /target/generated-sources/openapi dizinindedir. Kod sadece interface'lerden ibarettir. Daha sonra kendi sınıfımızı üretilen arayüzden kalıtırız. Şöyle yaparız
@RestController
public class EmployeeController implements EmployeeApi {

  @Override
  public ResponseEntity<String> addEmployee(@Valid Employee employee) {
    ...
  }

  @Override
  public ResponseEntity<List<Employee>> getEmployees() {
    ...
  }
}


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 ederizchangeLogFile 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


Local Snapshot Kullanmak

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