Maven copies a specific jar of dependency to a specific directory in a .war file

I am trying to create 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 an application server. Full source code is available here

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

Maven project structure is like

parent  
|--lib
|--webapp

      

The Webapp module is a maven war module. Requires lib.jar package in webapp.war root. NOT under WEB-INF / lib.

How do I achieve this in maven?

+3


source to share


2 answers


I found that the correct way to do this is using the maven-dependency plugin. Since "lib.jar" is not used when compiling the "webapp" module, it only depends on the package time. Using the maven-dependency-plugin, I can copy the lib.jar to whatever directory I want during the package preparation step. Then the maven-war package included 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 is better suited for packaging javaws apps. See my example project
https://github.com/KiranMohan/dynamic-tree-javaws-sample-project
for details

+9


source


Ok, as I said in the comments for readability, this is part of the answer:

Since Maven will always store the web project dependencies in its WEB-INF/lib

default folder I (I'm not a Maven expert ...) would try to put mine lib.jar

in the /target

project folder before executing the phase package

.

NOTE. ... I haven't tested it, so you will need to adjust the paths - primarily the output path so that yours is lib.jar

correctly placed in the root war

(for example, if you open yours war

, there will be folders next to lib.jar

, for example 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 with JAR signing at all, but there is a plugin called maven-jarsigner-plugin

that I think will do the job (I would sign it and then move and then pack the war) using a guide - I recommend you tweak it according to my "sample config maven-resource-plugin

and post a new question directly containing your two plugin configurations. Don't forget to link to this question in this case and also leave this question open so someone with a better approach can fix my path) ...

+1


source







All Articles