12 Aralık 2021 Pazar

assembly plugin - Executable Jar veya Zip Dosyası Oluşturur, Fat Jar İçin Kullanmayın

Giriş
Not : Bu plugin'i fat jar üretmek için kullanmamak lazım. shade plugin bu iş için daha iyi
Açıklaması şöyle
The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files.
Açıklaması şöyle
The Assembly plugin relies on a specific assembly.xml configuration file. It allows you to pick and choose which files will be included in the artifact. Note that the final artifact doesn't need to be a JAR: the configuration file lets you choose between available formats e.g. zip, war, etc.

The plugin manages common use-cases by providing pre-defined assemblies. The distribution of self-contained JARs is among them.
Goaller
single

Fat Jar
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>3.1.1</version>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
       <id>assemble-all</id>
       <phase>package</phase>
       <goals>
         <goal>single</goal>
       </goals>
     </execution>
   </executions>
 </plugin>
Executable Jar
Spring projeleri için çalışmaz!. Aslında Fat Jar ile Executable Jar aynı şey. Aradaki tek fark birisinde mainClass belirtilmesi.

Örnek
Şöyle yaparız. Burada mainClass ve execution goal belirtiliyor.
<plugin>
  <artifactid>maven-assembly-plugin</artifactid>
  <configuration>
    <descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef> <!--1-->
</descriptorRefs>
<archive>
  <manifest>
    <mainClass>com.foo.executable.jar.ExecutableJarApplication</mainClass> <!--2-->
  </manifest>
</archive>
  </configuration>
  <executions>
    <execution>
  <goals>
    <goal>single</goal> <!--3-->
  </goals>
  <phase>package</phase> <!--4-->
</execution>
  </executions>
</plugin>
Açıklaması şöyle. Burada tek gıcık nokta çıktı olarak verilen jar ismi foo-1.0-SNAPSHOT-jar-with-dependencies.jar şeklinde.
1. Reference the pre-defined self-contained JAR configuration
2. Set the main class to execute
3. Execute the single goal
4. Bind the goal to the package phase i.e. after the original JAR has been built
Running mvn package yields two artifacts:

<name>-<version>.jar
<name>-<version>-with-dependencies.jar
The first JAR has the same content as the one that would have been created without the plugin. The second is the self-contained JAR. You can execute it like this:
java -jar target/executable-jar
Örnek
Şöyle yaparız
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>fully.qualified.MainClass</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>
Burada execution goal vermediğimiz için şöyle kullanırız
mvn clean compile assembly:single
Çalıştırmak için şöyle yaparız
java -cp target/s2-app-1.0-SNAPSHOT-jar-with-dependencies.jar
Örnek
Şöyle yaparız.
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>YOUR MAIN CLASS HERE</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Zip



Hiç yorum yok:

Yorum Gönder

Local Snapshot Kullanmak

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