Maven压缩超级JAR和Shell脚本

3

我希望maven将shade插件创建的超级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>                    
1个回答

2
您不希望使用maven-assembly-plugin创建超级JAR。但您将想要使用它来创建ZIP。
目前,您的maven-shade-plugin绑定到package阶段。您可以将其执行转移到prepare-package阶段(因为它实际上准备了最终打包),并添加一个绑定到package阶段的maven-assembly-plugin执行。您的装配将基于阴影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>

典型的组装描述符位置在src/assembly下,根据Maven标准目录布局

我如何将projB-shaded.jar和mvn_script.sh这两个文件添加到一个单独的zip文件中? - Abhi
2
在 prepare-package 中运行 shade 会导致失败。似乎它必须在 package 中运行,或者有其他配置可以允许它更早地运行? - puhlen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接