Maven dependency command not working - maven

I can run mvn clean compile and mvn install from command prompt but mvn dependency: tree doesn't work. I get:
Invalid task 'dependency': you must specify a valid lifecycle phase, or a goal in the format plugin:goal or pluginGroupId:pluginArtifactId : pluginVersion: goal.
Also for mvn eclipse : eclipse , I get
Invalid task 'eclipse' :blah
Am I missing any configuration here?
Thanks.

It is
mvn dependency:tree
mvn eclipse:eclipse
The part before the colon indicates the maven plugin, the part after the colon is the goal within the plugin that should be invoked. There must not be a space between the plugin name, the colon and the goal so that maven recognizes it as one argument
mvn <plugin>:<goal>
If you put a space there, maven thinks that each is is a separate argument and would e.g. try to run a goal "dependency" and another goal "tree".
Default lifecycle names (clean, install, etc.) are built-in and not part of plugins - therefore they work as they are without the ":" notation.

Related

Maven run plugin goal and preceding phases

Usually when we run a plugin goal in maven directly from the command line, the build phases preceding the one the plugin goal is bound to will not be run.
How can we run the plugin goal and all its preceding build phases?
There is no special support for this. But you can call
mvn preceding-phase goal:you-want-to-call
which will be essentially what you want.

Maven - Running mvn without arguments

Does running mvn with no arguments in the directory of the project result in the default lifecylce being executed for that project?
What if a profile is specified with the -P flag? does it execute the default lifecycle and binds any plugins in the profile to the phases they declare?
On your pom you can use the tag defaultGoal to specify the goal that should be executed by maven if you do not specify nothing on the command line.
See POM reference

Maven plugin removed is still usable

I added to my maven project the a PMD and checkstyle plugins. And when I run them the work perfectly. But when I remove them from the pom.xml I can still run mvn checkstyle:checkstyle or mvn pmd:pmd even though I removed them. Also after removing them I ran mvn clean install. ANy idea of what could happen ?
The commands you execute are plugin goals (plugin:goal) and unlike "mvn install" not a phase.
you can run almost any plugin on a project if maven can find it. The apache maven plugins allow that shortcut notation (pmd:pmd) since maven will try to resolve them in the apache namespace.
Plugins from other sources would need to be run with their full name, for example:
org.codehaus.mojo:versions-maven-plugin:2.5:display-dependency-updates
The plugin itself decides if it can run a goal on its own or if it requires a running reactor and only works within the maven life-cycle (usually because it depends on outputs from other phases)
So in your case: mvn install should not run the pmd plugin anymore if its not in the pom - and install is a phase. mvn pmd:pmd will run it directly with its default config - since pmd:pmd is a plugin goal.
The default plugins per packaging and phase are documented here. These may run if in the pom or not (depending on whats in the project).

Is there a way to invoke a goal of a particular version of a Maven plugin?

I would like to invoke a goal of a particular version in a Maven plugin from the command line.
How can this be achieved?
I'm looking for something along the lines of:
mvn org.foo:my-plugin:1.2.3-SNAPSHOT:do-something
Your first guess is correct. The scheme for such a command is
mvn groupid:artifactid:version:goal
Example:
mvn org.apache.maven.plugins:maven-compiler-plugin:3.0:compile

Maven questions

When I just type mvn command in my machine where pom is located, which phase it is going to run? mvn install or deploy??
When I use mvn eclipse:eclipse in my machine to convert maven project to eclipse project, what phases it is going to execute from default life cycle? is it going to run all the phases again?
When I just type mvn command in my machine where pom is located, which phase it is going to run?
No goal is executed, instead you'll get:
[ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal [...]
You can specify default goal with the following pom.xml declaration:
<build>
<defaultGoal>install</defaultGoal>
...
</build>
When I use mvn eclipse:eclipse in my machine to convert maven project to eclipse project, what phases it is going to execute from default life cycle?
It's described in the documentation:
Attributes:
Requires a Maven project to be executed.
Invokes the execution of the lifecycle phase generate-resources prior to executing itself.
See also
How do you specify a string of goals as the defaultGoal in maven 2?

Resources