Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Why can I run mvn checkstyle:checkstyle without pom.xml config?

I'm using a a basic maven project where only the following things are defined in pom.xml:

  • dependency javaee-api
  • dependency junit
  • plugin maven-compile-plugin
  • plugin maven-war-plugin
  • plugin wildfly-maven-plugin

Why can I run mvn checkstyle:checkstyle in the command window? Shouldn't I have to define the checkstyle plugin the pom.xml? What am I missing?

Edit: The eclipse plugin "Eclipse Checkstyle Plug-In" is installed. Is that the reason? If yes, how does maven communicate with it?

like image 451
NthDegree Avatar asked Nov 27 '15 12:11

NthDegree


People also ask

How do you add Checkstyle to Pom?

To generate the Checkstyle report as part of the Project Reports, add the Checkstyle Plugin in the <reporting> section of your pom. xml . Then, execute the site phase to generate the report.

What does Checkstyle XML do?

Checkstyle obtains a configuration from an XML document whose elements specify the configuration's hierarchy of modules and their properties. You provide a file that contains the configuration document when you invoke Checkstyle at the command line, and when you run a Checkstyle task in ant.

How do I fix Checkstyle issue in IntelliJ?

Go to Settings|Editor|Code Style, choose a code style you want to import CheckStyle configuration to. Click on the gear, then 'import scheme', choose "CheckStyle Configuration" and select a corresponding CheckStyle configuration file. Click OK.


2 Answers

As you have already seen it is possible to execute plugins directly from the command line without configuring the plugin in your project's POM file.

To know which plugin you want to execute maven uses plugin prefix resolution.

Using your command line, mvn checkstyle:checkstyle, as an example this is roughly what maven does:

  • take the prefix, the bit before the colon, from the execution. In your case checkstyle.
  • resolve this to the possible name (artifactId) of a plugin. For this it uses two possible formats:
    • maven-${prefix}-plugin (for official Maven plugins)
    • ${prefix}-maven-plugin (for plugins from other sources)
  • so now it has maven-checkstyle-plugin as the artifactId of the plugin
  • search for a plugin with this artifactId and one of the configured groupIds (by default maven searches using the groupId org.apache.maven.plugins)
  • it finds the maven-checkstyle-plugin
  • executes the goal (the bit after the colon in the command), so the checkstyle goal in this case.

All of this is explained in far more detail and better than I can at the plugin prefix mapping page of the official maven documentation.

like image 144
DB5 Avatar answered Sep 25 '22 19:09

DB5


The reason is that a plugin might not need to be explicitly configured in the pom nor to be bound to a phase to be run. And so, it can always be run by direct invocation (specifying the goal instead the phase when invoking to Maven):

mvn [options] [<goal(s)>] [<phase(s)>]

Look at the lifecycle documentation:

A plugin goal represents a specific task (finer than a build phase) which contributes to the building and managing of a project. It may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation.

It definitely has nothing to be with Eclipse plugins.

like image 27
Little Santi Avatar answered Sep 22 '22 19:09

Little Santi