SJuan76
SJuan76

Reputation: 24885

Generated sources being compiled twice

Using Eclipse Luna with m2eclipse, I have a parent Maven project (facturas_root) and two Maven modules inheriting from it (sharepoint_ws and api_sharepoint).

sharepoint_ws was to be used only to generate JAXWS classes to connect to the Sharepoint WebServices, so I downloaded the related WSDL and included those as resources of the project. At generate-sources phase, it works correctly and generates the sources in target\generated-sources\ws-consume\mypackage\.

Now, the issue is that I made api_sharepoint import the sharepoint_ws dependency, but it does not detect any class. I assumed that it was because the generated classes were not at src/main/java, so I added a plugin to copy them there. Now, the problem is that at the compile phase of sharepoint_ws, it finds twice the source file of each class and throws an error.

My pom.xml -> build

<plugins>
  <!-- clean /src/main/java and /target/generated-sources -->
  <plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.6.1</version>
    <executions>
      <execution>
        <phase>generate-sources</phase>
        <goals>
          <goal>clean</goal>
        </goals>
        <configuration>
          <filesets>
            <fileset>
              <directory>${basedir}/src/main/java/es</directory>
            </fileset>
            <fileset>
              <directory>${basedir}/target/generated-sources</directory>
            </fileset>
          </configuration>
        </execution>
      </executions>
    </plugin>

    <!-- generate jaxws -->
    <plugin>
      <groupId>org.jboss.ws.plugins</groupId>
      <artifactId>maven-jaxws-tools-plugin</artifactId>
      <version>1.1.2.Final</version>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>wsconsume</goal>
          </goals>
          <configuration>
            <wsdls>
              <wsdl>${basedir}/resources/lists.wsdl</wsdl>
            </wsdls>
           <targetPackage>es.ssib.otic.facturas.sharepoint_ws</targetPackage>
            <outputDirectory>${basedir}/target/generated-sources</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>

    <!-- copy sources -->
    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <version>2.7</version>
      <dependencies>
        <dependency>
          <groupId>org.apache.maven.shared</groupId>
          <artifactId>maven-filtering</artifactId>
          <version>1.3</version>
        </dependency>
      </dependencies>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>copy-resources</goal>
          </goals>
          <configuration>
            <outputDirectory>${basedir}/src/main/java</outputDirectory>
            <resources>
              <resource>
                <directory>${basedir}/target/generated-sources/wsconsume</directory>
                <filtering>true</filtering>
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>

In order to try to exclude target/generated-sources I have addded this:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
      <execution>
        <id>default-compile</id>
        <phase>compile</phase>
        <configuration>
          <excludes>
            <exclude>**/target/generated-sources/*.java</exclude>
          </excludes>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

As stated above, I do comment the "copy" plugin, the module depending on sharepoint_ws does not have any ot its classes available; I do use it I get errors in the tune of

[ERROR] /C:/Users/s004256/workspace/facturas_root/sharepoint_ws/src/main/java/es/ssib/otic/facturas/sharepoint_ws/DeleteList.java:[34,8] duplicate class: es.ssib.otic.facturas.sharepoint_ws.DeleteList

for each generated list.

Upvotes: 2

Views: 2942

Answers (1)

Little Santi
Little Santi

Reputation: 8793

In the first place, I recommend you'd better declare target/generated-sources as a source folder, instead of copying files here and there:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.7</version>
        <executions>
            <execution>
                <id>add-source</id>
                <phase>initialize</phase>
                <goals>
                    <goal>add-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>${project.build.directory}/generated-sources</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>

This should be enough to make Maven compile the target/generated-sources/*.java and package them all in the library, and also for Eclipse to recognize target/generated-sources as a source directory (after you execute Maven/Update Project).

By the way: You should take care of binding the plugins to a phase in the correct order: If you bound all tasks to "generate-sources", you have no gurantee about in which order will they be executed. And the same goes for the "compile" phase: You have to set properly the source folders, with its inclusions and exclusions, before the compile phase.

Take a look a the Default Maven Lifecycle and try to chose different, sequential phases for your tasks.

Upvotes: 1

Related Questions