20

我已经使用m2eclipse 2 年左右,现在已经切换到m2e

不幸的是,这破坏了我的一些功能。

在许多项目中,我生成了 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>

以前,我只需要将该项目与 eclipse 作为 maven 项目导入,代码将自动执行并将源文件夹添加到 eclipse 项目中。

现在,m2e 已经为 buildhelper 插件安装了一个“连接器”,因此创建了源文件夹,但我必须通过执行手动触发代码生成Run As > Maven > generate-sources。这真的很烦人,我希望 maven 构建能够像Project > Clean ...以前一样响应 pom.xml 更改、SVN 更新、Eclipse 启动等。

我该怎么做才能让 m2e 像 m2eclipse 一样工作?

4

2 回答 2

21

您必须告诉 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>

笔记:

  1. 将此配置放在 pluginManagement 部分中很重要,而不是直接放在插件下。
  2. 这将导致 M2E 执行POM 中指定的所有java 执行,作为每个 Eclipse 构建的一部分。这意味着它们会经常运行,如果它们很慢,就会很麻烦。我不确定您如何让 M2E 运行其中一些并跳过其他。您可能必须将不需要的执行放在配置文件中。
于 2011-10-19T19:11:17.010 回答
1

在 Eclipse 中,您可以定义在默认情况下为空的导入期间将运行哪个生命周期步骤。您可以简单地将其更改为流程资源或执行简单的 Maven -> 更新项目配置。在配置(Windows -> Preferences -> Maven)中,您可以更改默认行为以简化您的生活。

于 2011-07-14T10:50:16.640 回答