我有一个JavaEE,maven应用程序。在这个应用程序中,我使用从XSD生成的类,以及通过Mapstruct生成的映射器。
在我的EJB模块中,maven构建应该执行以下操作:
不幸的是,它对我不起作用。这是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生成的类。
感谢您的帮助。
答案 0 :(得分:2)
根据我的理解,所需的流程如下:
因此,您可以尝试:
更好地阐明它:两个不同阶段的build-helper-maven-plugin的两次执行,用于两个不同的生成源。最好还为每个生成的源指向两个不同的文件夹(即generate-sources-xsd
和generated-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。