【问题标题】:M2E and having maven generated source folders as eclipse source foldersM2E 并将 maven 生成的源文件夹作为 eclipse 源文件夹
【发布时间】:2011-11-01 20:47:18
【问题描述】:

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

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

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

项目配置不是最新的pom.xml。运行项目 配置更新

我想我理解为什么会这样,因为 Eclipse 的源文件夹集与 Maven 的集不同。但我需要这个不同的集合,因为我需要 Eclipse 才能看到 generated 源文件夹...

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

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

【问题讨论】:

    标签: eclipse maven m2eclipse


    【解决方案1】:

    您需要使用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>
    

    您还需要:

    【讨论】:

    • 在 Eclipse 中安装 m2e connector for build-helper-maven-plugin 时,此解决方案效果很好
    • 对我不起作用。你能详细说明一下 build-helper-plugin 的设置吗?
    • org.codehaus.mojobuild-helper-maven-pluginadd-source generate-sourcesadd-source${project.build.directory}/generated-来源/java/${project.build.directory}/jaxws/wsimport/java
    • 使用 Kepler,我还必须为构建助手 maven 插件安装 m2e 连接器。 (只需在 Eclipse 的 maven pom 编辑器中打开你的 pom,然后点击顶部的红色链接)。
    • 据我所知,从build-helper-maven-plugin 开始,&lt;version&gt; 指定为3.2.0(截至撰写时的最新版本),没有必要使用 Apt M2E 连接器。目前在 Eclipse IDE 2020-06 上工作,在 Eclipse 中使用嵌入式 Maven 3.6.3,在尝试添加源文件夹的目录路径后,我们的源文件夹具有排除项 (**) 的问题消失了。现在,所有包含项都有(**/*.java) 在其中。
    【解决方案2】:

    右键单击错误消息:

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

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

    【讨论】:

    • 这也适用于我。为什么这不是公认的答案?似乎接受的答案太多了。
    • @NielsBasjes 这不是公认的答案,因为它根本没有帮助。当您在 Eclipse 中向构建路径添加内容时,这意味着您不再与 POM 同步,因此会出现警告。更新项目配置只是删除了额外的构建路径条目,这是最初的问题。
    【解决方案3】:

    切换到新版本的 m2e/maven/apt 后,...由于 buildhelper 添加的构建路径导致文件重复,我遇到了构建错误,因此我需要从buildhelper。

    为了解决 Eclipse 中的问题,而不是通过在 M2E 中更新 Maven 配置添加“apt-generated”文件夹,我编写了一个 M2E 插件来解决这个问题。它将 maven-apt-plugin 中配置的 outputDirectories 添加到项目的构建路径中。

    https://apt-m2e.googlecode.com

    【讨论】:

    【解决方案4】:

    在 m2e 1.0 中,对 Maven 插件的处理发生了变化。您的代码生成插件可能缺少特定的 m2e 扩展。这是我设法找到的所有documentation

    这个bug report 也可能是相关的。

    【讨论】:

    • 我猜m2e是新的并且有很多出色的发展
    • 我认为问题在于,只是将 Maven 插件应用于 m2eclipse 项目,就像 m2eclipse 过去所做的那样,只是碰巧在大多数情况下都可以工作,但不能保证总是做正确的事情。新方法可能更可靠,但需要许多 Maven 插件都有对应的 m2e。
    【解决方案5】:

    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。

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

        面向 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。解决这个问题。

        【讨论】:

          【解决方案8】:

          这是我发现使用 spring 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 中,它会自动找到它

          希望这会有所帮助! ~野鸟

          【讨论】:

            【解决方案9】:

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

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

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

            【讨论】:

              【解决方案10】:

              对于这个问题的新访客 -

              build helper maven 插件和 m2e 连接器齐头并进。旧版本 (2.0 之前)的构建助手已移至 Eclipse 存档链接

              build helper versions archived

              从列表中选择正确的链接并将其添加为 Eclipse 更新站点。它应该要求你提供一堆(说真的……一大堆)日食更新。请接受,一切顺利。

              【讨论】:

                【解决方案11】:

                构建助手插件的配置确实对我们有用。

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

                例如,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>
                

                【讨论】:

                  【解决方案12】:

                  解决办法

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

                  【讨论】:

                    猜你喜欢
                    • 2012-07-09
                    • 2014-10-23
                    • 2013-10-19
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-01-15
                    相关资源
                    最近更新 更多