Maven创建ad hoc zip工件的最佳实践

时间:2011-10-20 14:51:01

标签: maven zip

假设我需要管理一个由作为zip存档汇总的aribtrary文件夹/文件结构组成的工件。我不清楚如何以最适合“Maven方式”的方式在Maven中实现这一目标。

我知道没有“zip”包装类型。这是否意味着Maven中没有通用的生命周期,只需将我在资源文件夹中的内容,拉链并将其安装/部署到我的存储库中即可?

我正在寻找选择,评估每个选项如何满足我遵循maven方式的要求,因为我不希望招致明显的偏离黄金道路的惩罚。 。 。

3 个答案:

答案 0 :(得分:63)

决定将用于zip文件的分类器,为了论证,我们会说它是sample

在您的项目中创建文件assembly/sample.xml

使用以下内容填写assembly/sample.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>sample</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <outputDirectory>/</outputDirectory>
      <directory>some/directory/in/your/project</directory>
    </fileSet>
  </fileSets>
  <!-- use this section if you want to package dependencies -->
  <dependencySets>
    <dependencySet>
      <outputDirectory>lib</outputDirectory>
      <excludes>
        <exclude>*:pom</exclude>
      </excludes>
      <useStrictFiltering>true</useStrictFiltering>
      <useProjectArtifact>false</useProjectArtifact>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

将此添加到您的pom的build部分

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>create-distribution</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>assembly/sample.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

因此,它应该创建并安装you-project-name-VERSION-sample.zip

我建议你阅读Sonatype的maven书中关于装配的章节: https://books.sonatype.com/mvnref-book/reference/assemblies.html

另外,阅读汇编格式规范:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

答案 1 :(得分:3)

使用以下内容填写assembly / sample.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>install</id>
  <baseDirectory>/</baseDirectory>
  <formats>
  <format>zip</format>
</formats>
<fileSets>
  <fileSet>
    <includes>
      <include>README*</include>
      <include>*.sh</include>
    </includes>
  </fileSet>
  <fileSet>
    <directory>target/classes/</directory>
    <outputDirectory>resources</outputDirectory>
    <includes>
      <include>*.properties</include>
       <include>*.xml</include>
    </includes>
    <lineEnding>dos</lineEnding>
  </fileSet>
  <!-- 
  <fileSet>
    <directory>target/</directory>
    <outputDirectory>lib</outputDirectory>
    <includes>
      <include>*.jar</include>
    </includes>
    <lineEnding>dos</lineEnding>
  </fileSet>
   -->
</fileSets>
 <!-- use this section if you want to package dependencies -->
  <dependencySets>
    <dependencySet>
      <outputDirectory>lib</outputDirectory>
      <excludes>
        <exclude>*:pom</exclude>
      </excludes>
      <useStrictFiltering>true</useStrictFiltering>
      <useProjectArtifact>true</useProjectArtifact>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

在pom.xml文件中添加以下代码。

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptor>assembly/sample.xml</descriptor>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

将命令运行为mvn package -P dev -Dmaven.test.skip

将创建包含以下内容的zip文件:      xxxx.zip           -lib /的的.jar           -resources / .XML /属性           -readme           -start.sh

答案 2 :(得分:0)

在您的maven项目中,创建一个文件夹assembly。 在组装文件夹中添加zip.xml文件。 在zip.xml中添加以下代码,以将项目内容打包到zip中。

<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>zip</id>
<formats>
    <format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
    <fileSet>
        <directory>directoryName of your project</directory>
        <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>