Maven lifecycle understanding

1.7k Views Asked by At

I have a JavaEE, maven application. In this application I use classes generated from XSD, and mappers via Mapstruct.

In my EJB module, the maven build should do the following:

  1. generate the java classes from XSD by jaxb2-maven-plugin
  2. add these generated classes to sources folder by build-helper-maven-plugin, because:
  3. maven-processor-plugin generates Mapstruct mapper implementions by generate-mapstruct-mappers, which use these XSD generated classes
  4. finally add these mapper implementations to the sources folder also

Unfortunately, it does not work for me. This is the plugins part of the ejb's pom.xml:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>${version.jaxb2-maven-plugin}</version>
    <executions>
        <execution>
            <id>xjc</id>
            <goals>
                <goal>xjc</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
        <packageName>com.my.project</packageName>
        <catalog>src/main/resources/xsd/CustomCatalog.xml</catalog>
        <xjbSources>
            <xjbSource>${project.basedir}/src/main/resources/jaxb2/</xjbSource>
        </xjbSources>
        <sources>
           <source>${project.basedir}/src/main/resources/xsd/applications/MyNewClass.xsd</source>
       </sources>
       <xsdPathWithinArtifact>my/source/xsds</xsdPathWithinArtifact>
    </configuration>
</plugin>
<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <configuration>
        <defaultOutputDirectory>
            ${project.build.directory}/generated-sources
        </defaultOutputDirectory>
    </configuration>
    <executions>
        <execution>
            <id>generate-mapstruct-mappers</id>
            <phase>compile</phase>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <processors>
                    <processor>org.mapstruct.ap.MappingProcessor</processor>
                </processors>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${version.mapstruct}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${version.mapstruct}</version>
        </dependency>
    </dependencies>
</plugin>
<!-- attach sources -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>compile</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

This generates the classes from JAXB, and put into the sources to the JAR file. But the generated Mapper implementations aren't in the sources.

In eclipse I can set the generated classes to be part of the build path, but the generated JAR doesn't contain the mapperimplementations.

If I change the phase of the plugins, the maven-processor-plugin will throw a cannot find simbol ERROR, and the symbol is the generated class from jaxb.

Thanks for helping me.

1

There are 1 best solutions below

0
On

According to my understanding, the desired flow would be as following:

  • Generate classes from XSD
  • Add them as sources (via Builder helper)
  • Generate mappers from MapStruct (which requires classes from XSD)
  • Add them as sources (via Builder helper)
  • compile all

Hence, you could try to:

  • set the execution of jaxb2-maven-plugin to phase generate-sources
  • set a first execution of build-helper-maven-plugin to phase generate-sources (which should add the classes from XSD as sources)
  • set the mappers generation execution to phase process-sources (which should find XSD classes as sources), but declare it after the build-helper-maven-plugin entry
  • set a second execution of build-helper-maven-plugin to phase process-sources, which should add the mappers as sources

To better clarify it: two executions of the build-helper-maven-plugin in two different phases for two different generated sources. Also better to point at two different folders for each generated sources (i.e. generate-sources-xsd and generated-sources-mappers). In your pom follow this declaration order: jaxb2 plugin, mappers plugin, build helper plugin.

Below a possible example:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.1</version>
    <executions>
        <execution>
            <id>xjc</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>xjc</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <outputDirectory>${project.build.directory}/generated-sources-xsd</outputDirectory>
        <packageName>com.my.project</packageName>
        <catalog>src/main/resources/xsd/CustomCatalog.xml</catalog>
        <xjbSources>
            <xjbSource>${project.basedir}/src/main/resources/jaxb2/</xjbSource>
        </xjbSources>
        <sources>
            <source>${project.basedir}/src/main/resources/xsd/applications/MyNewClass.xsd</source>
        </sources>
        <xsdPathWithinArtifact>my/source/xsds</xsdPathWithinArtifact>
    </configuration>
</plugin>
<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>2.2.4</version>
    <configuration>
        <defaultOutputDirectory>
            ${project.build.directory}/generated-sources-mappers
        </defaultOutputDirectory>
    </configuration>
    <executions>
        <execution>
            <id>generate-mapstruct-mappers</id>
            <phase>process-sources</phase>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <processors>
                    <processor>org.mapstruct.ap.MappingProcessor</processor>
                </processors>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
    </dependencies>
</plugin>
<!-- attach sources -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources-xsd</source>
                </sources>
            </configuration>
        </execution>
        <execution>
            <id>add-source2</id>
            <phase>process-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources-mappers</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

Note: in the snippet above I had to add some versions in order to make it work.

For a full list of Maven lifecycle phases, here.