用于创建临时压缩工件的Maven最佳实践



假设我需要管理一个工件,该工件由一个任意文件夹/文件结构组成,该结构卷成一个zip归档文件。我不清楚如何在Maven中以最适合"Maven方式"的方式完成这一任务。

我知道没有"zip"包装类型。这是否意味着Maven中没有通用的生命周期来简单地获取资源文件夹中的内容,将其压缩,然后将其安装/部署到存储库中?

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

决定您将为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

在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.ziplib/ . jar- - -/ . xml/属性自述-start.sh

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>

最新更新