maven - 使用 Maven 生成的带有依赖项和其他一些文件的 jar 创建一个 tar

标签 maven maven-assembly-plugin

我是 maven 的新手,它的学习主题很有趣。 使用以下命令成功创建具有依赖项的 jar:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>

    <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <executions>
            <execution>
            <phase>package</phase>
            <goals>
            <goal>single</goal>
            </goals>
            </execution>
        </executions>
        </configuration>
    </plugin>

现在,我必须将一些 shell 脚本和生成的 jar 包含到 tar 中。 为此,我尝试了以下方法: 添加了

 <descriptors>
    <descriptor>hadoop-job.xml</descriptor>
 </descriptors>

到上面的脚本。

在 hadoop-job.xml 中,我将所需文件包含到 tar 中。

问题是首先生成 tar 并说在目标中找不到 *.jar。 有没有一种方法可以先安排 jar 创建,然后再安排 tar,因为这两种配置都位于程序集插件中。 或者 有没有先执行生成jar然后再生成tar的命令?

顺便说一下,我正在执行 mvn clean assembly:assembly -Dbinary=true。 帮我解决。谢谢。

最佳答案

这是一个组装构建 jar-with-dependencies 的示例,然后是包含此 jar-with-dependencies 的 tar(之前由 maven-assembly-plugin 构建)。在 tar 中,我还在 src/main/bin 文件夹中的 .sh 中包含了 shell 脚本文件。

首先是插件配置:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.3</version>
    <executions>
        <execution>
            <id>jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptorRefs>
                    <descriptorId>jar-with-dependencies</descriptorId>
                </descriptorRefs>
            </configuration>
        </execution>
        <execution>
            <id>all</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/assembly/all.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <outputDirectory>${project.build.directory}/assembly/</outputDirectory>
    </configuration>
</plugin>

执行顺序很重要,因为如果我想将 jar-with-dependencies 包含到我的 tar 中,我需要先构建它。
我将所有程序集放在同一个文件夹中,这就是为什么我在 executionsconfiguration 标签之外还有一个全局 configuration 标签>

然后是我的程序集文件 all.xml 的内容:

<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>all</id>
    <formats>
        <format>tar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory> 
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>**/*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <fileMode>755</fileMode>
        </fileSet>
    </fileSets>
</assembly>

includeBaseDirectory 在这里是为了避免我的 tar 包含带有项目名称和版本的根文件夹。
如果你没有指定outputDirectory标签,你的存档中的文件夹结构将从项目的根目录开始,例如它会包含你的jar路径为target/your-project-1.0-SNAPSHOT.jar.



编辑:我之前所做的插件配置可以简化为以下内容:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.3</version>
    <executions>
        <execution>
            <id>jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptorId>jar-with-dependencies</descriptorId>
                <descriptors>
                    <descriptor>src/assembly/all.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <outputDirectory>${project.build.directory}/assembly/</outputDirectory>
    </configuration>
</plugin>

原因是因为在 maven-assembly-plugin 中,descriptorIddescriptors 之前被读取,所以这对我们的情况有好处,但是如果不,我们需要两次执行,因为要注意顺序。 必须对 descriptors 的顺序进行同样的关注,它们是按阅读顺序执行的(这看起来很合乎逻辑,但我认为指出它可能会有用)。

关于maven - 使用 Maven 生成的带有依赖项和其他一些文件的 jar 创建一个 tar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29295781/

相关文章:

java.lang.NoClassDefFoundError - org/apache/spark/sql/hive/HiveContext

java - 如何使用 maven-assembly-plugin 包含 package.jar

maven - 创建 '--rsyncable' maven 程序集

maven - 在多模块项目中生成聚合 javadoc 时如何避免重复 fork

performance - 我可以让 maven-assembly-plugin 更快吗?

java - 从 Java 启动 Spring 应用程序的异常

java - ExceptionMapper 在 JerseyTest 框架中不起作用

android - 从 Maven 导入远程库

maven - 使用 Google Cloud Build 时如何将私有(private)存储库凭据传递给 maven docker 镜像

java - Android 构建在 AppCenter 上失败,但在 Android Studio 上构建良好