maven assembly plugin

개요

project 의 output(module, site documentation, resource, etc..)을 단일 파일로 만들어 주는 plugin으로 zip, tar, tar.gz, tar.bz2, jar, dir, war 포맷을 지원함

사용

Goal

단일 goal 을 갖고 있으며 주로 package goal 에 binding 하여 사용

Version 2.2 부터 4개의 미리 정의된 assembly descriptor 가 있어서 일반적인 패턴은 descriptorRef 에 Pre-defined Descriptor를 지정하여 사용하거나 재활용하여 패키징 할 수 있다.(Ref: Pre-defined Descriptor Files)

Configuration

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <!-- NOTE: We don't need a groupId specification because the group is
             org.apache.maven.plugins ...which is assumed by default.
         -->
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        [...]
</project>


Execution: Building an Assembly

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      [...]
</project>

위와 같이 package goal 에 bind 하여 많이 사용함


Creating an Executable JAR

JVM 의 -jar switch 를 통해 실행 가능한 jar 파일을 만드는 기능으로 jar 내부에 dependacy jar 와 resource(xml, image, properties..) 가 들어있어야 자체적으로 실행 가능

실행 가능한 jar 파일을 만들기 위해서라면 maven shade plugin 를 사용하는 것을 권장

 Click here to expand...
<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          [...]
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>org.sample.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        [...]
      </plugin>
      [...]
</project>

archive 태그안의 element 는 maven archiver plugin 인의 기능을 사용하므로 maven-archiver 기능을 알아야 한다.

 

zip/tar.gz 으로 artifact 묶기

Pre-defined Descriptor중 bin descriptor 를 사용하면 편리하며 미리 정의된 템플릿은 다음과 같다.

 Click here to expand...
bin descriptor format:
<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>bin</id>
  <formats>
    <format>tar.gz</format>
    <format>tar.bz2</format>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>README*</include>
        <include>LICENSE*</include>
        <include>NOTICE*</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>${project.build.directory}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>*.jar</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>${project.build.directory}/site</directory>
      <outputDirectory>docs</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>


사전 정의된 템플릿을 수정하여 다음과 같이 custom bin descriptor 를 적용할 수 있다.

  1. assembly-descriptor 를  작성하고 프로젝트의 root 에 저장한다.

    assembly-desc-bin.xml
    <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>bin</id>
      <formats>
        <format>zip</format>
      </formats>
      <fileSets>
        <fileSet>
          <directory>${project.basedir}</directory>
          <outputDirectory>/</outputDirectory>
          <includes>
            <include>README*</include>
            <include>LICENSE*</include>
            <include>NOTICE*</include>
          </includes>
        </fileSet>
        <fileSet>
          <directory>${project.build.directory}</directory>
          <outputDirectory>/</outputDirectory>
          <includes>
            <include>*.jar</include>
            <include>*.pdf</include>
          </includes>
          <excludes>
          	<exclude>pom.xml</exclude>
          </excludes>
        </fileSet>
    	<!-- 예제 소스 패키징 -->
    	<!-- 예제소스 위치: src/test/java/com/snakeoil/example !-->
    	<fileSet>
          <directory>src/test/java/com/snakeoil/example</directory>
          <outputDirectory>/com/snakeoil/example</outputDirectory>
          <excludes>
    	    <exclude>**/target/**</exclude>
    	  </excludes>
        </fileSet>
      </fileSets>
    </assembly>
  2. pom.xml 에 plugin 설정 및 descriptors 에 기술

    <build>
     <plugins>
      <plugin>
    	<groupId>org.apache.maven.plugins</groupId>
    	<artifactId>maven-assembly-plugin</artifactId>
    	<version>2.4</version>
    	<configuration>
    		<descriptors>
                <!-- 위에서 작성한 파일명 기술 -->
    			<descriptor>assembly-desc-bin.xml</descriptor>
    		</descriptors>
    	</configuration>
    	<executions>
    		<execution>
    			<id>bin</id>
    			<phase>package</phase>
    			<goals>
    				<goal>single</goal>
    			</goals>
    		</execution>
    	</executions>
     </plugin>
     </plugins>
    </build>
  3. mvn package 를 실행
  4. ${artifactId}-${version}-bin.zip 으로 package 가 생성된다.

참고자료

Maven Home Page

기타