Using Maven Assembly Plugin to make an independent jar file.

1. To use the Assembly Plugin in Maven, you simply need to:

  • choose or write the assembly descriptor to use,
  • configure the Assembly Plugin in your project's pom.xml, and
  • run "mvn assembly:single" on your project.

2. How to configure it in pom.xml:

        <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <archive>
                      <manifest>
                          <addClasspath>true</addClasspath>
                          <mainClass>com.redhat.gss.jaxbTest.Main</mainClass>
                      </manifest>
                    </archive>
                    <descriptors>
                        <descriptor>src/main/assembly/descriptor.xml</descriptor>
                    </descriptors>
                    <outputDirectory>${basedir}/target/bin</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>

3. pre-defined desrciptor files

    This descriptor specifies the type of assembly archive to create, the contents of the assembly, and the ways in which dependencies or its modules are bundled with an assembly.There are some pre-defined desrciptor files. Below is the jar-with-dependencies 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">
  <!-- TODO: a jarjar format would be better -->
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>
     It contains the binary output of your project, along with its unpacked dependencies. You can replace

                   <descriptors>
                        <descriptor>src/main/assembly/descriptor.xml</descriptor>
                    </descriptors>

    with

                   <descriptorRefs>
                        <descriptor>jar-with-dependencies</descriptor>
                    </descriptorRefs>


4. Filter distribution files in descriptor.

    Below is an example of filtering files before adding them into the assembly.

<fileSets>
    <fileSet>
      <directory>${basedir}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>*.txt</include>
        <include>*.xml</include>
      </includes>
      <excludes>
        <exclude>README.txt</exclude>
        <exclude>NOTICE.xml</exclude>
      </excludes>
    </fileSet>
  </fileSets>

    The above descriptor tells the assembly plugin to include all txt and xml files in ${basedir}, but exclude README.txt and NOTICE.xml.

5. Using Maven Archiver to  configure the Manifest file (META-INF/MANIFEST.MF)

    The manifest is a special file that can contain information about the files packaged in a JAR file.

    (see:http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

             http://maven.apache.org/shared/maven-archiver/examples/manifest.html)
    Here is a simple example

        <archive>

                      <manifest>

                          <addClasspath>true</addClasspath>

                          <mainClass>com.redhat.gss.jaxbTest.Main</mainClass>

                      </manifest>

                    </archive>

    It adds the classpath to the manifest and specifies the main class. Thus the jar file is  an executable file.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值