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

С Maven, как я могу построить дистрибутив, у которого есть мой проект jar и все зависимые баночки?

У меня есть проект (типа "jar" ), который (очевидно) строит банку. Но этот проект имеет много зависимостей. Я хотел бы, чтобы Maven создавал "пакет" или "сборку", содержащий мою банку, все зависимые баночки и некоторые скрипты (для запуска приложения и т.д.).

Какой лучший способ сделать это? В частности, какой лучший способ получить иждивенцы в сборке?

4b9b3361

Ответ 1

Для одного модуля я бы использовал сборку, похожую на следующую (src/assembly/bin.xml):

<assembly>
  <id>bin</id>
  <formats>
    <format>tar.gz</format>
    <format>tar.bz2</format>
    <format>zip</format>
  </formats>
  <dependencySets>
    <dependencySet>
      <unpack>false</unpack>
      <scope>runtime</scope>
      <outputDirectory>lib</outputDirectory>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>src/main/command</directory>
      <outputDirectory>bin</outputDirectory>
      <includes>
        <include>*.sh</include>
        <include>*.bat</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

Чтобы использовать эту сборку, добавьте следующую конфигурацию в свой pom.xml:

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

В этом примере скрипты запуска/остановки, расположенные под src/main/command и вложенные в bin, зависимостей объединяются в lib. Настройте его в соответствии с вашими потребностями.

Ответ 2

Вот мое решение для создания дистрибутивного .zip (или .tar.gz/.tar.bz2), включая все зависимости в папке lib. Он будет:

  • Создайте jar с манифестом, включая зависимости каталога lib как путь к классам и основной класс для запуска при выполнении jar.
  • Скопируйте все зависимые баночки в каталог target/lib.
  • Создайте дистрибутив `zip с главной банкой и всеми зависимыми баночками каталога lib.

Выдержка из pom.xml:

<!-- create distributable -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>full.path.to.MainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>attached</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/main/resources/dist.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

dist.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>bin</id>
    <formats>
        <format>zip</format>
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/lib</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/site</directory>
            <outputDirectory>docs</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

dist.xml был получен из формата дескриптора bin здесь: http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#bin