ChatGPT解决这个技术问题 Extra ChatGPT

Maven command to list lifecycle phases along with bound goals?

I'm just learning Maven, and so this might be obvious, but I can't find an easy way to list the goals associated for each maven lifecycle phase for a given project.

I saw that the Maven default life cycle phases and corresponding default goals are documented here. My understanding so far is that each pom.xml can bind additional goals to each lifecycle phase.

So, is there a mvn command to determine the goals that will be run for each lifecycle phase for a given project? If not, I guess I just have to look through the pom.xml for each new maven project to figure this out?


C
Chad Nouis

The buildplan-maven-plugin is an excellent tool for showing how goals are bound to phases.

Below are examples of commands you can run. The commands will automatically download and install the plugin if it hasn't already been installed.

List goals by the order they will execute

> 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

Group goals by phase

> 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

Group goals by plugin

> 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

Notes

By default, the goals search for tasks that would run if the user invoked mvn deploy. Phases such as clean won't be included. To include multiple phases in the search, use the buildplan.tasks property:

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

This should really be built into Maven by default.
also works without adjusting the pom: mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list -Dbuildplan.tasks=clean,install,deploy
This answer would be perfect if it started off with FibreFoX's comment.
I think this answer is much better than the accepted answer.
I agree with @Ianoxx that this should be added to Maven by default. Great plug-in!
S
Sean Patrick Floyd

mvn help:describe -Dcmd=compile (or any other valid phase)


Not seeing goals bound to phases in the output of this command. Instead, I am seeing plugins/phases.
Mabye I understood the question wrong, but this command is the "reverse lookup" to the question (Listing all phases this goal is in)? link says "Displays a list of the attributes for a Maven Plugin and/or goals (aka Mojo - Maven plain Old Java Object)."
a
abyx

One tool that helps is mvn help:effective-pom It will print the POM with all variables and all parent POMs expanded. This helps to understand what Maven sees. From that, it's pretty simple to find all the additional goals (which usually aren't that many).

The bigger problem is the implicit goals (i.e. when a plugin hooks itself to some phases of the lifecycle automatically). There is no easy way to see these without actually running Maven. This should become better in Maven 3. Until then, run Maven with -X which will print a whole lot of debug output plus the current phase and which plugins are executed.


Thanks, Aaron, this was helpful!
How is this getting better in Maven 3? Is it in the current alpha-6?
Jason told me that the new Maven 3 will build a model of the whole build before actually starting it. That means that you can examine (and print) the hooks without running the commands.
the effective pom should show the actual binding phase of each goal which currently is not doing that... sigh..
S
Sandip Chitale

If not with Maven but using m2e you can do it using the code block that you can use in a Eclipse plugin:

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);
                }

Look at full source.

Already implemented in:

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

It makes use of m2e's ability to compute the association of goals with phases. I am also trying to solve it at maven level.


Please do not post link only answers
C
Craig

I put Chad's answer into a script (so I don't have to remember the plugin name which is really long). Put it in your ~/bin/ folder so you can use it anywhere.

#!/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;