m2e: Generated code with exec-maven-plugin

11,446

Solution 1

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.

Solution 2

In Eclipse you can define which life-cycle step will be run during an import which is by default empty. You can simply change this to process-resources or do a simply Maven -> Update Project Configuration. In the configuration (Windows -> Preferences -> Maven) you can change the default behaviour to simplify your live.

Share:
11,446
Sean Patrick Floyd
Author by

Sean Patrick Floyd

About me: Austrian / American Java developer / architect with 25+ years of experience, working in the Seattle Area for Domino Data Lab (views are my own). 50 y/o, married to a lovely German wife and father of three. Hold a 5th degree black belt in Taekwondo (Kukkiwon), love Monty Python and appreciate a good glass of Whisk(e)y. He / him. Java aficionado, dabbling in Scala / Kotlin / Groovy, off and on also JS, and Python or GoLang when I have to. Connect: Twitter, LinkedIn

Updated on June 03, 2022

Comments

  • Sean Patrick Floyd
    Sean Patrick Floyd almost 2 years

    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?

  • Sean Patrick Floyd
    Sean Patrick Floyd almost 13 years
    In m2eclipse, yes. In m2e, you can no longer specify what lifecycles are executed
  • Sean Patrick Floyd
    Sean Patrick Floyd over 12 years
    I'm accepting this because it's the correct answer. But it's still awful. Basically, because of m2e, I will have to write a custom plugin + custom connector for every code generation scenario unless I want my eclipse to be blocked by superfluous builds all the time.
  • Jonathan Fuerth
    Jonathan Fuerth over 12 years
    Agreed. I'm currently struggling through the multitude of problems thrown in my path by m2e, and I don't like it either. I'd rather be writing code than fighting the build.
  • Stian Soiland-Reyes
    Stian Soiland-Reyes about 12 years
    You can use <execute><runOnIncremental>false</runOnIncremental></execute‌​> to avoid running the plugin on every save.
  • Gobliins
    Gobliins over 8 years
    I dont understand this solution, I dont use pluginmanagement, so what do i have to enter in the run/debug configuration?
  • Gimby
    Gimby over 7 years
    I think that version range has a typo, it will produce an unparsable version range at least in modern Eclipses. [1,) works a lot better.
  • wutzebaer
    wutzebaer over 7 years
    Hm the Plugin execution not covered by lifecycle configuration has gone away with this, but my class is still not executed at all with eclipse