2

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

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

  1. 通过jaxb2-maven-plugin从 XSD 生成 java 类
  2. 通过build-helper-maven-plugin将这些生成的类添加到源文件夹 ,因为:
  3. maven-processor-plugin通过generate-mapstruct-mappers生成Mapstruct映射器实现,它使用这些 XSD 生成的类
  4. 最后将这些映射器实现也添加到源文件夹

不幸的是,它对我不起作用。这是 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 不包含映射器实现。

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

谢谢你帮助我。

4

1 回答 1

4

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.

于 2015-12-04T21:16:05.127 回答