如何使用 Maven 在子文件夹中构建包含所需库的 jar(如 Eclipse)

2024-04-05

在 Eclipse 中,您可以通过执行以下操作在相邻子文件夹中创建一个项目 jar 及其所需的依赖项:

  1. 导出->Java->可运行的 JAR 文件

  2. 选择库处理选项:将所需的库复制到生成的 JAR 旁边的子文件夹中

有没有办法使用 Maven 程序集插件来做到这一点?或者是否有另一个 Maven 插件更适合此任务?

Thanks!


是的,您可以使用程序集插件。 pom.xml:

<build>
    <!-- final name set the jar name, if left it 
    will give defualt "${artifactId}-${version}" -->
    <finalName>jar final name</finalName>
    <sourceDirectory>src</sourceDirectory>

        <!-- compiler plug in -->
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- assembly plugin -->
        <!-- the assembly plugin is used to define your 
        final deploy output (jar, zip, dir, war, etc..)-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>assembly:package</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <!-- The filename of the assembled distribution 
                        file defualt ${project.build.finalName}-->
                        <finalName>${project.build.finalName}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <!--    A list of descriptor files path to generate from-->
                        <descriptors>
                            <descriptor>assembly/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- jar plug in -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>fully.qualified.MainClass</mainClass>
                        <!-- to create a class path to your 
                        dependecies you have to fill true in this field-->
                        <addClasspath>true</addClasspath>
                        <!-- if you put your dependencySet/outputDirectory 
                        in the assembly file is in a specific folder (lib for example), 
                        you need to add classpathPrefix-->
                        <classpathPrefix>lib/</classpathPrefix>

                        <!-- if you defined your dependencySet/outputFileNameMapping 
                        in the assembly, instead of using the classpathPrefix, 
                        you should use customClasspathLayout, 
                        add the classpathPrefix at the begining of the 
                        customClasspathLayout, and then add the pattern of the outputFileNameMapping, 
                        NOTICE YOU NEED TO ADD '$' BEFOR OF EACH '$'.
                        supported only from version 2.3>-->
                        <!--<classpathLayoutType>custom</classpathLayoutType>
                        <customClasspathLayout>
                            lib/$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension}
                        </customClasspathLayout>-->

                    </manifest>

                    <manifestEntries>
                        <Class-Path>conf/</Class-Path>
                    </manifestEntries>
                </archive>

            </configuration>
        </plugin>

    </plugins>
</build>

程序集.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <!--the id will be add to the end of the distribution file -->
    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>


    <fileSets>
        <fileSet>
            <directory>target</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>                
            </includes>
        </fileSet>
        <fileSet>
            <directory>icons</directory>
            <outputDirectory>icons</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>conf</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
    </fileSets>

    <files>
        <!-- you need to create the bat file yourself -->
        <file>
            <source>batFileName.bat</source>
            <filtered>true</filtered>
        </file>
    </files>

        <dependencySets>
            <dependencySet>
                <!--define the outputDirectory of the dependencies, 
                    NOTICE: if it's diffrent from '/'  make sure to 
                    change the classPath configuration for 
                    the maven-jar-plugin in the pom-->
                <outputDirectory>lib</outputDirectory>
                <!-- maping the dependencies jar names.
                    NOTICE : if you used this definition, you need to use 
                    customClasspathLayout classPath configuration 
                    for the maven-jar-plugin in the pomg-->
                <outputFileNameMapping>
                    ${artifact.groupId}.${artifact.artifactId}.${artifact.extension}
                </outputFileNameMapping>
                <unpack>false</unpack>
            </dependencySet>
        </dependencySets>

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

如何使用 Maven 在子文件夹中构建包含所需库的 jar(如 Eclipse) 的相关文章

随机推荐

  • 使用递归将列表分解为子列表

    我正在尝试使用类型声明编写一个函数 Int Bool gt Int 我希望该功能仅添加Ints 到同一个嵌套子列表 如果布尔值是True 但是如果布尔值是False 我想要Int与下一个相关联True要添加到 a 的布尔值new子列表 例如
  • 内存不足,无法继续运行 Java 运行时环境

    我的系统有 4 GB RAM 运行 Windows 操作系统 我安装了 VMWare 来设置具有 2GB RAM 的 Ubuntu 虚拟机 在 Ubuntu 中 我打开 Eclipse 并开始编码 Eclipse 开始一次又一次关闭 我开始
  • 如何将 HostControl 实例传递给 TopShelf 中的自定义主机服务?

    This 问题已在其他地方提出 https stackoverflow com questions 28740658 how to detect if topshelf is running in console mode 但没有迹象表明我
  • 如何更新 SQL 中的选择性字段(保留部分字段不变)?

    我想用两个日期更新记录 如果没有要更新的新值 则保持现有数据不变 这是一个示例表记录 id last foo last bar 1 2010 05 30 2010 05 30 我正在使用的查询 UPDATE sampledates SET
  • Android 使用 P12 证书签署数据

    因此 我搜索了一段时间 但找不到真正需要的东西 因为出现的每个结果都与签署实际的包有关 所以基本上我所做的就是 Web 服务受到会话管理器的保护 为了获取会话号 需要将 GUID 发送到 Web 服务 在 iOS 方面 我们能够获得此设置
  • 通过 AWS API Gateway 集成模板使用 DynamoDB 的 lastEvaluatedKey 进行扫描

    我使用 AWS APIGateway 集成模板直接扫描 dynamoDB 而不是编写单独的 lambda 来处理请求 响应 但是 扫描有 1MB 的限制 这导致扫描仅限于部分数据 根据文档 我们应该重新扫描 直到找到匹配项或使用 lastE
  • 如何将 .csv 文件导入 R?

    我有这个 csv 文件 ID GRADES GPA Teacher State 3 C 2 Teacher3 MA 1 A 4 Teacher1 California 我想做的是使用 R 统计软件读取文件 并将标题读入某种列表或数组 我是
  • 有没有具有这些功能的轻量级 JDBC 包装器?

    命名参数 就像 Spring 中的 JdbcTemplate JDBC 连接设置的 XML 配置 用于查询的 XML 配置 类似休眠的东西
  • ParDo 中的侧面输出 | Apache Beam Python SDK

    由于该文档仅适用于 JAVA 我无法真正理解它的含义 它指出 虽然 ParDo 始终生成一个主输出 PCollection 作为 apply 的返回值 但您也可以让 ParDo 生成任意数量的附加输出 PCollection 如果您选择有多
  • 如何从 SQL Server 更改跟踪获取所有已更改表的列表

    如何获取在给定版本之后有任何跟踪更改的所有表 已启用更改跟踪 的列表 这将返回自上一个跟踪版本以来已更改的所有表的列表 set nocount on We want to check for changes since the previo
  • 计算段:距绝对地址的偏移量

    我可以计算一个地址Segment Offset as Segment 0x10 Offset 但如何计算相反的情况呢 例如 我怎样才能从0xF4170 to F400 0170和来自0xACF04 to ABCD 1234 您需要具有起始基
  • 为什么控件不想被删除?

    我试图删除 winform 面板中包含的所有控件 但它们只是不想被删除 其中一些被删除 有些则没有 我已经尝试了两种方法 但什么也没有 foreach foreach Control controlEntry in this content
  • int [] 和 int* 作为函数参数的区别

    这是一道面试题 有什么区别int and int 它们都是函数的输入参数 f int a int b 我的回答 For f 它们具有相同的功能 第一个是第一个元素的开始位置a 第二个指向一个int 但是 如何在不传递其他参数的情况下区分它们
  • Git 提交针对没有分支的标签

    如果我在没有创建分支的情况下查看源代码的标记版本 Git 会指示我根本不与任何分支关联 不过 很高兴让我进行更改并检查它们 这些变化去哪里了 如果我切换回 master 它们就会消失 被 master 中的内容覆盖 而且我似乎无法再次找到它
  • 三元组组合的递归 SPARQL 查询

    我使用onspy在Python中递归运行以下查询 SELECT c WHERE c rdfs subClassOf restriction restriction owl onProperty has part owl someValues
  • 使用自定义类型(Substrate FRAME 托盘 + Polkadot/Substrate 前端)

    设想 您正在开发一个框架托盘 https substrate dev docs en knowledgebase runtime frame为定制基质 https substrate dev 在存储或事件中使用自定义类型的节点 要与自定义
  • Qt:QOpenGLWidget 中的文本渲染

    我想在 QOpenGLWidget 上绘制文本标签 我尝试使用 QPainter 来完成此任务 但没有成功 文本看起来很难看并且没有抗锯齿 它在 Qt OpenGL 2dpainting 示例中看起来也很难看 然而 在也使用 OpenGL
  • 使用 firebase admin sdk 发送通知不起作用

    我正在尝试将通知从 Node js 服务器发送到 ios 应用程序 如果我从 Firebase 控制台发送通知 它似乎可以工作 但如果使用 firebase admin sdk 从我的 node js 服务器尝试 则不起作用 我按照教程ht
  • Unity 错误:给定的程序集名称或代码库无效

    我正在使用微软统一框架并收到此错误 4 15 2012 9 46 08 AM Inner Exception Type System IO FileLoadException Inner Exception The given assemb
  • 如何使用 Maven 在子文件夹中构建包含所需库的 jar(如 Eclipse)

    在 Eclipse 中 您可以通过执行以下操作在相邻子文件夹中创建一个项目 jar 及其所需的依赖项 导出 gt Java gt 可运行的 JAR 文件 选择库处理选项 将所需的库复制到生成的 JAR 旁边的子文件夹中 有没有办法使用 Ma