maven zip uber-jar 和 shell 脚本

maven zip uber-jar and shell script

我希望 maven 将 shade-plugin 创建的 uber-jar 和 all_files 目录中的 shell 脚本结合起来。

项目结构如下所示:

all_files/
    mvn_script.sh
    projB-shaded.jar

    maven_project/
        guide/
            parent-pom.xml
        projA/
            pom.xml
        projB/
            pom.xml

jar 由projectB 的pom 文件生成,然后放入最外层的文件夹中,准备用shell 脚本压缩。原因是shell脚本可以调用jar文件执行工程

我希望其他程序员能够毫无顾虑地轻松解压缩文件和 运行 shell 脚本。而且我还需要让 maven 将脚本和 jar 打包在一起。我不确定如何在 shaded 插件中实现它。

注意:我不想使用assembly-plugin,因为它不能很好地打包依赖的jar。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <minimizeJar>true</minimizeJar>
          <outputFile>../../guide/${project.artifactId}-${project.version}-shaded.jar</outputFile>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <manifestEntries>
                <Main-Class>projB.classB</Main-Class>
              </manifestEntries>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>                    

您不想使用 maven-assembly-plugin 创建 uber-jar。但是你会想用它来创建那个 ZIP。

目前,您的 maven-shade-plugin 绑定到 package 阶段。您可以将该执行转移到 prepare-package 阶段(因为它实际上准备了您的最终包装)添加 maven-assembly-plugin 绑定到 package 阶段的执行。您的程序集将基于阴影 JAR(自阴影插件执行后将存在)和 shell 脚本创建一个 ZIP。

样本描述符如下:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>your-id</id>
    <formats>
        <format>zip</format>
    </formats>
    <files>
        <file>
            <source>${project.build.directory}/projB-shaded.jar</source>
            <outputDirectory>/</outputDirectory>
        </file>
        <file>
            <source>/path/to/mvn_script.sh</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
</assembly>

使用以下 POM 配置:

<plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <!-- current configuration -->
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>assemble</id>
            <goals>
                <goal>single</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <descriptors>
                    <descriptor>/path/to/assembly.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

根据 Maven standard directory layout.

,程序集描述符的典型位置在 src/assembly