8

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Xdg
  • 1,735
  • 2
  • 27
  • 42
  • The question is a little vague: normaly you will have a Webapp-project with packaging "war" from the maven-perspective and as an "dynamic web project" in eclipse. Normaly this project wont contain jars but maven-dependencies. If you have such dependencies as open projects in eclipse, m2e will use these, otherwise the jars from your lokal repository. So I can't understand "Maven depdendencies included as JAR files" in your question. – Volker Seibt Sep 14 '13 at 11:23
  • All I need is to have some files (CSS, JS) in a specific directory. I need to extract-and-copy these files from JAR or in case of Eclipse-opened project, only copy them. I want m2e/maven to decide if extract-from-JAR is required (=if project is included as JAR or as an Eclipse project). – Xdg Sep 16 '13 at 06:43

1 Answers1

22

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.

Volker Seibt
  • 1,479
  • 16
  • 19