Unpack files from jar if jar is used, copy then extracted files to a directory

Xdg picture Xdg · Sep 13, 2013 · Viewed 18.7k times · Source

I have a large webapp which uses many Maven dependencies. They are included as JAR files, but I want to have a chance to use some of them as an opened project directly in Eclipse. Then dependent projects are linked with m2e.

From some of that JARs/projects, resources need to be extracted.

How can I do that with Maven-dependency-plugin? If artifact is included as JAR, unpack it, and then copy files to required directory. If artifact is included as project, it exists on a harddrive and files can be directly accessed and copied, without unpack.

Answer

Volker Seibt picture Volker Seibt · Sep 16, 2013

The m2e-plugin can neither execute the maven dependency plugin nor copy sources on its own.

You can use the maven dependency-plugin to extract files from jar:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>yourSourceGroup</groupId>
                                <artifactId>yourSourceArtifact</artifactId>
                                <version>1.0.0</version>
                                <type>jar</type>
                                <includes>path/to/Files.whatsoever</includes>
                                <outputDirectory>${project.build.directory}/your/target/folder</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

(You may have to change phase or goal for your needs.)

You can run "rightclick->Run as-> Maven install" on your project from inside eclipse to copy the files to the place you need them.

As an alternative for all this you can use a resource filter in your webapp to get resources directly from jars at runtime, e. g. from spring framework.