eclipse m2e:使用 exec-maven-plugin 生成的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6691723/
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
m2e: Generated code with exec-maven-plugin
提问by Sean Patrick Floyd
I have been using m2eclipsefor 2 years or so and have now switched to m2e.
我已经使用m2eclipse2 年左右,现在已经切换到m2e。
Unfortunately, this has broken some functionality for me.
不幸的是,这对我来说破坏了一些功能。
In many projects, I have generated Java code, usually generated through a main class in a library project. Here's a typical setup:
在很多项目中,我都生成过Java代码,通常是通过库项目中的一个主类生成的。这是一个典型的设置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generateDTOs</id>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<classpathScope>test</classpathScope>
<mainClass>com.somecompany.SomeCodeGenerator</mainClass>
<arguments>
<argument>${project.build.directory}/generated-sources/foo</argument>
<argument>${project.basedir}/path/to/a/config/file</argument>
<argument>more arguments</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>addDtoSourceFolder</id>
<goals>
<goal>add-source</goal>
</goals>
<phase>process-sources</phase>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/foo</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Previously, I would just have to import that project with eclipse as a maven project, the code would automatically be executed and the source folder added to the eclipse project.
以前,我只需要使用 eclipse 作为 maven 项目导入该项目,代码将自动执行并将源文件夹添加到 eclipse 项目中。
Now, m2e has installed a "connector" for the buildhelper plugin, so the source folder is created, but I have to manually trigger code generation by executing Run As > Maven > generate-sources
. This is really annoying, I would like the maven build to respond to pom.xml changes, Project > Clean ...
, SVN Updates, Eclipse startup etc. as it previously did.
现在,m2e 已经为 buildhelper 插件安装了“连接器”,因此创建了源文件夹,但我必须通过执行Run As > Maven > generate-sources
. 这真的很烦人,我希望 maven 构建能够Project > Clean ...
像以前一样响应 pom.xml 更改、SVN 更新、Eclipse 启动等。
What can I do to make m2e work like m2eclipse?
我该怎么做才能使 m2e 像 m2eclipse 一样工作?
回答by Jonathan Fuerth
You have to tell M2E that it's okay to run your code generator as part of the Eclipse build:
您必须告诉 M2E 可以将代码生成器作为 Eclipse 构建的一部分运行:
<project>
<build>
[...]
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence
on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<versionRange>[,)</versionRange>
<goals>
<goal>java</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute/>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Notes:
笔记:
- It's important to put this configuration in the pluginManagement section, not directly under plugins.
- This will cause M2E to perform alljava executions specified in your POM as part of every Eclipse build. This means they will run often and be a big nuisance if they are slow. I'm not sure how you would get M2E to run some of them and skip others. You'd probably have to place the unwanted executions in profiles.
- 将此配置放在 pluginManagement 部分中很重要,而不是直接放在 plugins 下。
- 这将导致 M2E 执行POM 中指定的所有java 执行,作为每个 Eclipse 构建的一部分。这意味着它们会经常运行并且如果它们很慢就会造成很大的麻烦。我不确定您如何让 M2E 运行其中的一些并跳过其他的。您可能必须将不需要的执行放在配置文件中。
回答by khmarbaise
In Eclipse you can define which life-cycle step will be run during an import which is by default empty. You can simply change this to process-resources or do a simply Maven -> Update Project Configuration. In the configuration (Windows -> Preferences -> Maven) you can change the default behaviour to simplify your live.
在 Eclipse 中,您可以定义在导入期间将运行哪个生命周期步骤,默认情况下为空。您可以简单地将其更改为 process-resources 或执行简单的 Maven -> 更新项目配置。在配置(Windows -> Preferences -> Maven)中,您可以更改默认行为以简化您的生活。