Maven命令列出生命周期阶段以及受限制的目标?


104

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

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

因此,是否有mvn命令来确定将为给定项目在每个生命周期阶段运行的目标?如果没有,我想我只需要仔细检查每个新的Maven项目的pom.xml即可解决?

Answers:


128

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


3
在此命令的输出中没有看到与阶段有关的目标。相反,我看到的是插件/阶段。
德米特里·明科夫斯基

Mabye我理解这个问题是错误的,但是此命令是对该问题的“反向查找”(列出此目标所处的所有阶段)?链接说:“显示Maven插件和/或目标(又名Mojo-Maven普通的旧Java对象)的属性列表。”
user2039709 '16

131

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

笔记

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

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

33
默认情况下,这确实应该内置到Maven中。
lanoxx '16

5
也可以在不调整pom的情况下工作:mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list -Dbuildplan.tasks=clean,install,deploy
FibreFoX '16

1
如果从FibreFoX的评论开始,这个答案将是完美的。
UFL1138 '16

我认为这个答案比公认的答案要好得多。
Panayotis

我同意@Ianoxx,默认情况下应将其添加到Maven。很棒的插件!
米格尔·帕达尔

18

一种有用的工具是mvn help:effective-pom它将打印具有所有变量和所有父POM扩展的POM。这有助于了解Maven看到的内容。由此,找到所有其他目标(通常不是很多)非常简单。

更大的问题是隐式目标(即,当插件自动将自身挂接到生命周期的某些阶段时)。如果没有实际运行Maven,就没有简单的方法来查看这些内容。这在Maven 3中应该会变得更好。在此之前,运行Maven -X,它将打印大量调试输出以及当前阶段以及将执行哪些插件。


谢谢,亚伦,这很有帮助!
Upgradingdave

Maven 3的情况如何?在当前的alpha-6中吗?
Lars Corneliussen

2
Jason告诉我,新的Maven 3将在实际启动之前构建整个构建的模型。这意味着您可以在不运行命令的情况下检查(打印)钩子。
亚伦·迪古拉

有效的pom应该显示每个目标的实际绑定阶段,而目前还没有这样做……感叹..
Junjun Liu 2015年

1

如果不是使用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级别上解决它。


请不要发布仅链接的答案
bish 2015年

1

我将Chad的答案放入脚本中(因此,我不必记住很长的插件名称)。将其放在〜/ bin /文件夹中,以便可以在任何地方使用它。

#!/usr/bin/env bash
# Created based on https://stackoverflow.com/a/35610377/529256
debug=false

goal='list-phase'
build_plan='clean,deploy'
working_directories=""

for (( i=1; i<=$#; i++ )) do
    case ${!i} in

        -h|--help)
            programName=$( basename ${0} )
            echo "Lists the goals of mvn project(s) by phase in a table";
            echo
            echo "Usage:";
            echo "    ${programName} -d|--debug -g|--goal goal -b|--build_plan build_plan [*directory]";
            echo
            echo "           --goal  The goal for the buildplan-maven-plugin (default: $goal)"
            echo "                   (possible values: list, list-plugin, list-phase)"
            echo
            echo "     --build_plan  The value of the buildplan.tasks parameter (default: $build_plan)"
            echo "                   (examples: 'clean,install', 'deploy', 'install', etc...) "
            echo
            echo "     [*directory]  The directories (with pom.xml files) to run the command in"
            exit 0;
            ;;
        -d|--debug)
            debug=true;
            echo "debug = ${debug}";
            ;;
        -b|--build_plan)
            ((i++))
            build_plan="${!i}"
            ;;
        -g|--goal)
            ((i++))
            goal="${!i}"
            ;;
        *)
            working_directory="${!i}";
            if [ ! -e "${working_directory}" ]; then
                echo "'${working_directory}' doesn't exist";
                exit 1;
            fi;
            if [ -z "${working_directories}" ]; then
                working_directories="$working_directory"
            else
                working_directories="$working_directories ${!i}"
            fi;
            ;;
    esac;
done;

if [ -z "${working_directories}" ]; then
    working_directories="$PWD"
fi

if [ ${debug} = true ]; then
    echo "working_directories=$working_directories"
    echo "goal=$goal"
    echo "build_plan=$build_plan"
fi

for workingDirectory in ${working_directories}; do
    pushd ${workingDirectory} > /dev/null
    echo "cd $workingDirectory"
    echo "mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:${goal} -Dbuildplan.tasks=${build_plan}"
    mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:${goal} -Dbuildplan.tasks=${build_plan}
    popd > /dev/null
done;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.