Maven命令列出生命周期阶段以及约束目标?

我只是在学习Maven,所以这可能是显而易见的,但我找不到一个简单的方法来列出给定项目的每个Maven生命周期阶段的相关目标。

我看到了Maven默认的生命周期阶段和相应的默认目标。 我的理解到目前为止,每个pom.xml可以将其他目标绑定到每个生命周期阶段。

那么,是否有一个mvn命令来确定将在给定项目的每个生命周期阶段运行的目标? 如果没有,我想我只需要通过每个新的Maven项目的pom.xml来看看这个呢?

mvn help:describe -Dcmd=compile (或其他任何有效的阶段)

buildplan-maven-plugin是展示目标如何绑定到阶段的绝佳工具。

以下是您可以运行的命令示例。 如果尚未安装,该命令将自动下载并安装该插件。

按照执行顺序列出目标

 > mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list PLUGIN | PHASE | ID | GOAL -------------------------------------------------------------------------------------------- maven-enforcer-plugin | validate | default | enforce maven-dependency-plugin | process-sources | default | copy-dependencies maven-resources-plugin | process-resources | default-resources | resources maven-compiler-plugin | compile | default-compile | compile maven-resources-plugin | process-test-resources | default-testResources | testResources maven-compiler-plugin | test-compile | default-testCompile | testCompile maven-surefire-plugin | test | default-test | test maven-jar-plugin | package | default-jar | jar maven-assembly-plugin | package | make-assembly | single maven-install-plugin | install | default-install | install maven-deploy-plugin | deploy | default-deploy | deploy 

按阶段分组

 > mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list-phase validate ----------------------------------------------------------------- + maven-enforcer-plugin | default | enforce process-sources ---------------------------------------------------------- + maven-dependency-plugin | default | copy-dependencies process-resources -------------------------------------------------------- + maven-resources-plugin | default-resources | resources compile ------------------------------------------------------------------ + maven-compiler-plugin | default-compile | compile process-test-resources --------------------------------------------------- + maven-resources-plugin | default-testResources | testResources test-compile ------------------------------------------------------------- + maven-compiler-plugin | default-testCompile | testCompile test --------------------------------------------------------------------- + maven-surefire-plugin | default-test | test package ------------------------------------------------------------------ + maven-jar-plugin | default-jar | jar + maven-assembly-plugin | make-assembly | single install ------------------------------------------------------------------ + maven-install-plugin | default-install | install deploy ------------------------------------------------------------------- + maven-deploy-plugin | default-deploy | deploy 

按插件分组目标

 > mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list-plugin maven-enforcer-plugin --------------------------------------------------- + validate | default | enforce maven-dependency-plugin ------------------------------------------------- + process-sources | default | copy-dependencies maven-resources-plugin -------------------------------------------------- + process-resources | default-resources | resources + process-test-resources | default-testResources | testResources maven-compiler-plugin --------------------------------------------------- + compile | default-compile | compile + test-compile | default-testCompile | testCompile maven-surefire-plugin --------------------------------------------------- + test | default-test | test maven-jar-plugin -------------------------------------------------------- + package | default-jar | jar maven-assembly-plugin --------------------------------------------------- + package | make-assembly | single maven-install-plugin ---------------------------------------------------- + install | default-install | install maven-deploy-plugin ----------------------------------------------------- + deploy | default-deploy | deploy 

笔记

默认情况下,目标search用户调用mvn deploy将运行的任务。 不包括clean阶段。 要在search中包含多个阶段,请使用buildplan.tasks属性:

 > mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list -Dbuildplan.tasks=clean,deploy 

一个mvn help:effective-pom工具是mvn help:effective-pom它将打印所有variables的POM,并展开所有父POM。 这有助于理解Maven所看到的内容。 从这个angular度来说,find所有额外的目标(通常并不是那么多)是非常简单的。

更大的问题是隐含的目标(即当插件自动挂钩到生命周期的某些阶段时)。 没有简单的方法来看到这些没有实际运行Maven。 在Maven 3中,这应该会变得更好。在此之前,用-X运行Maven将会打印出大量的debugging输出加上当前的阶段以及哪些插件被执行。

如果不是使用Maven,而是使用m2e,则可以使用可在Eclipse插件中使用的代码块来执行此操作:


 final IMavenProjectRegistry projectRegistry = MavenPlugin.getMavenProjectRegistry(); final IMavenProjectFacade facade = projectRegistry.getProject(project); projectRegistry.execute(facade, new ICallable<Void>() { public Void call(IMavenExecutionContext context, IProgressMonitor monitor) throws CoreException { MavenProject mavenProject = facade.getMavenProject(monitor); List<MojoExecution> mojoExecutions = ((MavenProjectFacade) facade).getMojoExecutions(monitor); LifecycleMappingResult mappingResult = LifecycleMappingFactory.calculateLifecycleMapping( mavenProject, mojoExecutions, facade.getResolverConfiguration().getLifecycleMappingId(), monitor); Map<MojoExecutionKey, List<IPluginExecutionMetadata>> mojoExecutionMapping = mappingResult .getMojoExecutionMapping(); Map<String, List<MojoExecutionKey>> phases = new LinkedHashMap<String, List<MojoExecutionKey>>(); for (MojoExecutionKey execution : mojoExecutionMapping.keySet()) { List<MojoExecutionKey> executions = phases.get(execution.getLifecyclePhase()); if (executions == null) { executions = new ArrayList<MojoExecutionKey>(); phases.put(execution.getLifecyclePhase(), executions); } executions.add(execution); } 

看完整的来源 。

已经在:

http://marketplace.eclipse.org/content/phases-and-goals

它利用了m2e的能力来计算目标与阶段的关联。 我也试图在maven级别解决它。