【问题标题】:m2e: Generated code with exec-maven-pluginm2e:使用 exec-maven-plugin 生成的代码
【发布时间】:2011-10-05 05:23:15
【问题描述】:

我已经使用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 &gt; Maven &gt; generate-sources 手动触发代码生成。这真的很烦人,我希望 maven 构建能够像以前一样响应 pom.xml 更改、Project &gt; Clean ...、SVN 更新、Eclipse 启动等。

如何让 m2e 像 m2eclipse 一样工作?

【问题讨论】:

  • 我怀疑您的问题与 M2E in the area of project lifecycle mapping 中所做的更改有关,尽管我不确定将插件目标指定为执行是否会对您的情况有所帮助。
  • @Vineet 谢谢,这个链接看起来是一个很好的起点。

标签: java eclipse maven m2eclipse m2e


【解决方案1】:

您必须告诉 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 运行其中一些并跳过其他。您可能必须将不需要的执行放在配置文件中。

【讨论】:

  • 我接受这个,因为它是正确的答案。但这仍然很糟糕。基本上,由于 m2e,我将不得不为每个代码生成场景编写一个自定义插件 + 自定义连接器,除非我希望我的 eclipse 一直被多余的构建阻塞。
  • 同意。我目前正在努力解决 m2e 在我的道路上抛出的众多问题,我也不喜欢它。我宁愿写代码也不愿与构建作斗争。
  • 您可以使用&lt;execute&gt;&lt;runOnIncremental&gt;false&lt;/runOnIncremental&gt;&lt;/execute&gt; 来避免每次保存时都运行插件。
  • 我不懂这个解决方案,我不使用插件管理,所以我必须在运行/调试配置中输入什么?
  • 我认为版本范围有错字,至少在现代 Eclipse 中会产生无法解析的版本范围。 [1,) 效果更好。
【解决方案2】:

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

【讨论】:

  • 在 m2eclipse 中,是的。在 m2e 中,您无法再指定执行哪些生命周期
猜你喜欢
  • 2022-10-20
  • 2017-04-15
  • 2023-03-04
  • 2012-04-13
  • 2020-04-07
  • 2020-08-15
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
相关资源
最近更新 更多