M2E 并让 maven 生成源文件夹作为 Eclipse 源文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7160006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:27:03  来源:igfitidea点击:

M2E and having maven generated source folders as eclipse source folders

eclipsemavenm2eclipsem2e

提问by Michael Wiles

I have a maven project in eclipse and have maven goals that run annotation processors to generate code. The output folder for this code is target/generated-sources/apt.

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

In order for eclipse to see this generated code I need to add target/generated-sources/apt as a source folder to the eclipse project.

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

However, this causes there to be an error of type "Maven Configuration Problem" saying

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

Project configuration is not up-to-date with pom.xml. Run project configuration update

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

I think I understand why this is the case as eclipse has a different set of source folders to maven's set. But I need this different set, as I need eclipse to be able to see the generated source folders...

我想我明白为什么会出现这种情况,因为 eclipse 的源文件夹集与 maven 的集不同。但是我需要这个不同的集合,因为我需要 eclipse 才能看到生成的源文件夹......

When doing a pure maven built, these source folders will be included in the build, by maven.

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

btw, I have upgraded to the official eclipse release of the maven eclipse plugin, m2e 1.0 - what used to be m2eclipse. I'd like to see if I can find a work around/solution to this with the m2e plugin before I have to go back to the old m2eclipse version.

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

回答by Michael-O

You need to attach the source directory with the build-helper-plugin.

您需要使用build-helper-plugin附加源目录。

Like so:

像这样:

 <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>

You will also need to:

您还需要:

回答by peter

Right-click the Error message:

右键单击错误消息:

Project configuration is not up-to-date with pom.xml Run project configuration update

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

in the Problems View and select Quick Fixand click Finishto select the default Update project configuration. This fixes it.

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

回答by Stefan Wo

After switching to new versions of m2e/maven/apt,... i had builderrors because of the duplicated files, caused by the added buildpath by the buildhelper, so i needed to remove the "apt-generated"-Folders from the buildhelper.

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

To fix the Problem in Eclipse, not adding the "apt-generated"-folder via Update Maven Configuration in M2E, i've written a M2E Plugin to fix this problem. It adds the outputDirectories configured in the maven-apt-plugin to the buildpath of the Project.

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

https://apt-m2e.googlecode.com

https://apt-m2e.googlecode.com

回答by Nicola Musatti

In m2e 1.0 the handling of Maven plugins has changed. You might be lacking a specific m2e extension for your code generating plugin. Here is all the documentationI managed to find.

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

This bug reportmay also be relevant.

错误报告也可能相关。

回答by Mike Grandmaison

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

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

request on CXF JIRA (see 1) to add lifecycle mappings in the cxf-codegen-plugin itself. This would require m2e 1.1 but I believe it is better approach than having connectors built outside of cxf project, assuming that lifecycle mapping API would change less frequently than cxf-codegen-plugin and cxf.

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

回答by burtsevyg

Eclipse Java EE IDE for Web Developers. Version: Juno Service Release 1

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

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

mvn clean install

work perfectly.

完美地工作。

But in eclipse I have the same error on Asinc class.

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

Just press F5 on project. Fix this problem.

只需在项目上按 F5。修复这个问题。

回答by WildBill

This was what I found that worked good using spring 3.1.1 which does have the 3.0.6 version as well in it. Once I got the plugins setup and put into the correct area of the pom and included the argline and endorseddirs to have the java sources put out into the target/generated-sources/cxf folder then maven generated the sources ok.

这是我发现在使用 spring 3.1.1 时效果很好的东西,它也包含 3.0.6 版本。一旦我设置了插件并放入了 pom 的正确区域并包含了 argline 和认可的目录,以便将 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>

If your wsdl folder is in ${basedir}/src/main/resources it'll find it automatically

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

Hope this helps! ~wildbill

希望这可以帮助!~野鸟

回答by Cédric Vidal

You can also use the buildhelper m2e connector available in the discovery catalog. I'm using Eclipse 3.7

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

回答by Adam Gent

In case for some reason you can't use the build helper plugin the easiest way (albeit not as convenient and somewhat tedious) I have found to deal with this is:

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

  1. Separate the generated source code into its own project or sub module.
  2. You will want to keep this project predominately closed or not imported into Eclipse when you are working on the parent project.
  3. In the parent project that needs the generated code make sure to now depend on the generated source code project via Maven pom dependency.
  4. When you need to update the generated code go to the generated code project and run mvn install. Now refresh the parent project by right clicking and selecting Maven->Update Project...
  1. 将生成的源代码分成自己的项目或子模块。
  2. 当您处理父项目时,您将希望保持该项目主要关闭或不导入到 Eclipse 中。
  3. 在需要生成代码的父项目中,确保现在通过 Maven pom 依赖依赖于生成的源代码项目。
  4. 当您需要更新生成的代码时,请转到生成的代码项目并运行mvn install. 现在通过右键单击并选择Maven->Update Project...刷新父项目

This generally works well for projects that use a semi static source for code generation such as SOAP WSDLs (Apache CXF) or code generated from a database (jOOQ). For APT and other AspectJ-like-code it doesn't work as well because you are editing the source frequently.

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

回答by Hannes Kogler

the configuration to the build helper plugin did work for us.

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

but be aware, that the destination folder always has to be equal to the configuration of the plugin u're using for the annotation processing itself.

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

for example the maven-processor-plugin uses the target folder ${project.build.directory}/generated-sources/apt as default. if you wish another destination for your generated source files you can set it by the tag as shown below.

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