繁体   English   中英

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

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

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

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

maven项目结构就像

parent  
|--lib
|--webapp

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

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

我发现正确的方法是使用maven-dependency-plugin。 由于“lib.jar”未在“webapp”模块的编译阶段使用,因此它只是一个包时间依赖项。 使用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>

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

那么为了便于阅读,我在评论中说的是答案的一部分:

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

注意:我还没有尝试过,所以你必须调整路径 - 尤其是输出路径,这样你的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>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM