Saturday, November 29, 2008

Packaging dynamic resources with Maven

Maven is an excellent project management tool which makes difficult things easy. With doing so many things it also bundles resource files if we tell it the path to resources as shown below-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    . . .
    <properties>
        <resource.dir>${code.base.dir}/${project.artifactId}/src/main/resources</resource.dir>
    </properties>
    . . .
    <build>
        . . .
    <resources>
            <resource>
                <directory>${resource.dir}</directory>
            </resource>
        </resources>
        . . .
    </build>
</project>
Above configuration works well for the resources, which are already present along with source code. There are cases when these resources are generated during build process like WSDL for a web service project. Though usually it is not required but in certain use cases it may be required to bundle such dynamically generated resources. As I mentioned in my earlier post about issues being faced in consuming web services in an array of applications based on SOA Architecture. In this particular scenario bundling WSDL will be really helpful, I will discuss more about it in coming posts.

To include generated WSDLs we can decalre their directory as resource directory and invoke the wsgen and wsimport tasks to generate the artificats-
    <properties>
        <wsdl.dir>${target.dir}/jaxws/wsgen/wsdl</wsdl.dir>
        <resource.dir>${wsdl.dir}/..</resource.dir>
        <keep.src>false</keep.src>
        <verbose>true</verbose>
    </properties>
    . . .
    <build>
        <plugins>
             <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <version>${jaxws-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>My Service</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>wsgen</goal>
                        </goals>
                        <configuration>
                            <sei>pkg.MyServiceImpl</sei>
                            <destDir></destDir>
                            <genWsdl>true</genWsdl>
                            <keep>${keep.src}</keep>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <version>${jaxws-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>My Service</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>wsimport</goal>
                        </goals>
                        <configuration>
                            <wsdlDirectory>${wsdl.dir}</wsdlDirectory>
                            <wsdlFiles>
                                <wsdlFile>MyService.wsdl</wsdlFile>
                            </wsdlFiles>
                            <packageName>pkg.wsclient</packageName>
                            <target>2.0</target>
                            <keep>${keep.src}</keep>
                            <verbose>${verbose}</verbose>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
In the above example <phase>generate-sources</phase> declaration is very important. If phase is defined as compile then WSDL files won't be included in the jar as by that time Maven has already decided what to be included and from where. Similar tricks can be used to include other kind of dynamically generated content as well.

0 Comments: