jueves, 8 de enero de 2015

One pom.xml, multiple jar, different resources per jar

Today I had one of those problems born mainly from stubordness: how to make two jars with different resources with only one maven run?

I wanted to take advantage of Servlet 3.0 and serve static resources - namely, ExtJS - from a jar, thus avoiding the need to copy the distribution of Ext JS on every application I wanted to use it. That was easy enough, but today I had one of those strange desires: I wanted the whole distribution - sources, resources, html documentation... everything - to be in another jar, just in case I wanted to include it in another application.

At first I thought it was going to be simple enough, after all, I had done similar things to web applications just with maven-war-plugin, so first I tried maven-jar-plugin, since I wanted a second jar.
Add another execution, with its own configuration - in particular, the resources/resource section - and that should do, right?
Well, wrong.

Then I tried the assembly plugin but didn't like the whole "build your own descriptor" thing.
Then I tried the resources plugin, but it still created one jar (with everything I added in the resources additional execution)

Finally, pulling the thread, I understood that the copy-resources I was doing was putting the documentation files in the classes directory that would get into the jar, hence the jar containing everything.
Then I looked the jar plugin and found that you can select what folder he has to use as source for the jar's contents, and that was it.

The end is:
1. Configure another execution of the resources plugin to copy all the content into a different folder.
2. Configure another execution of the jar plugin to use that folder.

Here's the particular fragment of the pom.xml, just in case

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>documentation</id>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <classifier>docs</classifier>
                        <outputDirectory>${project.build.outputDirectory}/../docs</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                            </resource>
                            <resource>
                                <directory>src/docs/resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <classifier>docs</classifier>
                        <classesDirectory>${project.build.outputDirectory}/../docs</classesDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

No hay comentarios: