M2E并将maven生成的源文件夹作为eclipse源文件夹

时间:2011-08-23 11:01:56

标签: eclipse maven m2eclipse m2e

我在eclipse中有一个maven项目,并且有maven目标运行注释处理器来生成代码。此代码的输出文件夹是target / generated-sources / apt。

为了让eclipse看到这个生成的代码,我需要将target / generated-sources / apt作为源文件夹添加到eclipse项目中。

但是,这会导致出现“Maven配置问题”类型错误

  

项目配置与pom.xml不同步。运行项目   配置更新

我想我明白为什么会这样,因为eclipse有一组不同的源文件夹到maven的集合。但我需要这个不同的设置,因为我需要eclipse才能看到生成的源文件夹......

在构建纯maven时,这些源文件夹将由maven包含在构建中。

顺便说一句,我已经升级到maven eclipse插件的官方eclipse版本,m2e 1.0 - 曾经是m2eclipse。在我不得不回到旧的m2eclipse版本之前,我想看看我是否可以使用m2e插件找到解决方案/解决方案。

11 个答案:

答案 0 :(得分:103)

您需要将源目录附加到build-helper-plugin

像这样:

 <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/java/</source>
                </sources>
            </configuration>
        </execution>
    </executions>
 </plugin>

您还需要:

答案 1 :(得分:80)

右键单击错误消息:

  

pom.xml Run项目的项目配置不是最新的   配置更新

在问题视图中单击并选择快速修复,然后单击完成以选择默认的更新项目配置。这解决了它。

答案 2 :(得分:4)

切换到m2e / maven / apt的新版本后,...由于buildhelper添加的构建路径导致重复文件,我有构建器错误,因此我需要删除&#34; apt-generated&# 34; - 来自buildhelper的文件。

要修复Eclipse中的问题,而不是通过M2E中的Update Maven配置添加&#34; apt-generated&#34; -folder,我已经编写了一个M2E插件来解决这个问题。它将maven-apt-plugin中配置的outputDirectors添加到Project的构建路径中。

https://apt-m2e.googlecode.com

答案 3 :(得分:3)

在m2e 1.0中,Maven插件的处理已经改变。您的代码生成插件可能缺少特定的m2e扩展。以下是我设法找到的所有documentation

bug report也可能相关。

答案 4 :(得分:1)

https://bugs.eclipse.org/bugs/show_bug.cgi?id=350081

请求CXF JIRA(请参阅1)在cxf-codegen-plugin本身中添加生命周期映射。这将需要m2e 1.1,但我认为这比在cxf项目之外构建连接器更好,假设生命周期映射API的变化频率低于cxf-codegen-plugin和cxf。

答案 5 :(得分:0)

您还可以使用发现目录中提供的buildhelper m2e连接器。我正在使用Eclipse 3.7

答案 6 :(得分:0)

面向Web开发人员的Eclipse Java EE IDE。 版本:Juno Service Release 1

mvn archetype:generate \
   -DarchetypeGroupId=org.codehaus.mojo \
   -DarchetypeArtifactId=gwt-maven-plugin \
   -DarchetypeVersion=2.5.0

mvn clean install

完美地工作。

但是在eclipse中我在Asinc类上有同样的错误。

只需在项目上按F5即可。解决了这个问题。

答案 7 :(得分:0)

这是我发现使用弹簧3.1.1工作得很好,其中也有3.0.6版本。一旦我获得了插件设置并放入pom的正确区域并包含argline和endorseddirs以将java源放入target / generated-sources / cxf文件夹,然后maven生成源可以。

     ....

 <properties>...

   <dependencyManagement>
      <dependencies>.....
   </dependencyManagement>

<dependencies>
   <dependency>....

</dependencies>



<!-- *************************** Build process ************************************* -->
<build>
    <finalName>eSurety</finalName>
    <plugins>
        <!-- Force Java 6 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- Deployent on AS from console
        <plugin>
            <groupId>org.jboss.as.plugins</groupId>
            <artifactId>jboss-as-maven-plugin</artifactId>
            <version>${version.jboss.as.maven.plugin}</version>
        </plugin>
        -->

        <!-- wildbill added tomcat plugin -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.0</version>              
        </plugin>

        <!-- Surefire plugin before 2.9 version is buggy. No need to declare here,
              it's being referenced below w/ the version
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
        </plugin>
        -->

        <!-- developer added these -->   
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <compilerArguments>
                    <endorseddirs>target/generated-sources/cxf</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
            <configuration>
                <forkMode>once</forkMode>
                <argLine>-Djava.endorsed.dirs=target/generated-sources/cxf</argLine>
            </configuration>
        </plugin>           
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <compilerArguments>
                    <endorseddirs>target/generated-sources/cxf</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <forkMode>once</forkMode>
                <argLine>-Djava.endorsed.dirs=target/generated-sources/cxf</argLine>
            </configuration>
        </plugin>                       
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>                       
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>javax.xml.bind</groupId>
                        <artifactId>jaxb-api</artifactId>
                        <version>2.2</version>
                    </artifactItem>
                    <artifactItem>
                        <groupId>javax.xml.ws</groupId>
                        <artifactId>jaxws-api</artifactId>
                        <version>2.2</version>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>target/generated-sources/cxf</outputDirectory>
            </configuration>                      
        </plugin>                                                 
    </plugins>
</build>



<!-- *********************** Profiles ************************************ -->
<profiles>
    <profile>
        <!-- When built in OpenShift the 'openshift' profile will be 
            used when invoking mvn. -->
        <!-- Use this profile for any OpenShift specific customization 
            your app will need. -->
        <!-- By default that is to put the resulting archive into the 
            'deployments' folder. -->
        <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
        <id>projName</id>
        <build>
            <plugins>                                                   
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-codegen-plugin</artifactId>
                    <version>2.5.2</version>                        
                    <executions>
                        <execution>
                            <id>process-sources</id>
                            <phase>generate-sources</phase>                                                                                               
                            <configuration>
                                <fork>once</fork>
                                <additionalJvmArgs>-Djava.endorsed.dirs=target/generated-sources/cxf</additionalJvmArgs>                                          
                            </configuration>
                            <goals>                             
                                <goal>wsdl2java</goal>
                            </goals>
                        </execution>
                    </executions>                       
                    <dependencies>
                        <dependency>
                           <groupId>com.sun.xml.bind</groupId>
                           <artifactId>jaxb-impl</artifactId>
                           <version>2.2</version>
                        </dependency>
                        <dependency>
                           <groupId>com.sun.xml.bind</groupId>
                           <artifactId>jaxb-xjc</artifactId>
                           <version>2.2</version>
                        </dependency>
                     </dependencies>
                </plugin>

                <!-- Actual war created in default target dir -->
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.2</version>                                               
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

如果你的wsdl文件夹在$ {basedir} / src / main / resources中,它会自动找到它

希望这有帮助! 〜wildbill

答案 8 :(得分:0)

如果由于某种原因你不能使用构建助手插件最简单的方法(虽然不方便,有点乏味)我发现处理这个是:

  1. 将生成的源代码分隔到自己的项目或子模块中。
  2. 当您处理父项目时,您将希望保持此项目主要关闭或不导入Eclipse。
  3. 在需要生成代码的父项目中,请确保现在依赖于通过Maven pom依赖项生成的源代码项目。
  4. 当您需要更新生成的代码时,请转到生成的代码项目并运行mvn install。现在通过右键单击并选择 Maven-&gt;更新项目...
  5. 来刷新父项目

    这通常适用于使用半静态源代码生成的项目,如SOAP WSDL(Apache CXF)或从数据库生成的代码(jOOQ)。对于APT和其他类似AspectJ的代码,它不能正常工作,因为您经常编辑源代码。

答案 9 :(得分:-1)

构建帮助程序插件的配置确实适用于我们。

但请注意,目标文件夹始终必须等于您用于注释处理本身的插件的配置。

例如,maven-processor-plugin使用目标文件夹$ {project.build.directory} / generated-sources / apt作为默认值。如果您希望生成源文件的其他目的地,可以通过标签进行设置,如下所示。

<plugin>
<groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.1.1</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>process-sources</phase>
                        <configuration>
                            <defaultOutputDirectory>apt_generated</defaultOutputDirectory>
                            <processors>
                                <processor>com.any.processor.invoker</processor>
                            </processors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

答案 10 :(得分:-3)

这是解决方案

  1. 打开标记视图(窗口&gt;显示视图
  2. 右键单击错误消息
  3. 选择快速修复
  4. 点击完成