javaeclipsemavenlifecycleexec-maven-plugin

How to trigger maven exec plugin automatically in Eclipse when file is changing?


I am using Eclipse 2023 to build a Typescript module with node/npm. I like there to execute each time the a "*.ts" file changes the maven exec plugin to do the typescript compilation.

I have added the exec-maven-plugin and as well the lifecycle-mapping plugin. But when I change the "*.ts" file or calling "build" on my maven project, then the plugin get not automatically executed. I need each time to run the maven build manually by using a "Run Configuration".

The project in Eclipse is a maven project (has the "M" icon) and as well under Builders the "Maven Project Builder set.

Any hint what I am doing wrong or what I missed?

My Eclipse Version: 2023-03 (4.27.0)

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example.application</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>webcomponent</artifactId>
    <packaging>pom</packaging>

    <build>
           <pluginManagement>
            <plugins>
                <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>
                             <goals>
                               <goal>exec</goal>
                             </goals>
                          </pluginExecutionFilter>
                           <action>
                             <execute/>
                        </action>
                         </pluginExecution>
                       </pluginExecutions>
                     </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                  <execution>
                    <goals>
                      <goal>exec</goal>
                    </goals>
                    <phase>compile</phase>
                  </execution>
                </executions>
                <configuration>
                  <executable>npm</executable>
                  <arguments>
                    <argument>run</argument>
                    <argument>tsc</argument>
                  </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

enter image description here


Solution

  • Eclipse usually uses his embedded copy of Maven. The m2.conf is generated dynamically by eclipse on demand (in example: /home/yourname/workspace/.metadata/.plugins/org.eclipse.m2e.launching/launches/m2conf17481979518454286727.tmp).

    You need to install maven separatly in order to do a automatic build using a builder in the project.

    Installed maven manually?

    Ok, first you need to activate automatic builds: enter image description here

    Then open the project-properties and add a builder

    enter image description here

    Choose the mvn-executable and adjust the properties and goals according to your project.