m2e:使用exec-maven-plugin生成代码 [英] m2e: Generated code with exec-maven-plugin

查看:116
本文介绍了m2e:使用exec-maven-plugin生成代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

在许多项目中,我已经生成了Java代码,通常通过库项目中的一个主类生成。这是一个典型的设置:

 < plugin> 
< groupId> org.codehaus.mojo< / groupId>
< artifactId> exec-maven-plugin< / artifactId>
<执行>
< 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>更多参数< / argument>
< / arguments>
< / configuration>
< / execution>
< / executions>
< / plugin>
< plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> build-helper-maven-plugin< / artifactId>
<执行>
< 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>

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



现在,m2e已经为buildhelper插件安装了一个连接器,所以源文件夹被创建,但是必须通过执行 Run As>手动触发代码生成。 Maven>产生来源。这真的很烦人,我想要maven构建来响应pom.xml的变化, Project>清洁... ,SVN更新,Eclipse启动等,如以前那样。



我可以做什么来使m2e工作像m2eclipse?

解决方案

您必须告诉M2E,可以在Eclipse版本中运行代码生成器:

 < project> 
< build>
[...]
< pluginManagement>
< plugins>
<! - 此插件的配置仅用于存储Eclipse m2e设置。它对Maven构建本身没有影响
。 - >
< plugin>
< groupId> org.eclipse.m2e< / groupId>
< artifactId>生命周期映射< / 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. 这将导致M2E执行您的POM中指定的所有全部执行 java执行,这一点非常重要。作为每个Eclipse构建的一部分。这意味着他们会经常运行,如果速度很慢,会是一个很大的麻烦。我不知道你会如何让M2E运行其中一些,并跳过其他的。您可能必须在个人资料中放置不需要的执行。


I have been using m2eclipse for 2 years or so and have now switched to 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:

<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.

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.

What can I do to make m2e work like m2eclipse?

解决方案

You have to tell M2E that it's okay to run your code generator as part of the Eclipse build:

<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.
  2. This will cause M2E to perform all java 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.

这篇关于m2e:使用exec-maven-plugin生成代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆