【问题标题】:maven copy a particular dependency jar to a specific directory in the .war filemaven 将特定的依赖 jar 复制到 .war 文件中的特定目录
【发布时间】:2014-11-19 14:33:55
【问题描述】:

我正在尝试基于 Oracle 教程的 Simple Java Web Start 项目。我正在使用 maven 将其打包为 webapp 并将其部署到应用程序服务器。完整的源代码可在此处获得

https://github.com/KiranMohan/dynamic-tree-javaws-sample-project

maven项目结构是这样的

parent  
|--lib
|--webapp

webapp 模块是一个 Maven 战争模块。需要在 webapp.war 的根目录下打包 lib.jar。不在 WEB-INF/lib 下。

如何在 maven 中实现这一点?

【问题讨论】:

  • 我无法在签名部分为您提供帮助,但由于您的第 1 点违反了关于包装类型 war 的文件结构的 maven 约定,除了将库移动到 webabbs 之外,我没有看到任何其他选择-target 目标 (/target) 直接使用例如maven-resources-plugin。您可能希望将目标copy-resources 应用于早于package 的阶段,以便之后在war 的根中说出jar
  • 还有一个 Jarsigner 插件,它也会在打包阶段之前启动。我将快速为您添加复制部分的示例配置 - 为了便于阅读,以答案的形式)
  • 我删除了关于 jarsigning 问题的第二部分。将为此添加一个新问题。

标签: java maven maven-3


【解决方案1】:

我发现正确的方法是使用 maven-dependency-plugin。 由于“webapp”模块的编译阶段没有使用“lib.jar”,它只是一个打包时间依赖。使用 maven-dependency-plugin,我可以在准备包阶段将 lib.jar 复制到任何需要的目录。然后,maven-war 包会将 lib.jar 包含在 .war 包中。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>[ group id ]</groupId>
                        <artifactId>[artifact id]</artifactId>
                        <version>[ version ]</version>
                        <outputDirectory>${project.build.directory}/${project.artifactId}</outputDirectory>                                 
                    </artifactItem>
                </artifactItems>
                <!-- other configurations here -->
            </configuration>
        </execution>
    </executions>
</plugin>

更新: 有一个 webstart-maven-plugin 可以更好地打包 javaws 应用程序。查看我的示例项目
https://github.com/KiranMohan/dynamic-tree-javaws-sample-project
了解详情

【讨论】:

  • 确实 - IF lib.jar 是一个 Maven 工件,这是一种更好的方法,因为它将从存储库中使用。 (但是复制机制是相同的)。请注意,我怀疑您是否真的需要这样做-我的猜测是您需要lib.jar(相应的maven artefact)作为scoperuntimeprovided的依赖项。我也假设您可以将lib.jar 存储在WEB-INF/lib 文件夹中,并且您只是在从该目录中加载内容时遇到了麻烦。请问lib.jar 的用途以及为什么它必须驻留在war 的根目录中?
  • 作为 javaws 应用程序,lib.jar 需要由客户端下载,并且应该可以通过网络访问,即 jar 需要由应用程序服务器公开可用。现在 Jboss 似乎隐藏了 WEB-INF 目录下的任何内容。所以我把 .war 文件的根目录。
  • 我在运行时收到以下错误:'artifactItems 未被识别为 maven 标签'
【解决方案2】:

为了便于阅读,我在 cmets 中所说的内容是答案的一部分:

由于默认情况下 Maven 将始终将 Web 项目的依赖项存储在其 WEB-INF/lib 文件夹下,我(我不是 Maven 专家...)会尝试将我的 lib.jar 放在项目的 /target 文件夹中在执行阶段package之前。

注意:我还没有尝试过,所以您必须调整路径 - 特别是输出路径,以便正确放置您的 lib.jar 以打包到 war 的根目录中(例如,如果您打开您的war,那么在WEB-INF 等文件夹旁边会有一个lib.jar

<!--
lets assume the root of my project would be under C:/devjba/projectX this equals the maven
variable ${project.basedir}.

from there the output-directory would be located under C:/devjba/projectX/target which equals the
maven variable ${project.build.directory}. This is the location a .war would be placed in after
the build

lets assume the required jar lib.jar is located under C:/devjba/projectX/misc which would equal to
the expression: ${project.basedir}/misc
-->

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>foo</id>
            <!-- may adjust to another phase before package but make sure your plugin is bound to a phase
            because otherwise it wont be invoked during build! Now its bound to the first phase of the
            default lifecycle for packaging type war -->
            <phase>process-resources</phase>
            <goals>
                <!-- use the copy-resources goal of this plugin - it will copy resources :) -->
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <!-- this points to /target of the current project, you may adjust it to wherever it must be placed to be packed into the root of the war (just try&error) -->
                <outputDirectory>${project.build.directory}</outputDirectory>
                <resources>
                    <resource>
                        <!-- this points to a folder /misc under the project root where we expect the lib.jar -->
                        <directory>${project.basedir}/misc</directory>
                        <!-- unless you specify what to include anything of the above directory will be included -->
                        <includes>
                            <include>lib.jar</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

正如我所说,我根本没有签署 JAR 的经验,但有一个名为 maven-jarsigner-plugin 的插件,我想它可以用手册完成这项工作(我会签名,然后移动它,然后打包战争) -我建议您尝试根据我的“maven-resource-plugin 的示例配置”来配置它,并直接发布一个包含您的两个插件配置的新问题。在这种情况下不要忘记链接到这个问题。并且还要让这个问题保持开放,以便有人更好的方法可能会纠正我的方式)。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-10-09
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 2016-10-21
  • 1970-01-01
  • 2013-11-17
相关资源
最近更新 更多