Search code examples
eclipsemavenm2erun-configuration

How to combine the update of a m2e maven project in Eclipse with a "maven build" run configuration to execute pom.xml


After updating my source code I currently have to manually execute two actions:

  • Update my maven projects with Alt+F5 (this overrides the Eclipse project settings with corresponding settings from the pom.xml files, e.g. udpates the classpath files)
  • Run my main pom.xml file with a maven run configuration (this executes all plugins of the pom.xml file)

Is there a way to

  • automatically execute a run configuration after updating m2e projects? or
  • include an m2e project update in a run configuration or
  • write an ant file to execute both, the m2e project update and the maven build or
  • adapt the m2e plugin to not just update the Eclipse settings but execute all plugins of the pom.xml file (I use packaging pom, not jar) ?

If I export my run configuration for the maven build it looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType">
<booleanAttribute key="M2_DEBUG_OUTPUT" value="false"/>
<stringAttribute key="M2_GOALS" value="clean install "/>
<booleanAttribute key="M2_NON_RECURSIVE" value="false"/>
<booleanAttribute key="M2_OFFLINE" value="false"/>
<stringAttribute key="M2_PROFILES" value=""/>
<listAttribute key="M2_PROPERTIES"/>
<stringAttribute key="M2_RUNTIME" value="EMBEDDED"/>
<booleanAttribute key="M2_SKIP_TESTS" value="true"/>
<intAttribute key="M2_THREADS" value="4"/>
<booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false"/>
<stringAttribute key="M2_USER_SETTINGS" value="../PowerShare/maven_settings.xml"/>
<booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dmaven.multiModuleProjectDirectory="/>
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${workspace_loc:PowerTools}"/>
</launchConfiguration>

Here is an example main pom.xml file:

<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 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <!-- HEADER **************************************************************************************************************** -->

  <modelVersion>4.0.0</modelVersion>
  <groupId>isi.power.tools</groupId>
  <artifactId>PowerTools</artifactId>
  <version>0.0.1-SNAPSHOT</version> <!--  is available as variable ${project.version} -->
  <packaging>pom</packaging>

  <!-- CUSTOM PROPERTIES ***************************************************************************************************** -->

  <properties>

        <!--  set encoding -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>      
  </properties>


  <build> 

    <!-- RESOURCES *********************************************************************************************************** -->   

     <resources>      
           <resource>
                <!--  add java source folder as resource to copy fxml files  -->
                <directory>src/main/java</directory>                
          </resource>
          <resource>
                <directory>src/main/resources</directory>
                <!-- enable replacement of variable place holders with values, e.g. to include version information -->         
                <filtering>true</filtering>  
           </resource>
    </resources>

    <!-- PLUGINS ************************************************************************************************************** -->  

    <plugins>   


            <!-- ### RESOURCES ### phase -->            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>resource-execution</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>resources</goal>                         
                        </goals>
                    </execution>                    
                </executions>   
            </plugin>   

            <!-- ### COMPILE ### phase -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <!-- specify current java version here: -->
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <id>compile-execution</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>                           
                        </goals>
                    </execution>
                    <execution>
                        <id>isi.power.ace.test-compile-execution</id>
                        <phase>isi.power.ace.test-compile</phase>
                        <goals>                         
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>       
            </plugin>

            <!-- ### PACKAGE ### phase -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>package-execution</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>                           
                        </goals>
                    </execution>                    
                </executions>               
            </plugin>

            <!-- ### INSTALL ### phase -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
                <executions>
                    <execution>
                        <id>install-execution</id>
                        <phase>install</phase>
                        <goals>
                            <goal>install</goal>                           
                        </goals>
                    </execution>    
                    <execution>
                        <id>install-file-execution</id>
                        <phase>install</phase>
                        <goals>
                            <goal>install-file</goal>                          
                        </goals>
                        <configuration>
                            <groupId>isi.power.tools</groupId>
                            <artifactId>PowerTools</artifactId> 
                            <version>${project.version}</version>
                            <packaging>jar</packaging>
                            <file>${project.basedir}/target/PowerTools-${project.version}.jar</file>
                        </configuration>
                    </execution>                
                </executions>   
            </plugin>           


    </plugins>

  </build>

  <!-- MODULES ************************************************************************************************************** -->  

  <modules>     
        <module>../PowerCluster</module>        
  </modules>

  <!-- DEPENDENCIES ********************************************************************************************************* -->  

  <dependencies>
      <!-- Dependencies on other workspace projects -->
      <dependency>
          <groupId>isi.power.share</groupId>
          <artifactId>PowerShare</artifactId>
          <version>${project.version}</version>
      </dependency>
      <dependency>
          <groupId>PowerACEISI_trunk</groupId>
          <artifactId>PowerACEISI_trunk</artifactId>
          <version>${project.version}</version>
      </dependency>
      <dependency>
          <groupId>isi.power.cluster</groupId>
          <artifactId>PowerCluster</artifactId>
          <version>${project.version}</version>
      </dependency>

  </dependencies>

</project>

Solution

  • I found a solution that is based on EclipseScript: http://eclipsescript.org It requires some fine tuning but works in principle. After installing the EclipseScript plugin I created a file updateMavenProject.eclipse.js including the code below. The file can be executed with Alt+R if it is open or with Ctrl+4 (plus selection).

    //This script is based on EclipseScript, see following page for more information: http://eclipsescript.org/
    //Execute this script by pressing Alt+R
    //This script:
    // * updates the maven project (like Alt+F5 ...: apply information from pom.xml file to eclipse project settings, e.g. udpate classpath file) and
    // * runs the pom.xml file as maven build (like "Run as maven build": executes all maven plugins of the pom.xml file)
    //  (the run configuration "updateMavenProject.eclipse.js" has to exist)
    
    
    //#region SCRIPT COMMANDS
    
    //update maven project ************
    
    //get workbench
    //var workbench = Packages.org.eclipse.ui.PlatformUI.getWorkbench();
    
    //create maven update job
    var currentProject = eclipse.resources.currentProject
    var projects = [ currentProject ];
    var updateMavenJob = Packages.org.eclipse.m2e.core.ui.internal.UpdateMavenProjectJob(projects);
    
    //execute maven update job
    var progressMonitor = Packages.org.eclipse.core.runtime.NullProgressMonitor();
    eclipse.console.println("Updating maven project...")
    updateMavenJob.runInWorkspace(progressMonitor);
    eclipse.console.println("Updating maven project finished.")
    
    //execute maven build **************
    var launchConfiguration = getLaunchConfiguration("My_Maven_Run_Configuration");
    var debugTools  = Packages.org.eclipse.debug.ui.DebugUITools();
    
    eclipse.console.println("Launching Maven run configuration asynchonously.")
    debugTools.launch(launchConfiguration, Packages.org.eclipse.debug.core.ILaunchManager.RUN_MODE);
    
    
    //show end message *****************
    eclipse.window.alert("finished script. please wait until console is finished, too.");
    
    //#end region
    
    
    
    //#region METHODS 
    
    //
    // Returns the launch configuration with the given name or null if it does not exist
    //
    function getLaunchConfiguration(nameOfWantedLaunchConfiguration){
        var launchManager = Packages.org.eclipse.debug.core.DebugPlugin.getDefault().getLaunchManager();
        var launchConfiguration = null; 
        var launchConfigurations = launchManager.getLaunchConfigurations(); 
        launchConfigurations.forEach(
            function(currentLaunchConfiguration){   
                  var name = currentLaunchConfiguration.getName();
                  //eclipse.console.println(name);
                  if (name.equals(nameOfWantedLaunchConfiguration)){
                      launchConfiguration = currentLaunchConfiguration;
                  }
    
            }
        );
        return launchConfiguration;
    }
    
    //#end region