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>

repositories tag - Şirketin Repository Sunucusunu Belirtirtir

Giriş
Açıklaması şöyle
In Maven we have 3 types of repositories

1. Central Repository
2. Remote Repository
3. Local Repository

Central repository is maintained by Apache organization
Every company will maintain their own remote repository (Ex: Nexus, JFrog)
Local repository will be created in our system (Location : C://users/<uname>/.m2)
Örnek
Şöyle yaparız
<repositories>
<repository> <id>id</id> <url>jfrong-repo-url/</url> </repository> </repositories>
Örnek
Şöyle yaparız
<project ...>
  <parent>
    ...
  </parent>
  <properties>
    ...
  </properties>
  <dependencies>
    ...
  </dependencies>
  <build>
    <plugins>
    ...
    </plugins>
  </build>

  <repositories>
    <repository>
      <id>spring-libs-milestone</id>
      <url>https://repo.spring.io/libs-milestone</url>
    </repository>
  </repositories>

  <pluginRepositories>
    ...
  </pluginRepositories>
</project>

23 Aralık 2022 Cuma

native plugin - GraalVM İle Native Image Oluşturur

Goaller
build
compile-no-fork
release

Örnek
Sadece şöyle yapmak yeterli. maven install ile proje derlenir.
<plugin>
  <groupId>org.graalvm.buildtools</groupId>
  <artifactId>native-maven-plugin</artifactId>
</plugin>
Profile
Örnek
Bu plugin bazen bir profile ile kullanılıyor. Önce bir profile ekleriz
<profiles>
  <profile>
    <id>native</id>
    <build>
      <plugins>
        <plugin>
          <groupId>org.graalvm.buildtools</groupId>
          <artifactId>native-maven-plugin</artifactId>
          <executions>
            <execution>
              <id>build-native</id>
              <goals>
                <goal>compile-no-fork</goal>
              </goals>
              <phase>package</phase>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
</profile>
</profiles>
Daha sonra şöyle yaparız
./mvnw clean package -Pnative
Eğer plugin profile içinde değilse şöyle yaparız
./mvnw clean package -Dpackaging=native-image
Örnek 
Açıklaması şöyle
If you have not installed it to your own machine, you also have an option to use a Docker container that has GraalVM and the native image extension installed by default. You can use the following command for that: 

./mvnw package -Pnative -Dquarkus.native.container-build=true

It might take some time to finish compilation, especially in the docker container, but after a while you should have a native executable named code-with-quarkus-1.0.0.-SNAPSHOT-runner in your target folder.
Şöyle yaparız
./mvnw package -Pnative -Dquarkus.native.container-build=true
Örnek
Şöyle yaparız
<profile>
  <id>native</id>
  <activation>
    <property>
      <name>native</name>
    </property>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.graalvm.buildtools</groupId>
        <artifactId>native-maven-plugin</artifactId>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>build-native</id>
            <goals>
              <goal>build</goal>
            </goals>
            <phase>package</phase>
          </execution>
          <execution>
            <id>release-native</id>
            <goals>
              <goal>release</goal>
            </goals>
            <phase>release</phase>
          </execution>
        </executions>
        <configuration>
          <imageName>${appName}-${project.version}</imageName>
          <mainClass>${mainClass}</mainClass>
          <buildArgs>
            <buildArg>--allow-incomplete-classpath</buildArg>
            <buildArg>--no-fallback</buildArg>
            <buildArg>-H:+ReportExceptionStackTraces</buildArg>
            <buildArg>--verbose</buildArg>
          </buildArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>
Çalıştırmak için şöyle yaparız
./mvnw -B --file pom.xml -Pnative package
Açıklaması şöyle
When you run the resulting application, the application will report the Class files it is unable to find
...
You can then add them to the reflect-config.json in the resources/META-INF/native-image folder of your project.

And you specify the configuration in native-image.properties for GraalVM native-image-plugin to refer the reflect-config.json while building the native image.

-H=ReflectionConfigurationResources=${.}/reflect-config.json
reflect-config.json dosyası şöyle. Burada her bir eksik class teker teker tanımlanıyor
[
  {
    "name": "io.jsonwebtoken.impl.crypto.MacProvider",
    "allDeclaredConstructors": true,
    "allPublicConstructors": true,
    "allDeclaredMethods": true,
    "allPublicMethods": true,
    "allDeclaredClasses": true,
    "allPublicClasses": true
  },
  ...
]
configuration Seçenekleri

--allow-incomplete-classpath Seçeneği
reflection-config.json dosyasına her sınıfı yazdığımızı düşünsek bile halen hata almaya devam edebiliriz. Bu durumda şöyle yaparız
<buildArg>--allow-incomplete-classpath</buildArg>
Açıklaması şöyle
An alternative quick fix could be the option --allow-incomplete-classpath. This ensures that the possible linking errors are shifted from build time to run time.
--initialize-at-build-time Seçeneği
Şöyle bir hata alabiliriz. Yani bir sınıf derleme esnasında (compile time) hata veriyor
ERROR: Classes that should be initialized at run time got initialized during image building:…
Açıklaması şöyle
The most classes are initialized at build time and GraalVM tries to find out what can be initialized at build time and which classes must be initialized at run time. This error can be fixed with the parameter --initialize-at-run-time. This parameter will force to initialize this class at runtime. Another way to force to initialize a class during build is to use the parameter --initialize-at-build-time.
Örnek
Şöyle yaparız
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
  <buildArgs>
    <buildArg>--initialize-at-build-time=my.build.package</buildArg>
    <buildArg>--initialize-at-build-time=my.other.build.package.SpecificClass</buildArg>
    <buildArg>--initialize-at-run-time=my.run.package</buildArg>
    <buildArg>--initialize-at-run-time=my.other.run.package.SpecificClass</buildArg>
  </buildArgs>
</configuration>
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.graalvm.buildtools</groupId>
  <artifactId>native-maven-plugin</artifactId>
  <configuration>
    <buildArgs>
      <buildArg>--initialize-at-build-time=org.apache.commons.logging.LogFactory,org.apache.commons.logging.LogFactoryService,org.slf4j.MDC,ch.qos.logback.core.pattern.parser.Parser,ch.qos.logback.core.util.Loader,ch.qos.logback.core.util.StatusPrinter,org.slf4j.impl.StaticLoggerBinder,org.slf4j.LoggerFactory,ch.qos.logback.classic.Logger,ch.qos.logback.core.spi.AppenderAttachableImpl,ch.qos.logback.core.status.StatusBase,ch.qos.logback.classic.Level,ch.qos.logback.core.status.InfoStatus,ch.qos.logback.classic.PatternLayout,ch.qos.logback.core.CoreConstants</buildArg>
      <buildArg>-H:+ReportExceptionStackTraces</buildArg>
     </buildArgs>
     <jvmArgs>
      <arg>-Xmx8g</arg>
      <arg>-Xms8g</arg>
     </jvmArgs>
  </configuration>
  <executions>
    <execution>
      <id>build-native</id>
      <goals>
        <goal>compile-no-fork</goal>
      </goals>
      <phase>package</phase>
     </execution>
    </executions>
</plugin>
jvmArgs
Örnek
Native image oluşturmak çok fazla bellek tüketiyor. Eklentinin kullanacağı belleği ayarlamak için şöyle yaparız
<plugin>
  <groupId>org.graalvm.buildtools</groupId>
  <artifactId>native-maven-plugin</artifactId>
  <configuration>
    <jvmArgs>
      <arg>-Xmx8g</arg>
      <arg>-Xms8g</arg>
     </jvmArgs>
  </configuration>
  <executions>
    <execution>
      <id>build-native</id>
      <goals>
        <goal>compile-no-fork</goal>
      </goals>
      <phase>package</phase>
    </execution>
  </executions>
</plugin>

skipNativeTests Alanı
Açıklaması şöyle
So far Mockito is not supported for tests. This can bring up problems for a high number of existing applications and result in big test refactoring projects. There are two possible ways to get it running: either exclude all mocking tests or simply skip native tests with setting the configuration skipNativeTests to true:
Örnek
Şöyle yaparız
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
    <skipNativeTests>true</skipNativeTests>
</configuration>




mvnup komutu

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