shell - maven zip uber-jar 和 shell 脚本

标签 shell maven jar maven-assembly-plugin maven-shade-plugin

我希望 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 绑定(bind)到 package 阶段。您可以将该执行转移到 prepare-package 阶段(因为它实际上准备了您的最终包装)添加 maven-assembly-plugin 的执行绑定(bind)到 阶段。您的程序集将基于着色 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 下.

关于shell - maven zip uber-jar 和 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35368011/

相关文章:

shell - 在 Unix 中匹配文件字符串

java - Spring Boot中分项目的使用方法

java - 如何将一组类从 jar 库映射到 jpa 实体以创建数据库表

java - 如何配置 exec-maven-plugin 类路径

xml - : 'identifiedType' 的重复定义

java - 图像在 .jar 文件中不起作用

shell - 如何直接转到联机帮助页上的特定(简短)选项

linux - shell 脚本 : parse blank spaces correctly

java - 加载jar文件中的资源目录

git - 有没有办法将 "git blame"的结果输出到文本文件?