【问题标题】:Use Maven to create runnable jar with dependencies and pack that into a zip with other files使用 Maven 创建具有依赖项的可运行 jar,并将其与其他文件打包成一个 zip
【发布时间】:2012-01-11 01:10:01
【问题描述】:

我翻遍了所有地方,看到了一些类似的问题和答案,但似乎没有什么与我想要实现的目标完全一致。我目前能够成功构建一个带有依赖项的可运行 jar,这很棒。我的 POM 的相关部分是这样的:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>

                <archive>
                    <manifest>
                        <mainClass>com.main.whatever</mainClass>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>   

现在我要做的是将创建的可运行 jar 放入一个 zip 文件中,其中包含目录中的其他一些文件。我尝试添加另一个描述符,该描述符指向试图创建 zip 的 XML 程序集文件,但是存在一个依赖问题,它无法找到在上述步骤中创建的 JAR 文件。我不知道如何指定一个首先运行。我搜索了高低,只是无法弄清楚最好的方法是什么——关于多个模块、多个插件调用、依赖集等有很多答案。我只是在寻找最佳实践和最简单的方法。

谢谢!

编辑:所以我似乎已经通过使用这种方法实现了我想要做的事情:

            <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>jar-with-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>com.jasonwjones.hyperion.jessub.Jessub</mainClass>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>

                        <!-- this creates a warning during the Maven package, which I don't
                             love -->
                        <appendAssemblyId>false</appendAssemblyId>

                    </configuration>
                </execution>
                <execution>
                    <id>dist</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/dist.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

然后是一个简单的组装:

<assembly>
<id>dist</id>
<formats>
<format>zip</format>
</formats>
<files>
<file>
    <source>target/${project.artifactId}-${project.version}.jar</source>
  <!-- <source>target/${project.artifactId}-${project.version}-jar-with-dependencies.jar</source> -->
  <outputDirectory>/</outputDirectory>
</file>
</files>
<fileSets>
<fileSet>
  <directory>${project.basedir}</directory>
  <includes>
    <include>*.txt</include>
  </includes>
 <outputDirectory>/</outputDirectory> 
</fileSet>
<fileSet>
    <directory>${project.basedir}/src/main/resources</directory>
    <includes>
        <include>*</include>
    </includes>
    <outputDirectory>/</outputDirectory> 

</fileSet>

【问题讨论】:

  • 你可以添加你的程序集xml的内容吗?您还缺少插件声明中对程序集 xml 的引用。我已经完成了您过去尝试做的事情。您也正确定义了阶段(包)。
  • 请将您的解决方案发布为答案,而不是编辑。

标签: maven jar zip maven-assembly-plugin runnable


【解决方案1】:

创建另一个模块,该模块依赖于执行可执行 jar 和其余内容的组装的当前模块。记住 Maven 的口头禅 .. 每个模块一个输出工件 ;-)

【讨论】:

  • 我对模块的工作方式仍然很模糊(我需要阅读它们)......但我确实通过使用另一种方法让它工作,我将在原始评论中发布得到你的想法。我不确定我的做法是不是 Maveny...
  • 一个模块只是另一个 maven 项目(单独的文件夹和 pom.xml),在你的情况下它会依赖于可运行的 jar。
猜你喜欢
  • 2011-04-12
  • 1970-01-01
  • 2021-12-25
  • 2020-08-17
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
相关资源
最近更新 更多