Make Eclipse Respect Maven-Dependency-Plugin

453 views Asked by At

How can I have Eclipse recognise my use of the maven-dependency-plugin and execute it before deploying resources to Tomcat via WTP?

In a previous question I configured Maven to copy some artifacts into a war application, so I could serve them to web clients. The approach of copying them to target/${project.artifactId}-${project.version} works when packaging via the Maven command line. Sadly there's no such luck when using Eclipse's Tomcat integration.

Maven plugin configuration:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
    <execution>
        <id>copy</id>
        <phase>compile</phase>
        <goals>
            <goal>copy</goal>
        </goals>
        <configuration>
            <artifactItems>
                <artifactItem>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>3.8.1</version>
                    <type>jar</type>
                    <overWrite>false</overWrite>
                    <destFileName>optional-new-name.jar</destFileName>
                </artifactItem>
            </artifactItems>
            <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/</outputDirectory>
            <overWriteReleases>false</overWriteReleases>
            <overWriteSnapshots>true</overWriteSnapshots>
        </configuration>
    </execution>
</executions>
1

There are 1 answers

1
Fred Bricon On

This answer assumes you're using Eclipse Java EE Kepler SR1 (or more recent), which comes with the Maven integration plugins (m2e and m2e-wtp).

m2e-wtp, the Maven Integration for WTP plugin ignores maven CLI builds and configures WTP to publish resources from known locations.

So you need to do 2 things :

  • configure the dependency plugin to copy jars to a known m2e-wtp location during eclipse builds
  • tell m2e to actually run the dependency plugin during project configuration updates.

First, define a new maven property, in your properties section :

<jars.directory>${project.build.directory}/${project.artifactId}-${project.version}/</jars.directory>

In your maven-dependency-plugin configuration, use :

<outputDirectory>${jars.directory}</outputDirectory>

Now that should give you identical build results with Maven CLI. Then you need to use a specific m2e profile, that'll be automatically enabled when run in Eclipse and ignored in all other situations :

<profiles>
    <profile>
        <id>m2e</id>
        <!-- This profile is only activated when building in Eclipse with m2e -->
        <activation>
            <property>
                <name>m2e.version</name>
            </property>
        </activation>
        <properties>
            <jars.directory>${project.build.directory}/m2e-wtp/web-resources/</jars.directory>
        </properties>
    </profile>
</profiles>

Finally, we need to let m2e know it's ok to run maven-dependency-plugin:copy during project configuration. Add the following snippet to your pluginManagement section :

<pluginManagement>
    <plugins>
        <!--This plugin's configuration is used to store Eclipse m2e settings 
            only. It has no influence on the Maven build itself. -->
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-dependency-plugin</artifactId>
                                <versionRange>[2.8,)</versionRange>
                                <goals>
                                    <goal>copy</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute>
                                    <runOnConfiguration>true</runOnConfiguration>
                                    <runOnIncremental>false</runOnIncremental>
                                </execute>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

Now, make sure m2e knows about all these configuration changes : Hit Alt+F5 to bring up the Update Maven Configuration dialog, and click ok. Once the build completes, you should see your jar under Deployed Resources> web-resources, in the Project Explorer view.

From now on, deployments to Tomcat should contain optional-new-name.jar at the root of your webapp.