食谱:如何生成程序集?

概括

这个秘籍描述了如何生成像 zip、tar.gz 或 tar.bz2 这样的程序集。

先决条件插件

以下是使用的插件列表:

插入 版本
assembly 2.4.1

样本生成的输出

attach-source-javadoc
|-- pom.xml
|-- src\
`-- target
    `-- apache-maven-cookbook-1.0-SNAPSHOT-bin
    `-- apache-maven-cookbook-1.0-SNAPSHOT-bin.tar.bz2
    `-- apache-maven-cookbook-1.0-SNAPSHOT-bin.tar.gz
    `-- apache-maven-cookbook-1.0-SNAPSHOT-bin.zip

食谱

配置程序集描述符

要生成程序集,我们需要配置bin.xmlsrc/assembly目录中调用的程序集描述符。首先,我们指定想要的格式,即

<formats>
  <format>tar.gz</format>
  <format>tar.bz2</format>
  <format>zip</format>
</formats>

和想要的文件集,即

<fileSets>
  <fileSet>
    <includes>
      <include>README*</include>
    </includes>
  </fileSet>
  <fileSet>
    <directory>src/bin</directory>
    <outputDirectory>bin</outputDirectory>
    <includes>
      <include>*.bat</include>
    </includes>
    <lineEnding>dos</lineEnding>
  </fileSet>
  <fileSet>
    <directory>src/bin</directory>
    <outputDirectory>bin</outputDirectory>
    <includes>
      <include>hello</include>
    </includes>
    <lineEnding>unix</lineEnding>
    <fileMode>0755</fileMode>
  </fileSet>
  <fileSet>
    <directory>target</directory>
    <outputDirectory>lib</outputDirectory>
    <includes>
      <include>generate-assembly-*.jar</include>
    </includes>
  </fileSet>
</fileSets>

配置 Maven 程序集插件

我们assembly:single在阶段执行来自 Assembly 插件的目标package

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptor>src/assembly/bin.xml</descriptor>
    <finalName>apache-maven-cookbook-${pom.version}</finalName>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

运行 Maven

只需调用 Maven 来生成包:

mvn package

资源