简体   繁体   English

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

[英]m2e: Generated code with exec-maven-plugin

I have been using m2eclipse for 2 years or so and have now switched to m2e . 我已经使用m2eclipse 2年左右,现在已经切换到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. 在许多项目中,我生成了Java代码,通常是通过库项目中的主类生成的。 Here's a typical setup: 这是一个典型的设置:

<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 . 现在,m2e已经为buildhelper插件安装了一个“连接器”,因此创建了源文件夹,但我必须通过执行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. 这真的很烦人,我希望maven构建能够响应pom.xml的更改, Project > Clean ... ,SVN Updates,Eclipse启动等,就像以前一样。

What can I do to make m2e work like m2eclipse? 我能做些什么让m2e像m2eclipse一样工作?

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: 笔记:

  1. It's important to put this configuration in the pluginManagement section, not directly under plugins. 将此配置放在pluginManagement部分中非常重要,而不是直接在插件下。
  2. This will cause M2E to perform all java executions specified in your POM as part of every Eclipse build. 这将导致M2E执行POM中指定的所有 Java执行,作为每个Eclipse构建的一部分。 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. 我不确定如何让M2E运行其中一些并跳过其他人。 You'd probably have to place the unwanted executions in profiles. 您可能不得不将不需要的执行放在配置文件中。

In Eclipse you can define which life-cycle step will be run during an import which is by default empty. 在Eclipse中,您可以定义在导入期间将运行哪个生命周期步骤,默认为空。 You can simply change this to process-resources or do a simply Maven -> Update Project Configuration. 您只需将其更改为process-resources或简单地进行Maven - > Update Project Configuration。 In the configuration (Windows -> Preferences -> Maven) you can change the default behaviour to simplify your live. 在配置(Windows - > Preferences - > Maven)中,您可以更改默认行为以简化实时操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM