简体   繁体   中英

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

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

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

The maven project structure is like

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.

How to achieve this in maven?

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.

<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

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/lib folder by default i (i am no Maven expert ...) would try to place my lib.jar inside the /target folder of the project before the phase package is executed.

NOTE: I havent tried it out so you will have to adjust the paths - expecially the output path so your lib.jar is placed properly to be packed into the root of the war (eg if you open your war there will be a lib.jar next to folders such as 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-plugin which 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-plugin and 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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