Подтвердить что ты не робот

Лучшая практика Maven для создания искусственного артефакта zip

Предположим, что мне нужно управлять артефактом, который состоит из произвольной структуры папки/файла, свернутой как zip-архив. Мне непонятно, как это сделать в Maven таким образом, который наилучшим образом подходит для "пути Maven".

Я знаю, что нет типа упаковки "zip". Означает ли это, что в Maven нет универсального жизненного цикла, чтобы просто взять то, что у меня есть в папке ресурсов, закрепить его и установить/развернуть в свои репозитории?

Я ищу варианты, с оценками того, как каждый вариант удовлетворяет мое требование следовать по пути maven, так как я не хочу брать на себя очевидные наказания за отклонение от золотого пути.,

4b9b3361

Ответ 1

Определите, какой классификатор вы будете использовать для своего файла 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

Ответ 2

Заполните сборку /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         -ресурсов/.xml/свойства         -Прочти меня         -start.sh

Ответ 3

В вашем проекте 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>