如何从多模块 Maven 项目构建可执行 jar?

2023-12-21

我是 Maven 的初学者,很多东西都不明白。我可以构建简单的可执行 jar,但如何将多模块 Maven 项目构建为可执行 jar 对我来说很神奇。所以,我有三个项目。 家长:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Test</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>Main</module>
        <module>Dep</module>
    </modules>
</project>

还有两个子项目:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Test</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Main</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Dep</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

and:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Test</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Dep</artifactId>
</project>

主模块有带有 main 方法的 Main 类(笑)

public class Main {
    public static void main(String[] args) {
        Hello hello = new Hello();
        System.out.println(hello.sayHello());
    }
}

Hello 类在 Dep 模块中定义。我应该在我的 poms 中添加什么来构建可执行 jar,并使用 Main 模块中的 Main 类作为入口点?


您需要将 pom 更改为artifactid Main。 您需要添加 maven-assemble-plugin

在它的配置中,您可以选择在清单中指定 mainClass。这应该是主类的完全限定名称。

<project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>Test</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>Main</artifactId>

    <properties>
        <!-- plugins -->
        <maven.assembly.plugin.version>2.4</maven.assembly.plugin.version>
        <!-- dependencies -->
        <dep.version>1.0-SNAPSHOT</dep.version>
    </properties>

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

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>${maven.assembly.plugin.version}</version>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>Main</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Dep</artifactId>
            <version>${dep.version}</version>
        </dependency>
    </dependencies>
</project>

在父项目上开始构建后,它应该在模块 Main 的目标子文件夹中创建可执行 jar。 (我得到的是一个名为 Main-1.0-SNAPSHOT-jar-with-dependency.jar 的 jar)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从多模块 Maven 项目构建可执行 jar? 的相关文章

随机推荐

  • 这可能是初学者最好的backbonejs教程[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我对backbone js很感兴趣 但当我学习的时候 在创建结构时有很多困惑 有人可以向我展示一个带有很好示例的简单教程吗 这对所有新学习
  • Bootstrap Carousel 多个项目一次移动一个项目[重复]

    这个问题在这里已经有答案了 我尝试使用引导程序创建多个项目轮播 我的问题是它会同时移动所有 3 个项目 而不是仅移动一个 请在此处查看演示 http plnkr co edit Fl0HZaU5x5ZkPEVo87u3 p preview
  • 双重释放或腐败(fasttop)

    我的代码的以下部分在执行时给了我这条消息 检测到glibc a out 双重释放或损坏 fasttop 0x08e065d0 问题就在这里 temp2 first 基本上 当您释放 temp2 时 您首先释放 而不是此处分配的内存 temp
  • 无法将动态生成的图像从任何浏览器粘贴到 MS Word

    我有一个生成图像然后将其推送到浏览器的应用程序 图像显示完全没有困难 还可以右键保存 并且可以毫无问题地粘贴到Gimp等应用程序中 但不能粘贴到MS Word中 我摆弄了应用程序的各个方面 以确保内容类型和所有其他标题都正确 但这对粘贴图像
  • 如何设置上传文件的最大大小

    我正在使用 JHipster 开发基于 Spring Boot 和 AngularJS 的应用程序 我的问题是如何设置上传文件的最大大小 如果我尝试上传到大文件 我会在控制台中收到以下信息 DEBUG 11768 io 8080 exec
  • 如何将回调函数传递给 StreamController

    我想知道我正在创建这样的 StreamController class StreamController controller new StreamController onListen onListen onPause onPause o
  • 谷歌云存储交易?

    看来GCS没有任何交易机制 它是否正确 我希望能够进行长期交易 例如 如果我可以启动一个事务并指定过期时间 如果未在 X 时间内提交 它将自动回滚 那就太好了 然后我可以使用这个句柄插入对象 组合 删除等 如果一切顺利 发出 isCommi
  • UIView 的 contentScaleFactor 取决于实现drawRect:?

    我偶然发现了一件奇怪的事情 看起来像UIView s contentScaleFactor即使在 Retina 设备上也始终为 1 除非您实现drawRect 考虑这段代码 interface MyView UIView end imple
  • 为 iOS 应用内购买提供折扣代码

    所以我知道 iOS 中没有用于应用内购买的促销代码 我想知道的是 苹果会拒绝这种机制吗 提供两种应用内购买 一种是全价 例如 9 99 美元 另一种是折扣价 例如 7 99 美元 对于同一商品 当您点击 7 99 美元的价格时 系统首先会要
  • 如何使用向量 SSE 运算将图像像素数据的字节数组转换为灰度

    我在转换存储在中的图像数据时遇到问题byte array到灰度 我想使用矢量 SIMD 操作 因为将来需要编写 ASM 和 C DLL 文件来测量操作时间 当我阅读有关 SIMD 的内容时 我发现 SSE 命令是在 128 位寄存器上运行的
  • 使用 C# 生成 Word 文档

    给定一个邮寄地址列表 我需要打开一个现有的 Word 文档 该文档的格式适合打印标签 然后将每个地址插入到表的不同单元格中 当前的解决方案打开Word应用程序并移动光标以插入文本 但是 在阅读了安全问题以及与从 Web 应用程序打开较新版本
  • 将除法结果舍入到 c 中的下一个整数

    我编写代码来显示多个页面 每页最多 5 行 其中包含来自一个列表的人员 PRE page number of the page we want to show starting with 1 RETURNS pagenumber of th
  • sbt-buildinfo生成的对象无法被引用

    我正在使用前面提到的 sbt 插件来获取我正在开发的应用程序的版本 该项目有子模块 这是主要的build sbt lazy val abandon project in file aggregate base cli gui depends
  • 类似于Pyspark中set_index的方法

    pyspark DataFrame对象中类似于pandas DataFrame set index的方法是什么 你能建议一下吗 None
  • 在 VSTS 上归档存储库

    我们有一个最初是在 NET Framework 中构建的应用程序 我们通过 VSTS 上的存储库对其进行管理 我们现在将其转换为 NET Core 我想使用我们用于 NET Framework 版本的原始名称在 VSTS 上为其创建一个新存
  • 是否可以在 Swift 上创建常量文件?

    我有大约 10 个 Swift 3 应用程序 它们几乎相似 但有些字段在每个应用程序中都会发生变化 我希望这些值可以在整个程序中使用 例如 每个公司的名称 应用程序的主颜色等 这些值在整个程序中将保持不变 我想按应用程序创建一个常量文件 这
  • org.springframework.web.servlet.PageNotFound noHandlerFound 未找到带有 URI 的 HTTP 请求的映射

    我知道这个问题被问了很多次 我已经尝试过 所有可能的解决方案仍然存在问题 实际上 同一个项目在直接从 netbeans 部署的 Tomcat 8 中运行时出现 0 错误 我在 eclipse 中创建了一个新项目并部署在 Websphere
  • Angular Material 2:修复多行错误消息

    我在我的角度应用程序中使用角度材料 2 当我的表单输入字段错误消息超过一行时 我遇到了问题 这是照片 这是代码
  • Xcode 7 或 8 与 Pod 相关的问题,根本无法运行

    尝试运行应用程序时 Xcode 中的 pod 出现此错误 error A cryptographic verification failure has occurred 尝试重新安装 Pods repo 重新安装 Xcode 也不能在模拟器
  • 如何从多模块 Maven 项目构建可执行 jar?

    我是 Maven 的初学者 很多东西都不明白 我可以构建简单的可执行 jar 但如何将多模块 Maven 项目构建为可执行 jar 对我来说很神奇 所以 我有三个项目 家长