maven-processor-plugin忽略未定义的符号

时间:2014-03-25 12:02:10

标签: maven jpa-2.0 apt

我是JPA 2 maven项目,我想处理源以获得静态元模型。我做了什么,我JBoss' static meta model processor并将其设置为在generate-sources阶段运行。现在,显然我有一些引用元模型的类,编译本身也很好。但maven-processor-plugin本身会产生错误,抱怨它无法从元模型中找到符号,如下所示:

[INFO] --- maven-processor-plugin:2.2.4:process (process) @ ng-grid-java ---
[ERROR] diagnostic: c:\...\service\position\PositionSpecifications.java:13: cannot find symbol
symbol  : class Position_

这是合乎逻辑的,因为它实际上生成了这些类,但是不正确,因为它会给正确的项目带来错误。或者我可能错了?我错过了什么吗?

更新:我已经能够通过使用配置参数outputDiagnostics来禁止错误输出,但我不确定这是正确的方法。

2 个答案:

答案 0 :(得分:1)

解决方案可以使用build-helper-maven-plugin将生成的类添加到项目类路径中,如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>artifactId</artifactId>
        <groupId>groupId</groupId>
        <version>1.0.0-SNAPSHOT</version>       
    </parent>
    <artifactId>jpa-metamodel-generation</artifactId>

    <dependencies>

        <!-- Hibernate JPA metamodel generator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>1.2.0.Final</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate.javax.persistence</groupId>
                    <artifactId>hibernate-jpa-2.0-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>

            <!-- Plugin to generate JPA metamodel -->
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.0.5</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>process-sources</phase>
                        <configuration>
                            <outputDirectory>${project.build.directory}/metamodel</outputDirectory>
                            <processors>
                                <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                            </processors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Build helper plugin to add generated sources to classpath -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/metamodel</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

答案 1 :(得分:1)

我设置了插件的阶段 -s,如:

build-helper-maven-plugin --> <phase>process-sources</phase>

maven-processor-plugin --> <phase>compile</phase>