Maven best practice for creating ad hoc zip artifact

后端 未结 3 1730
孤街浪徒
孤街浪徒 2020-12-02 14:22

Assume that I need to manage an artifact that consists of an aribtrary folder / file structure rolled up as a zip archive. It\'s not clear to me how to accomplish this in M

相关标签:
3条回答
  • 2020-12-02 14:43

    Decide what classifier you will use for your zip file, for sake of argument let's say it would be sample.

    In your project create file assembly/sample.xml

    Fill in assembly/sample.xml with something like this:

    <?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>
    

    Add this to your pom's build section

      <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>
    

    As a result it should create and install you-project-name-VERSION-sample.zip.

    I suggest you read chapter on assemblies from Sonatype's maven book: https://books.sonatype.com/mvnref-book/reference/assemblies.html

    Also, read assembly format specification: http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

    0 讨论(0)
  • 2020-12-02 14:44

    Fill in assembly/sample.xml with something like this:

    <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>
    

    add below codes in pom.xml file.

    <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>
    

    run commands as mvn package -P dev -Dmaven.test.skip

    will create the zip files that include : xxxx.zip -lib/.jar -resources/.xml/properties -readme -start.sh

    0 讨论(0)
  • 2020-12-02 14:50

    In your maven project create a folder assembly. Add zip.xml file in the assembly folder. Add below code in zip.xml to package the content of your project in to 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>
    

    0 讨论(0)
提交回复
热议问题