m2e connectors and lifecycle mapping

At the beginning of this appendix, we spoke about the Maven plugin connectors, which work in the background to guarantee the execution of the Maven plugins during the Eclipse build process. For example, if we declare the JAXB-2 Maven Plugin in our project as follows (as we did in the transportation-common-jar module of our sample project; see Chapter 2, Core Maven Concepts):

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>1.6</version>
  <executions>
    <execution>
      <id>myExecution</id>
      <goals>
        <goal>xjc</goal>
      </goals>
      <configuration>
        <schemaDirectory>src/main/resources/schema/</schemaDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

When we put an XSD schema in the specified schema directory, the Eclipse IDE will automatically invoke the xjc goal of jaxb2-maven-plugin, which is bound to the generate-sources phase; the plugin will create an additional source folder target/generated-sources/jaxb, which is the default output directory of the plugin. Even without invoking Maven explicitly, the project will appear as shown in the following screenshot:

m2e connectors and lifecycle mapping

Structure of a project with generated sources

Of course m2e cannot support every Maven plugin out of the box. It could happen that while declaring a certain plugin, for example, org.antlr:antlr3-maven-plugin, we get the error Plugin execution not covered by lifecycle configuration, as shown in the following screenshot. Notice that we have to hover with the mouse on the underlined <execution> element of the plugin configuration to display this pop-up message:

m2e connectors and lifecycle mapping

Plugin execution error for antl3-maven-plugin

Here we have three quick fixes available, and we will firstly try the third option Discover new m2e connectors. If we are lucky, we will find a suitable connector on the Eclipse Marketplace, as shown in the following screenshot, and we will proceed with installing it. Once the connector has been installed, our Eclipse IDE will manage the antlr3 Maven plugin correctly.

Tip

The antl3-maven-plugin is able to generate the Java sources corresponding to the given grammar files, as in the case of the previous JAXB-2 plugin example.

The Install m2e connectors window is shown in the following screenshot:

m2e connectors and lifecycle mapping

When we cannot find a suitable m2e connector, we have to choose between the first and the second quick fixes proposed by the pop-up error message. This is the case of jaxws-maven-plugin, for which, at the moment, a m2e connector is not available on the Eclipse Marketplace. Suppose that we have to generate the JAX-WS Java client for a web service with a given WSDL descriptor; we would have to declare the following Maven plugin in the <build><plugins> section of our POM:

<plugin>
  <groupId>org.jvnet.jax-ws-commons</groupId>
  <artifactId>jaxws-maven-plugin</artifactId>
  <version>2.3</version>
  <executions>
    <execution>
      <goals>
        <goal>wsimport</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <wsdlDirectory>src/main/resources/wsdl/</wsdlDirectory>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>com.sun.xml.ws</groupId>
      <artifactId>jaxws-tools</artifactId>
      <version>2.2.6</version>
    </dependency>
  </dependencies>
</plugin>

We have to put our WSDL file in the directory specified with the <wsdlDirectory> element. This plugin, as usual, is bound to the generate-sources phase. The error message that we receive is shown in the following screenshot, and the search for a suitable connector does not give any results:

m2e connectors and lifecycle mapping

Plugin execution error for jaxws-maven-plugin

The second quick fix, Mark goal <goal-name> as ignored in Eclipse build in Eclipse preferences, simply disables this error message acting directly on the Eclipse project configuration and leaves the plugin ignored by the build process. As the POM is not interested in this setting, all the developers working on this project will encounter this error.

Tip

This happens if the Eclipse configuration files and directories are not added to the source version control (and we recommended adding them to .svnignore / .cvsignore).

On the contrary, if we choose the first quick fix Permanently mark goal <goal-name> in pom.xml as ignored in Eclipse build, a dummy plugin configuration will be automatically inserted in the POM, in the <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.jvnet.jax-ws-commons
                </groupId>
                <artifactId>
                  jaxws-maven-plugin
                </artifactId>
                <versionRange>
                  [2.3,)
                </versionRange>
                <goals>
                  <goal>wsimport</goal>
                </goals>
              </pluginExecutionFilter>
              <action>
                <ignore></ignore>
              </action>
            </pluginExecution>
          </pluginExecutions>
        </lifecycleMappingMetadata>
      </configuration>
    </plugin>
  </plugins>
</pluginManagement>

Tip

The lifecycle-mapping plugin does not exist as a Maven plugin and it is not downloaded from any repositories. This is only a directive for the Eclipse IDE.

This way, jaxws-maven-plugin will be ignored permanently by all the developers who have to check out the project and open it in their Eclipse IDEs. None of them will encounter the previous error. Now that we have fixed this problem, we still have the original one: how can we execute the wsimport goal? In this case, we have to manually launch the Maven build from the Debug As… or Run As… menus. The Maven process will execute all the phases of the lifecycle with their bindings, as we ran it from the command line. We have to remember to launch the Maven execution every time we change the WSDL descriptor, in order to keep the Java sources up to date. Unfortunately, there is still another problem to be solved: the Eclipse IDE does not see the generated sources under target/generated-sources/wsimport and so we cannot use them in the project as we might get a lot of compilation errors in the IDE. Of course the Maven build process invoked manually will succeed, but we want to work on our project within Eclipse and we cannot accept a "broken" Java project. We can solve this issue using a workaround that consists of declaring buid-helper-maven-plugin in our POM as follows:

<build>
[…]
<plugins>
[…]
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.8</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>add-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>target/generated-sources/wsimport</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>

The Build Helper Maven plugin allows you to insert additional source directories in your projects. While we can define multiple directories for resources and web resources, the source directory is unique. There will not be a need for this plugin if we used Maven only from the command line, because the source directories added in the generate-sources phase are considered by the compiler plugin. As Eclipse is ignoring the jaxws-maven-plugin, it does not know anything about its output source directory, but it can consider the additional source directory defined through build-helper-maven-plugin. For its part, the Build Helper plugin needs a connector, which can be found on the Eclipse Marketplace. The final result is shown in the following screenshot:

m2e connectors and lifecycle mapping

An additional source directory is recognized, thanks to the Build Helper Maven Plugin.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset