m2e:使用exec-maven-plugin生成代码

时间:2011-07-14 10:25:04

标签: java eclipse maven m2eclipse m2e

我已经使用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构建能像之前那样响应pom.xml更改,Project > Clean ...,SVN更新,Eclipse启动等。

我可以做些什么让m2e像m2eclipse一样工作?

2 个答案:

答案 0 :(得分: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运行其中一些并跳过其他人。您可能不得不将不需要的执行放在配置文件中。

答案 1 :(得分:1)

在Eclipse中,您可以定义在导入期间将运行哪个生命周期步骤,默认为空。您可以简单地将其更改为process-resources或简单地使用Maven - &gt;更新项目配置。在配置(Windows - &gt; Preferences - &gt; Maven)中,您可以更改默认行为以简化您的直播。

相关问题