0

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>
Stefan
  • 10,010
  • 7
  • 61
  • 117

2 Answers2

1

The answer to your question is really dependent on what plugins you're trying to execute and at what point you need them executed.

From what I know, m2e adds its own builder to your eclipse project, and it can invoke plugins you have defined in your pom.xml

That being said, eclipse's build only brings you up to the point of running "maven compile". If you have plugins that need execution at the package phase, you will have to manually run a "maven package" configuration.

If you have plugins that execute at any phase up to "compile", they can also run in an eclipse build. However, you might need to use lifecycle-mappings to get them to actually run. You can either do that in Eclipse settings, but I prefer to map specific plugins to my needs in the pom itself, so everyone can use it.

A mapping example:

<build>
    <pluginManagement>
       <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>templating-maven-plugin</artifactId>
                                <versionRange>[1.0-alpha-3,)</versionRange>
                                <goals>
                                    <goal>filter-sources</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute>
                                    <runOnIncremental>true</runOnIncremental>
                                    <runOnConfiguration>true</runOnConfiguration>
                                </execute>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </pluginManagement>
</build>

this will cause the templating-maven-plugin run the filter-sources goal on every eclipse build. Note you can determine if you want the plugin executed on configuration (full) builds (after Project Clean or Maven Update), and/or on incremental builds (after editing some source code)

you can also set

<action>
    <ignore />
</action>

which will cause m2e to ignore the plugin during its builds (it will still run when launching a maven run configuration)

  • Some plugins don't always play nicely with eclipse, for example if you need to unpack/copy a dependency you have as a project in your workspace. There are workarounds though.

Let me know if you need any more help or if I wasn't clear anywhere

Dagan Sandler
  • 475
  • 1
  • 5
  • 16
  • Thank you for your answer. Yes, I would like to go beyond the compile phase (my example pom file includes an Install phase). I tried to include a *pluginManagement* region in my pom.xml file but I did not get it working. The lifecyleMapping entries are not shown when I inspect the Maven project properties. This might be due to the fact that I have a modular project and use *pom* packaging (https://issues.sonatype.org/browse/MNGECLIPSE-1695). – Stefan Jun 03 '15 at 11:37
  • Let's separate this into two parts: 1) Do you have plugins bound to pre-compile phases that don't execute although they are mapped in your pom file? 2) Eclipse build will never run package/install phase as part of its build lifecycle for the same reason it doesn't create a jar every time it compiles your classes. You have to launch a maven run configuration for that, which will invoke your plugins as well – Dagan Sandler Jun 03 '15 at 11:52
0

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 
Stefan
  • 10,010
  • 7
  • 61
  • 117