Maven组装包只有项目和依赖jar

时间:2014-01-20 17:25:08

标签: maven maven-assembly-plugin

我有一个包含2个子模块的父项目。我想创建一个只包含项目和依赖jar的包。我面临的问题是所有源文件[java,scripts,configs]也包含在工件中,有人可以帮助如何实现这一点。 以下是我正在使用的描述符:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>binaries</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>

    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>*:sub1</include>
                <include>*:sub2</include>
            </includes>
            <binaries>
                <outputDirectory>${artifactId}</outputDirectory>
                <unpack>true</unpack>
                <unpackOptions>
                    <excludes>                      
                        <exclude>**/resources/**</exclude>
                        <exclude>**/docs/**</exclude>
                        <exclude>**/src/**</exclude>
                    </excludes>
                </unpackOptions>
                <dependencySets>
                    <dependencySet>
                        <outputDirectory>${artifactId}/lib</outputDirectory>
                        <useProjectArtifact>true</useProjectArtifact>
                        <unpack>false</unpack>
                        <scope>runtime</scope>
                    </dependencySet>
                </dependencySets>
            </binaries>
        </moduleSet>
    </moduleSets>

</assembly>

1 个答案:

答案 0 :(得分:1)

我建议使用这种汇编描述符:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  <id>xyz</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>true</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
          <useProjectArtifact>false</useProjectArtifact>
          <useTransitiveDependencies>true</useTransitiveDependencies>
          <outputDirectory>lib</outputDirectory>
          <unpack>false</unpack>
          <excludes>
            <exclude>${project.groupId}:*:*</exclude>
          </excludes>
      </dependencySet>
  </dependencySets>
  <moduleSets>
    <moduleSet>
      <useAllReactorProjects>true</useAllReactorProjects>
      <binaries>
        <outputDirectory>modules</outputDirectory>
        <unpack>false</unpack>
      </binaries>
    </moduleSet>
  </moduleSets>
</assembly>

根据您的描述符,我建议您使用<includeBaseDirectory>true</includeBaseDirectory>因为您使用${artifactId}为所有ouputDirectories添加前缀。

上面的描述符将创建一个zip文件,其中包含包含依赖项的子文件夹lib和包含多模块构建模块的文件夹modules。 此外,我建议创建一个separate module which is responsible for creating the archive

如果您的结果zip包含java源文件等,则说明您正在包装jar的模块中进行组装步骤。