0

更新我的源代码后,我目前必须手动执行两个操作:

  • 使用 Alt+F5 更新我的 Maven 项目(这会使用 pom.xml 文件中的相应设置覆盖 Eclipse 项目设置,例如更新类路径文件)
  • 使用 maven 运行配置运行我的主 pom.xml 文件(这会执行 pom.xml 文件的所有插件)

有没有办法

  • 更新 m2e 项目后自动执行运行配置?或者
  • 在运行配置中包含 m2e 项目更新或
  • 编写一个 ant 文件来执行 m2e 项目更新和 maven 构建或
  • 调整 m2e 插件以不仅更新 Eclipse 设置,而且执行 pom.xml 文件的所有插件(我使用打包 pom,而不是 jar)?

如果我为 Maven 构建导出我的运行配置,它看起来像这样:

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

这是一个示例主 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 
                             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>
4

2 回答 2

1

您的问题的答案实际上取决于您尝试执行的插件以及您需要在什么时候执行它们。

据我所知,m2e 将它自己的构建器添加到您的 eclipse 项目中,它可以调用您在 pom.xml 中定义的插件

话虽这么说,eclipse 的构建只会让你达到运行“maven compile”的地步。如果您有需要在包阶段执行的插件,则必须手动运行“maven 包”配置。

如果您有在“编译”之前的任何阶段执行的插件,它们也可以在 Eclipse 构建中运行。但是,您可能需要使用生命周期映射来让它们实际运行。您可以在 Eclipse 设置中执行此操作,但我更喜欢在 pom 本身中将特定插件映射到我的需求,以便每个人都可以使用它。

映射示例:

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

这将导致在每个 Eclipse 版本上templating-maven-plugin运行目标。filter-sources请注意,您可以确定是否要在配置(完整)构建(在 Project Clean 或 Maven 更新之后)和/或增量构建(在编辑一些源代码之后)执行插件

你也可以设置

<action>
    <ignore />
</action>

这将导致 m2e 在其构建过程中忽略插件(在启动 maven 运行配置时它仍会运行)

  • 有些插件并不总是能很好地与 Eclipse 配合使用,例如,如果您需要解压缩/复制作为工作区中的项目的依赖项。虽然有解决方法。

让我知道您是否需要更多帮助,或者我是否不清楚

于 2015-06-03T10:29:57.980 回答
0

我找到了一个基于 EclipseScript 的解决方案:http: //eclipsescript.org 它需要一些微调,但原则上可以工作。安装 EclipseScript 插件后,我创建了一个包含以下代码的文件 updateMavenProject.eclipse.js。如果文件已打开,则可以使用 Alt+R 或 Ctrl+4(加选择)执行该文件。

//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 
于 2015-06-03T12:59:19.893 回答