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

时间:2014-11-19 14:33:56

标签: java maven maven-3

我正在尝试基于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中实现这一目标?

2 个答案:

答案 0 :(得分:9)

我发现正确的方法是使用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
详情

答案 1 :(得分:1)

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

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

注意:我还没有尝试过,所以你必须调整路径 - 特别是输出路径,这样你的package就可以正确地放入lib.jar的根目录中1}}(例如,如果您打开warwar等文件夹旁会有lib.jar

WEB-INF

正如我所说,我根本没有签署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> 的插件,我想这将完成工作(我会签名,然后移动它,然后打包战争)手册 - 我建议您尝试根据maven-jarsigner-plugin的示例配置对其进行配置,并发布直接包含两个插件配置的新问题。在这种情况下,别忘了链接到这个问题。并且还要打开这个问题,以便有更好的方法的人可以纠正我的方式)。