30 Mart 2023 Perşembe

project tag

Örnek
groupIdartifactIdversion belirtilirŞöyle yaparız
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>wiremock-example</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <java.version>11</java.version>
    <spring-boot.version>2.6.3</spring-boot.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
</project>



6 Mart 2023 Pazartesi

POM İçinde dependencyManagement Tag

Giriş
Bu tag ister BOM içinde, ister kendi POM projemiz içinde olsun kullanılmasını istediğimiz kütüphane sürümlerini belirtir.

BOM Import Etmek
<type>pom</type> yapılır. <scope>import</scope> belirtilir

Ö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>
Conflicting Transitive Dependency
Conflicting Transitive Dependency yazısına taşıdım

16 Şubat 2023 Perşembe

Dependency Scopes

1. compile
Kendi kodumuz görebilir Açıklaması şöyle
Leaks into consumers' compile time?  yes
Leaks into consumers' runtime?     yes
Included in Artifact?     yes
2. runtime
Dependency paketlerini kendi kodumuz göremez Dolayısıyla kullanmayız. Açıklaması şöyle
Leaks into consumers' compile time?  no
Leaks into consumers' runtime?     yes
Included in Artifact?     yes
Örnek
Şöyle yaparız
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
    <scope>runtime</scope>
</dependency>
3. test
Kendi kodumuz görebilir Açıklaması şöyle
Leaks into consumers' compile time?  no
Leaks into consumers' runtime?     no
Included in Artifact?     no
4. provided
Açıklaması şöyle
- Dependencies with this scope are required for compiling and running the application, but they are expected to be provided by the runtime environment.
- They are available on the classpath during the compile and test phases but are not packaged with the application.
5. system
Açıklaması şöyle
- Dependencies with this scope are similar to compile dependencies but are referenced using an explicit path on the local system.
- They are not available in any Maven repository.
Örnek
Şöyle yaparız
<scope>system</scope>
<systemPath>/path/to/dependency.jar</systemPath>
gradle implementation vs maven
Açıklaması şöyle
Q : What is the maven equivalent of implementation dependencies in gradle?
A : 
- Such dependency should have scope=compile when declared in your lib. This way it will be available during compilation of the lib.

- But it should have scope=runtime when declared in dependencyManagement section of other modules that depend on your lib. This way it won't be present in the classpath when compiling other modules.



8 Ocak 2023 Pazar

hibernate-enhance plugin

Giriş
Native derleme için gerekir. Graalvm ile birlikte kullanılır

Örnek
Şöyle yaparız
<plugin>
  <groupId>org.hibernate.orm.tooling</groupId>
  <artifactId>hibernate-enhance-maven-plugin</artifactId>
  <version>${hibernate.version}</version>
  <executions>
    <execution>
      <id>enhance</id>
      <goals>
        <goal>enhance</goal>
      </goals>
      <configuration>
        <enableLazyInitialization>true</enableLazyInitialization>
	<enableDirtyTracking>true</enableDirtyTracking>
	<enableAssociationManagement>true</enableAssociationManagement>
      </configuration>
    </execution>
  </executions>
</plugin>

2 Ocak 2023 Pazartesi

Custom Archetype Yaratma

Giriş
Bir örnek burada

Örnek
pom.xml şöyledir. Burada önemli olan şey artifactId ile belirtilen isim
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>fr.simplex-software.archetypes</groupId>
  <artifactId>jakartaee10-basic-archetype</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Basic Java EE 10 project archetype</name>
  ...
  <packaging>maven-archetype</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  <build>
    <extensions>
      <extension>
        <groupId>org.apache.maven.archetype</groupId>
        <artifactId>archetype-packaging</artifactId>
        <version>3.1.1</version>
      </extension>
    </extensions>
  </build>
</project>
Böylece şöyle kullanırız
mvn -B archetype:generate \
  -DarchetypeGroupId=fr.simplex-software.archetypes \
  -DarchetypeArtifactId=jakartaee10-basic-archetype \
  -DarchetypeVersion=1.0-SNAPSHOT \
  -DgroupId=com.exemple \
  -DartifactId=test
Kod üretiminde kullanılacak her şey şu dosyada tanımlı. Bu dosyada tanımlı her şeyin önüne src/main/resources/archetype-resources/ eklenir.
src/main/resources/META-INF/maven/archetype-metadata.xml
XML şöyle ise 
<archetype-descriptor...>
  <fileSets>
    <fileSet filtered="true" packaged="false" encoding="UTF-8">
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.java</include>
      </includes>
    </fileSet>
    ...
  </fileSets>
</archetype-descriptor>
Java kodları şu dizinde
src/main/resources/archetype-resources/src/main/java
Örneğin test kodları şurada
src/main/resources/archetype-resources/src/test/java/
Diğer dosyalar şurada
src/main/resources/archetype-resources/src/main/resources/META-INF
src/main/resources/archetype-resources/src/main/webapp/WEB-INF/
Kodlarda Velocity placeholder kullanılıyor. Şöyle yaparız. Burada  $package bir placeholder
package $package;

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import jakarta.inject.*;

import org.eclipse.microprofile.config.inject.*;

@Path("myresource")
public class MyResource {
  @Inject
  @ConfigProperty(name = "message")
  private String message;

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String getIt() {
    return message;
  }
}



Custom Archetypes - Maven İle Hazır Gelmeyenler

Giriş
1. DarchetypeGroupId kullanılır. 
2. İsteğe bağlı olarak -DarchetypeArtifactId ve -DarchetypeVersion belirtilebilir. 
3. GAV (GroupID, ArtifactID, Version) örneğim com.example:test:1.0-SNAPSHOT mecburen belirtiliyor

Örnek
Şöyle yaparız
mvn archetype:generate \
   -DarchetypeGroupId=org.apache.flink \
   -DarchetypeArtifactId=flink-quickstart-java \
   -DarchetypeVersion=1.13.0 \
   -DgroupId=com.example \
   -DartifactId=my-flink-project \
   -Dversion=0.1 \
   -Dpackage=com.example \
   -DinteractiveMode=false
Açıklaması şöyle
This command creates a new Maven project with the Flink dependencies and a sample Flink job.
Örnek
Şöyle yaparız
mvn archetype:generate \
   -DarchetypeGroupId=com.hazelcast.simulator \
   -DarchetypeArtifactId=archetype \
   -DarchetypeVersion=0.11-SNAPSHOT
Örnek 
Şöyle yaparız. Burada -DarchetypeArtifactId ve -DarchetypeVersion da belirtilmiş
mvn archetype:generate -DarchetypeGroupId=io.confluent.maven
-DarchetypeArtifactId=kafka-connect-quickstart -DarchetypeVersion=0.10.0.0 \
-Dpackage=com.opencredo.examples \
-DgroupId=com.opencredo.examples \
-DartifactId=my-source-connector \
-Dversion=1.0-SNAPSHOT

28 Aralık 2022 Çarşamba

pluginRepositories tag

Örnek
Şöyle yaparız
<project ...>
  <parent>
    ...
  </parent>
  <properties>
    ...
  </properties>
  <dependencies>
    ...
  </dependencies>
  <build>
    <plugins>
    ...
    </plugins>
  </build>

  <repositories>
    ...
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>spring-libs-milestone</id>
      <url>https://repo.spring.io/libs-milestone</url>
    </pluginRepository>
  </pluginRepositories>
</project>

Local Snapshot Kullanmak

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