Wednesday, December 7, 2022

Maven assembly plugin

 HOWTO

Project input 

  • Java sources
  • resources files
  • 3rd party libraries for compilation only

Project output 

  • packaged JARs
  • packaged EARs
  • packaged WARs
  • config files
  • 3rd party libraries required for runtime
  • complete TAR file delivering the application

Artifact types

JAR

  • Java classes
  • descriptors

EAR

  • JARs - dependencies
  • descriptors

WAR 

  • JARs - dependencies 
  • Web code
  •  descriptors

 

 Test project

Sample assembly descriptor

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">

    <id>package-artifacts</id>
    <!-- Final distributive format. -->
    <formats>
        <format>tar</format>
    </formats>

    <!-- Exclude current module from distributive. -->
    <includeBaseDirectory>false</includeBaseDirectory>

    <!-- Project files to include to distributive.
    ${maven.multiModuleProjectDirectory} - path to root project folder. -->
    <fileSets>
        <fileSet>
            <!-- From directory. -->
        <directory>${maven.multiModuleProjectDirectory}/add-to-distribution/docs</directory>
            <!-- Directory in distributive. -->
            <outputDirectory>docs</outputDirectory>
            <includes>
                <!-- ** - include all files of any nesting. -->
                <include>**</include>
            </includes>
        </fileSet>
    </fileSets>

    <!-- Set of modules, included in distributive. -->
    <dependencySets>
        <dependencySet>
            <includes>
                <!-- Include all modules from POM-dependencies current module. -->
                <include>*</include>
            </includes>
            <!-- Target folder for modules in distributive. -->
            <outputDirectory>modules</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <useTransitiveDependencies>false</useTransitiveDependencies>
        </dependencySet>
    </dependencySets>

</assembly>

Run mvn install

INFO] -------------------------< dave:dist-assembly >-------------------------
[INFO] Building dist-assembly 1.0.0-SNAPSHOT                              [3/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (create-dist) @ dist-assembly ---
[INFO] Reading assembly descriptor: dist-assembly.xml
[WARNING] Cannot include project artifact: dave:dist-assembly:pom:1.0.0-SNAPSHOT; it doesn't have an associated file or directory.
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o  '*'

[INFO] Building tar : /git/java-tutorial/assembly/target/dist-assembly-1.0.0-SNAPSHOT-package-artifacts.tar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ dist-assembly ---
[INFO] Installing /git/java-tutorial/assembly/pom.xml to /home/dave/.m2/repository/dave/dist-assembly/1.0.0-SNAPSHOT/dist-assembly-1.0.0-SNAPSHOT.pom
[INFO] Installing /git/java-tutorial/assembly/target/dist-assembly-1.0.0-SNAPSHOT-package-artifacts.tar to /home/dave/.m2/repository/dave/dist-assembly/1.0.0-SNAPSHOT/dist-assembly-1.0.0-SNAPSHOT-package-artifacts.tar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for parent 1.0.0-SNAPSHOT:
[INFO] 
[INFO] parent ............................................. SUCCESS [  0.208 s]
[INFO] common ............................................. SUCCESS [  1.436 s]
[INFO] dist-assembly ...................................... SUCCESS [  0.301 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.049 s
[INFO] Finished at: 2022-12-08T20:50:54+01:00
[INFO] ------------------------------------------------------------------------
[dave@dave java-tutorial]$ tar tvf /git/java-tutorial/assembly/target/dist-1.0.0-SNAPSHOT-package-artifacts.tar
-rw-r--r-- dave/dave      5181 2022-12-08 20:47 modules/common-1.0.0-SNAPSHOT.jar
drwxr-xr-x dave/dave         0 2022-12-08 20:35 docs/
-rw-r--r-- dave/dave        12 2022-12-08 20:35 docs/file-to-add.txt

Project structure

./common
./common/src
./common/src/main
./common/src/main/java
./common/src/main/java/gc
./common/src/main/java/gc/LogGC.java
./common/src/main/java/generics
./common/src/main/java/generics/AbstractValue.java
./common/src/main/java/generics/BoxInteger.java
./common/src/main/java/generics/BoxString.java
./common/src/main/java/generics/GenericsTutorial.java
./common/src/main/java/ldap
./common/src/main/java/ldap/TestLDAP.java
./common/pom.xml
./assembly
./assembly/dist-assembly.xml
./assembly/pom.xml
./add-to-distribution
./add-to-distribution/docs
./add-to-distribution/docs/file-to-add.txt
./pom.xml


Sample code

 ear

      <plugin>
        <artifactId>maven-ear-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          <!-- configuration elements goes here -->
        </configuration>
      </plugin>

Packaged file name

${project.build.directory}/${project.build.finalName}.${project.packaging}

Copy all project dependencies to target/lib

<plugin>
   <artifactId>maven-dependency-plugin</artifactId>
   <executions>
      <execution>
         <phase>package</phase>
         <goals>
             <goal>copy-dependencies</goal>
         </goals>
             <configuration>
               <outputDirectory>${project.build.directory}/lib</outputDirectory>
             </configuration>
       </execution>
    </executions>
</plugin>

Exclude transitive dependencies 

  <dependency>
            <groupId>com.dave</groupId>
            <artifactId>dave-module</artifactId>
            <version>1.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
</dependency>

 war

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
        <configuration>
          <webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>

assembly 

Exclude artifacts from dependency set

      <dependencySets>
        <dependencySet>
          ....
          <excludes>
            <exclude>commons-lang:commons-lang</exclude>
            <exclude>log4j:log4j</exclude>
          </excludes>
        </dependencySet>
        ....
      </dependencySets>

Full assembly descriptor

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
      <id/>
      <formats/>
      <includeBaseDirectory/>
      <baseDirectory/>
      <includeSiteDirectory/>
      <containerDescriptorHandlers>
        <containerDescriptorHandler>
          <handlerName/>
          <configuration/>
        </containerDescriptorHandler>
      </containerDescriptorHandlers>
      <moduleSets>
        <moduleSet>
          <useAllReactorProjects/>
          <includeSubModules/>
          <includes/>
          <excludes/>
          <sources>
            <useDefaultExcludes/>
            <outputDirectory/>
            <includes/>
            <excludes/>
            <fileMode/>
            <directoryMode/>
            <fileSets>
              <fileSet>
                <useDefaultExcludes/>
                <outputDirectory/>
                <includes/>
                <excludes/>
                <fileMode/>
                <directoryMode/>
                <directory/>
                <lineEnding/>
                <filtered/>
                <nonFilteredFileExtensions/>
              </fileSet>
            </fileSets>
            <includeModuleDirectory/>
            <excludeSubModuleDirectories/>
            <outputDirectoryMapping/>
          </sources>
          <binaries>
            <outputDirectory/>
            <includes/>
            <excludes/>
            <fileMode/>
            <directoryMode/>
            <attachmentClassifier/>
            <includeDependencies/>
            <dependencySets>
              <dependencySet>
                <outputDirectory/>
                <includes/>
                <excludes/>
                <fileMode/>
                <directoryMode/>
                <useStrictFiltering/>
                <outputFileNameMapping/>
                <unpack/>
                <unpackOptions>
                  <includes/>
                  <excludes/>
                  <filtered/>
                  <nonFilteredFileExtensions/>
                  <lineEnding/>
                  <useDefaultExcludes/>
                  <encoding/>
                </unpackOptions>
                <scope/>
                <useProjectArtifact/>
                <useProjectAttachments/>
                <useTransitiveDependencies/>
                <useTransitiveFiltering/>
              </dependencySet>
            </dependencySets>
            <unpack/>
            <unpackOptions>
              <includes/>
              <excludes/>
              <filtered/>
              <nonFilteredFileExtensions/>
              <lineEnding/>
              <useDefaultExcludes/>
              <encoding/>
            </unpackOptions>
            <outputFileNameMapping/>
          </binaries>
        </moduleSet>
      </moduleSets>
      <fileSets>
        <fileSet>
          <useDefaultExcludes/>
          <outputDirectory/>
          <includes/>
          <excludes/>
          <fileMode/>
          <directoryMode/>
          <directory/>
          <lineEnding/>
          <filtered/>
          <nonFilteredFileExtensions/>
        </fileSet>
      </fileSets>
      <files>
        <file>
          <source/>
          <sources/>
          <outputDirectory/>
          <destName/>
          <fileMode/>
          <lineEnding/>
          <filtered/>
        </file>
      </files>
      <dependencySets>
        <dependencySet>
          <outputDirectory/>
          <includes/>
          <excludes/>
          <fileMode/>
          <directoryMode/>
          <useStrictFiltering/>
          <outputFileNameMapping/>
          <unpack/>
          <unpackOptions>
            <includes/>
            <excludes/>
            <filtered/>
            <nonFilteredFileExtensions/>
            <lineEnding/>
            <useDefaultExcludes/>
            <encoding/>
          </unpackOptions>
          <scope/>
          <useProjectArtifact/>
          <useProjectAttachments/>
          <useTransitiveDependencies/>
          <useTransitiveFiltering/>
        </dependencySet>
      </dependencySets>
      <repositories>
        <repository>
          <outputDirectory/>
          <includes/>
          <excludes/>
          <fileMode/>
          <directoryMode/>
          <includeMetadata/>
          <groupVersionAlignments>
            <groupVersionAlignment>
              <id/>
              <version/>
              <excludes/>
            </groupVersionAlignment>
          </groupVersionAlignments>
          <scope/>
        </repository>
      </repositories>
      <componentDescriptors/>
    </assembly>

No comments:

Post a Comment