Maven生命周期的理解

时间:2015-12-04 15:02:46

标签: java maven jaxb maven-plugin maven-lifecycle

我有一个JavaEE,maven应用程序。在这个应用程序中,我使用从XSD生成的类,以及通过Mapstruct生成的映射器。

在我的EJB模块中,maven构建应该执行以下操作:

  1. 通过 jaxb2-maven-plugin
  2. 从XSD生成java类
  3. 将这些生成的类添加到sources文件夹中 build-helper-maven-plugin ,因为:
  4. maven-processor-plugin 通过 generate-mapstruct-mappers 生成 Mapstruct 映射器实现,这些实现使用这些XSD生成的类
  5. 最后将这些映射器实现添加到sources文件夹
  6. 不幸的是,它对我不起作用。这是ejb的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>
    

    这将从JAXB生成类,并将其放入JAR文件的源代码中。但生成的Mapper实现并不在源代码中。

    在eclipse中,我可以将生成的类设置为构建路径的一部分,但生成的JAR不包含mapperimplementations。

    如果我更改了插件的阶段,maven-processor-plugin将抛出找不到simbol 错误,该符号是jaxb生成的类。

    感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

根据我的理解,所需的流程如下:

  • 从XSD生成类
  • 将它们添加为源(通过Builder帮助程序)
  • 从MapStruct生成映射器(需要来自XSD的类)
  • 将它们添加为源(通过Builder帮助程序)
  • 编译所有

因此,您可以尝试:

  • 将jaxb2-maven-plugin的执行设置为phase generate-sources
  • 将build-helper-maven-plugin的第一次执行设置为phase generate-sources(应该将来自XSD的类作为源添加)
  • 将映射器生成执行设置为阶段进程源(应该将XSD类作为源),但是在build-helper-maven-plugin条目之后声明它
  • 将build-helper-maven-plugin的第二次执行设置为阶段进程源,这应该将映射器添加为源

更好地阐明它:两个不同阶段的build-helper-maven-plugin的两次执行,用于两个不同的生成源。最好还为每个生成的源指向两个不同的文件夹(即generate-sources-xsdgenerated-sources-mappers)。在你的pom中遵循以下声明顺序:jaxb2 plugin,mappers plugin,build helper plugin。

下面是一个可能的例子:

<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>

注意:在上面的代码段中,我必须添加一些版本才能使其正常工作。

有关Maven生命周期阶段的完整列表,here