12 Aralık 2021 Pazar

enforcer plugin - JDK Sürümü, İşletim Sistemi Gibi Kuralları Kontrol Eder

Giriş
Açıklaması şöyle
The Enforcer plugin provides goals to control certain environmental constraints such as Maven version, JDK version and OS family along with many more built-in rules and user created rules.
Goals
Sadece enforce diye bir goal var. Bu goal mvn enforcer:enforce ile direkt çalışmıyor. Açıklaması şöyle
The reason is that by calling Maven via mvn enforcer:enforce you have not started a life cycle (something like clean, package or alike). You have requested to execute the goal enforce of the Maven Enforcer Plugin.

This means in the end there is a difference for configuring plugins between calling a life cycle and a goal.
Çalıştırmak için bir lifecycle belirtmek lazım. Açıklaması şöyle
That's easy to accomplish by just calling a life cycle phase like initialize (you can also use verify or package instead of initialize only takes longer to run.):
Yani şunlardan birisi olur
mvn initialize
mvn verify
mvn package
Eğer mvn enforcer:enforce ile direkt çalıştırmak istersek şöyle yaparız. id olarak default-cli değerini kullanmak gerekiyor
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>3.0.0-M3</version>
  <executions>
    <execution>
      <id>default-cli</id>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <requireMavenVersion>
            <version>3.5.0</version>
          </requireMavenVersion>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>
Açıklaması şöyle
The name default-cli is, as the name implies, the default used for command line execution. This means you can define configuration/execution which are only used for calling goals from command line and not part of the life cycle.
Kullanım Örnekleri
dependencyConvergence  Tag
Örnek
Şöyle yaparız. Burada kullanılan kütüphaneler arasında farklı junit sürümleri varsa yapılandırma durduruluyor
<plugin>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>${maven.enforcer.plugin.version}</version>
  <executions>
    <execution>
      <id>enforce-version</id>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <dependencyConvergence>
            <excludedScopes>
              <scope>compile</scope>
              <scope>runtime</scope>
            </excludedScopes>
            <includes>
              <include>junit</include>
            </includes>
          </dependencyConvergence>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>
Örnek - dependencyConvergence + fail
Şöyle yaparız. Burada kullanılan kütüphaneler arasında farklı sürümler varsa bile yapılandırma durdurulmuyor sadece log basılıyor
<execution>
  <id>enforce-version</id>
  <goals>
    <goal>enforce</goal>
  </goals>
  <configuration>
    <fail>false</fail>
    <rules>
      <dependencyConvergence>
         ...
      </dependencyConvergence>
    </rules>
  </configuration>
</execution>
requireProperty Tag
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer.plugin</artifactId>
  <version>3.0.0-M2</version>
  <executions>
    <execution>
      <id>enforce-properties</id>
        <goals>
          <goal>enforce </goal>
        </goals>
        <configuration>
          <rules>
            <requireProperty>
              <property>os</property>
              <message>Provide as -Dos='centos'</message>
            </requireProperty>
          </rules>
          <fail>true</fail>
        </configuration>
    </execution>
  </executions>
</plugin>
requireJavaVersion Tag
Örnek 
Şöyle yaparız
<plugin>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>${maven.enforcer.plugin.version}</version>
  <executions>
    <execution>
      <id>enforce-tools</id>
        <goals>
          <goal>enforce</goal>
        </goals>
      <configuration>
        <rules>
          <requireJavaVersion>
            <!-- JDK 1.8+ is required for compilation. -->
            <version>[1.8.0,)</version>
          </requireJavaVersion>
          <enforceBytecodeVersion>
            <maxJdkVersion>1.8</maxJdkVersion>
            <excludes>
              <exclude>com.tngtech.archunit:archunit</exclude>
            </excludes>
            <ignoreClasses>
              <ignoreClass>org.bson.codecs.record.*</ignoreClass>
            </ignoreClasses>
          </enforceBytecodeVersion>
        </rules>
      </configuration>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.mojo</groupId>
        <artifactId>extra-enforcer-rules</artifactId>
      <version>1.5.1</version>
    </dependency>
  </dependencies>
</plugin>



Hiç yorum yok:

Yorum Gönder

Local Snapshot Kullanmak

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