使用 Maven 创建具有依赖项的可运行 jar 并将其与其他文件一起打包到 zip 中

发布于 2024-12-19 00:19:16 字数 4341 浏览 5 评论 0原文

我已经查看了所有内容并看到了一些类似的问题和答案,但似乎没有一个与我想要实现的目标完全一致。我目前能够成功构建一个具有依赖项的可运行 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>

I have looked all over and have seen some similar questions and answers but nothing that seems to line up perfectly with what I am trying to achieve. I am currently able to successfully build a runnable jar with dependencies, which is great. The relevant section of my POM is this:

        <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>   

Now what I want to do is take the runnable jar that's created and put that into a zip file with some other files that are in a directory. I've tried adding another descriptor that points to a XML assembly file that tries to create the zip, but there is a dependency problem where it can't find the JAR file being created in the above step. I can't figure out how to specify for one to run first. I've searched high and low and just can't figure out what the best way to do it is -- there are many answers out there regarding multiple modules, multiple plugin calls, dependency sets, and so on. I'm just looking for the best practice and simplest approach.

Thanks!

Edit: So I seem to have achieved what I was trying to do by using this approach:

            <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>

And then a simply assembly:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-12-26 00:19:16

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

Create another module that depends on the current one that does the assembly of the executable jar and the rest of the stuff. Remember the Maven mantra .. one output artifact per module ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文