如何将 Eclipse 中 m2e maven 项目的更新与 "maven build" 运行 配置结合起来执行 pom.xml
How to combine the update of a m2e maven project in Eclipse with a "maven build" run configuration to execute pom.xml
更新我的源代码后,我目前必须手动执行两个操作:
- 使用 Alt+F5 更新我的 Maven 项目(这会使用 pom.xml 文件中的相应设置覆盖 Eclipse 项目设置,例如更新类路径文件)
- 运行 我的主要 pom.xml 文件带有 Maven 运行 配置(这会执行 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>
您问题的答案实际上取决于您尝试执行的插件以及您需要在什么时候执行它们。
据我所知,m2e 将自己的构建器添加到您的 eclipse 项目中,并且它可以调用您在 pom.xml
中定义的插件
也就是说,eclipse 的构建只会让您达到 运行ning "maven compile" 的程度。
如果您有需要在打包阶段执行的插件,则必须手动 运行 一个 "maven package" 配置。
如果您有在 "compile" 之前的任何阶段执行的插件,它们也可以 运行 在 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>
这将导致 templating-maven-plugin
运行 每个 eclipse 构建的 filter-sources
目标。请注意,您可以确定是否要在配置(完整)构建(Project Clean 或 Maven Update 之后)上执行插件,and/or 在增量构建上(编辑一些源代码后)
你也可以设置
<action>
<ignore />
</action>
这将导致 m2e 在其构建期间忽略插件(在启动 Maven 运行 配置时它仍然 运行)
- 有些插件并不总是能很好地与 eclipse 配合使用,例如,如果您需要 unpack/copy 您在工作区中作为项目的依赖项。不过有一些解决方法。
如果您需要更多帮助或者我哪里不清楚,请告诉我
我找到了一个基于 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
更新我的源代码后,我目前必须手动执行两个操作:
- 使用 Alt+F5 更新我的 Maven 项目(这会使用 pom.xml 文件中的相应设置覆盖 Eclipse 项目设置,例如更新类路径文件)
- 运行 我的主要 pom.xml 文件带有 Maven 运行 配置(这会执行 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>
您问题的答案实际上取决于您尝试执行的插件以及您需要在什么时候执行它们。
据我所知,m2e 将自己的构建器添加到您的 eclipse 项目中,并且它可以调用您在 pom.xml
中定义的插件也就是说,eclipse 的构建只会让您达到 运行ning "maven compile" 的程度。 如果您有需要在打包阶段执行的插件,则必须手动 运行 一个 "maven package" 配置。
如果您有在 "compile" 之前的任何阶段执行的插件,它们也可以 运行 在 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>
这将导致 templating-maven-plugin
运行 每个 eclipse 构建的 filter-sources
目标。请注意,您可以确定是否要在配置(完整)构建(Project Clean 或 Maven Update 之后)上执行插件,and/or 在增量构建上(编辑一些源代码后)
你也可以设置
<action>
<ignore />
</action>
这将导致 m2e 在其构建期间忽略插件(在启动 Maven 运行 配置时它仍然 运行)
- 有些插件并不总是能很好地与 eclipse 配合使用,例如,如果您需要 unpack/copy 您在工作区中作为项目的依赖项。不过有一些解决方法。
如果您需要更多帮助或者我哪里不清楚,请告诉我
我找到了一个基于 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