java maven 将特定的依赖 jar 复制到 .war 文件中的特定目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27019460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:05:09  来源:igfitidea点击:

maven copy a particular dependency jar to a specific directory in the .war file

javamavenmaven-3

提问by Kiran Mohan

I am trying out a Simple Java Web Start project based on the Oracle Tutorial. I am using maven to package it as a webapp and deploy it to application server. The full source code is available here

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

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

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

The maven project structure is like

Maven项目结构就像

parent  
|--lib
|--webapp

The webapp module is a maven war module. It is required to package lib.jar at the root of webapp.war. NOT under WEB-INF/lib.

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

How to achieve this in maven?

如何在 Maven 中实现这一目标?

回答by Kiran Mohan

I found that the right way to do this is to use the maven-dependency-plugin. Since "lib.jar" is not used in the compile phase of "webapp" module, it is only a package time dependency. Using maven-dependency-plugin, I can copy lib.jar to any required directory during the prepare-package phase. The maven-war package would then include the lib.jar in the .war package.

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

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

Update:There is a webstart-maven-plugin that does a better job of packaging javaws applications. See my sample project
https://github.com/KiranMohan/dynamic-tree-javaws-sample-project
for details

更新:有一个 webstart-maven-plugin 可以更好地打包 javaws 应用程序。见我的样本项目
https://github.com/KiranMohan/dynamic-tree-javaws-sample-project
的细节

回答by JBA

Well how i said in the comments for sake of readability here is a part of the answer:

好吧,为了可读性,我在评论中说的是答案的一部分:

Since Maven will always store the dependencies of a web project under its WEB-INF/libfolder by default i (i am no Maven expert ...) would try to place my lib.jarinside the /targetfolder of the project before the phase packageis executed.

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

NOTE:I havent tried it out so you will have to adjust the paths - expecially the output path so your lib.jaris placed properly to be packed into the root of the war(e.g. if you open your warthere will be a lib.jarnext to folders such as WEB-INF).

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

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

As i said i have no experience in signing JARs at all but there is a plugin called maven-jarsigner-pluginwhich i guess will do the job (i would sign it, then move it, then package the war) with a manual - i recomend you try to configure it according to my "example configuration of the maven-resource-pluginand post a new question directly containing your two plugin configurations. Dont forget to link to this question in that case. And also leave this question open so someone with a better approach may correct my way).

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