Maven assembly Plugin: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=External= * http://maven.apache.org/plugins/maven-assembly-plugin =Internal= * Maven")
 
No edit summary
Line 6: Line 6:


* [[Maven#Plugin|Maven]]
* [[Maven#Plugin|Maven]]
!!!External
* [http://maven.apache.org/plugins/maven-assembly-plugin/]
* [http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html]
* Sonatype Assemblies [http://www.sonatype.com/books/mvnref-book/reference/assemblies.html]
* Assembly Best Practices according to Sonatype [http://www.sonatype.com/books/mvnref-book/reference/assemblies-sect-best-practices.html]
!!!Internal
|[MavenFilesets]
|[How to Exclude a File from the Final Artifact|MavenHOW2#HowToExcludeAFileFromTheFinalArtifact]
!!!Overview
An "assembly" is a group of files, directories, and dependencies that are assembled into an archive format and distributed as a project's artifact. A module has only one artifact - usually a JAR - but we may want to distribute that jar together with documentation, source and supporting scripts in the form or an "atomic" archive. The way to do it is to define that archive as an "assembly".
!!!Notes
* The {{assembly.xml}} file should go under {{src/main/assembly}}. It also works if it goes into the {{basedir}}.
!!!Multi-Module Assemblies
|[MavenAssemblyPluginMultiModule]
!!!The simplest way to create a ZIP artifact
The following will create a test site content zip. The assembly plug in is the only "active" plug-in for a sub-module whose whole reason to exist is to create the content zip.
!!pom.xml
{{{
<plugins>
            ...
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            ...
}}}
!!assembly.xml
{{{
<assembly>
    <formats>
        <format>zip</format>
    </formats>
    <baseDirectory>.</baseDirectory>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>archimedes-test-site</directory>
        </fileSet>
    </fileSets>
</assembly>
}}}
!!!Jar With Dependencies
{{{
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>${project.build.outputDirectory}</directory>
    </fileSet>
  </fileSets>
</assembly>
}}}
!!!Create an executable jar
{{{
<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2-beta-5</version>
        <configuration>
          [...]
          <archive>
            <manifest>
              <mainClass>org.sample.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        [...]
      </plugin>
      [...]
</project>
}}}
!!!Create an assembly file without any suffix
By default, the assembly name is the &lt;projectname&gt;-&lt;assemblyid&gt;.&lt;ext&gt; If I just want it to be &lt;projectname&gt;.&lt;ext&gt;, don't use any
{{{
    <id>...</id>
}}}
in the assembly descriptor.
!!!Creating two assemblies for the same project
{{{
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
      <executions>
          <execution>
              <id>jar-with-all-dependencies</id>
      <phase>package</phase>
              <goals>
                  <goal>single</goal>
              </goals>
              <configuration>
                  <descriptors>
                      <descriptor>src/main/assembly/jar-with-all-dependencies.xml</descriptor>
                  </descriptors>
                  <archive>
                      <manifest>
                          <mainClass>${exec.main.class}</mainClass>
                      </manifest>
                  </archive>
              </configuration>
          </execution>
          <execution>
              <id>distribution-bundle</id>
              <phase>package</phase>
              <goals>
                    <goal>single</goal>
              </goals>
              <configuration>
                    <descriptors>
                            <descriptor>src/main/assembly/distribution-bundle.xml</descriptor>
                    </descriptors>
              </configuration>
          </execution>
          </executions>
</plugin>
}}}
!!!Directly Specifying the Name of the Assembly Result File
{{{
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <finalName>BLAH-${project.version}</finalName>
                    ...
}}}

Revision as of 02:02, 5 November 2016

External

Internal


!!!External

  • [1]
  • [2]
  • Sonatype Assemblies [3]
  • Assembly Best Practices according to Sonatype [4]

!!!Internal

|[MavenFilesets] |[How to Exclude a File from the Final Artifact|MavenHOW2#HowToExcludeAFileFromTheFinalArtifact]

!!!Overview

An "assembly" is a group of files, directories, and dependencies that are assembled into an archive format and distributed as a project's artifact. A module has only one artifact - usually a JAR - but we may want to distribute that jar together with documentation, source and supporting scripts in the form or an "atomic" archive. The way to do it is to define that archive as an "assembly".

!!!Notes

!!!Multi-Module Assemblies

|[MavenAssemblyPluginMultiModule]

!!!The simplest way to create a ZIP artifact

The following will create a test site content zip. The assembly plug in is the only "active" plug-in for a sub-module whose whole reason to exist is to create the content zip.

!!pom.xml

{{{ <plugins>

           ...
           <plugin>
               <artifactId>maven-assembly-plugin</artifactId>
               <configuration>
                   <descriptors>
                       <descriptor>assembly.xml</descriptor>
                   </descriptors>
               </configuration>
               <executions>
                   <execution>
                       <id>make-assembly</id>
                       <phase>package</phase>
                       <goals>
                           <goal>single</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>
           ...

}}}

!!assembly.xml

{{{ <assembly>

   <formats>
       <format>zip</format>
   </formats>
   <baseDirectory>.</baseDirectory>
   <includeBaseDirectory>false</includeBaseDirectory>
   <fileSets>
       <fileSet>
           <directory>archimedes-test-site</directory>
       </fileSet>
   </fileSets>

</assembly> }}}

!!!Jar With Dependencies

{{{ <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
 <id>jar-with-dependencies</id>
 <formats>
   <format>jar</format>
 </formats>
 <includeBaseDirectory>false</includeBaseDirectory>
 <dependencySets>
   <dependencySet>
     <unpack>true</unpack>
     <scope>runtime</scope>
   </dependencySet>
 </dependencySets>
 <fileSets>
   <fileSet>
     <directory>${project.build.outputDirectory}</directory>
   </fileSet>
 </fileSets>

</assembly> }}}


!!!Create an executable jar

{{{ <project>

 [...]
 <build>
   [...]
   <plugins>
     <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <version>2.2-beta-5</version>
       <configuration>
         [...]
         <archive>
           <manifest>
             <mainClass>org.sample.App</mainClass>
           </manifest>
         </archive>
       </configuration>
       [...]
     </plugin>
     [...]

</project>

}}}

!!!Create an assembly file without any suffix


By default, the assembly name is the <projectname>-<assemblyid>.<ext> If I just want it to be <projectname>.<ext>, don't use any {{{

   <id>...</id>

}}} in the assembly descriptor.

!!!Creating two assemblies for the same project

{{{ <plugin>

  <artifactId>maven-assembly-plugin</artifactId>
     <executions>
         <execution>
             <id>jar-with-all-dependencies</id>

<phase>package</phase>

              <goals>
                  <goal>single</goal>
              </goals>
              <configuration>
                  <descriptors>
                     <descriptor>src/main/assembly/jar-with-all-dependencies.xml</descriptor>
                  </descriptors>
                  <archive>
                     <manifest>
                         <mainClass>${exec.main.class}</mainClass>
                     </manifest>
                  </archive>
              </configuration>
          </execution>
          <execution>
              <id>distribution-bundle</id>
              <phase>package</phase>
              <goals>
                    <goal>single</goal>
              </goals>
              <configuration>
                    <descriptors>
                           <descriptor>src/main/assembly/distribution-bundle.xml</descriptor>
                    </descriptors>
              </configuration>
          </execution>
         </executions>
</plugin>

}}}

!!!Directly Specifying the Name of the Assembly Result File

{{{

   <build>
       <plugins>
           <plugin>
               <artifactId>maven-assembly-plugin</artifactId>
               <configuration>
                   <finalName>BLAH-${project.version}</finalName>
                   ...

}}}