views:

1683

answers:

3

I need to setup Maven plugins. I have downloaded the JARs. Can anyone please tell me what do I do next in order to integrate or setup the plugins with Maven? Should I copy the JARs into the parent directory or do I need to edit any file?

The plugins are:

  • Java2HTML
  • JDepend
  • Checkstyle
  • Clover
  • Cobertura
  • EMMA
  • Findbugs
  • JavaNCSS
  • PMD
  • QALab
  • Xradar
  • Sonar
A: 

you don't need to download the plugins manually. I'm not 100% sure, but if you want to use for example the checkstype pluging, you need to start maven with checkstyle parameter form command line

something like:

mvn checkstyle:checkstyle

or

mvn checkstyle:check

edit1: But you can also put the jars into the local m2 repository with the specific folder structure to access them.

edit2: you can put all your plugins into your own repository and then you need to tell maven (using the pom), which repositories you want to use. Every plugin must be described in the pom.

cupakob
A: 

Sirakov is right; Maven will download and install the plugins automatically when they are used.

You can either run them directly (for one-off jobs), or configure them in your pom.xml - this also allows you to configure then, and set the to run automatically, for example, to generate source code or report on test coverage. A major advantage of this is that you can define a single set of plugin configs in a shared parent pom, and reuse the same configurations across across all your projects, while still being able to override the inherited configuration in each subproject where necessary - this is one of the biggest advantages of using Maven on larger projects.

Each plugin has its own configuration parameters, the standard ones are documented at http://maven.apache.org/plugins/. Another good resource is the O'Reilly Maven book, online at http://www.sonatype.com/books/maven-book/reference/

An example configuration for cobertura:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <outputDirectory>${project.build.directory}/pmd</outputDirectory>
        <targetDirectory>${project.build.directory}</targetDirectory>
        <aggregate>true</aggregate>
        <!--  CPD minimum tokens to report on (5 to 10 duplicate lines)    --> 
        <minimumTokens>100</minimumTokens> 
        <minimumPriority>3</minimumPriority>
        <!--  Exclude mock classes     --> 
        <excludes>
        <exclude>**/Mock.*</exclude> 
        <exclude>**/Dummy.*</exclude> 
        <exclude>**/*Mock.java</exclude> 
        <exclude>**/*Dummy.java</exclude> 
        </excludes>
        <includeTests>true</includeTests> 
        <targetJdk>1.5</targetJdk>
        <rulesets>
            <ruleset>pmd_ruleset.xml</ruleset>  
        </rulesets>
    </configuration>
</plugin>
Andy Lynch
+5  A: 

If Maven has access to the central repository it will download most plugins (some are not hosted on central, to access those you need to define an additional repository in your pom or settings). If the dependencies are configured in your POM, Maven will automatically attempt to download them when you run a relevant goal. For the dependencies you listed this is mvn site.

The majority of those jars you've listed are reports, so should be declared in the reporting section of the POM, for example (I would also declare the versions to be sure you're getting the expected plugin):

<reporting>
  <plugins>
    <plugin>
      <artifactId>maven-checkstyle-plugin</artifactId>
    </plugin>
    <plugin>
      <artifactId>maven-pmd-plugin</artifactId>
      <configuration>
        <linkXref>true</linkXref>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>cobertura-maven-plugin</artifactId>
      <version>2.1</version>
      <configuration>
        <formats>
          <format>html</format>
          <format>xml</format>
        </formats>
        <outputDirectory>target/site/cobertura</outputDirectory>
      </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-surefire-report-plugin</artifactId>
      <configuration>
        <outputDirectory>${basedir}/target/surefire-reports</outputDirectory>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>jdepend-maven-plugin</artifactId>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>findbugs-maven-plugin</artifactId>
      <configuration>
        <xmlOutput>true</xmlOutput>
        <effort>Max</effort>
      </configuration>
    </plugin>
  </plugins>
</reporting>

Some background on Maven's plugin execution model: When you run mvn site, this is short hand for "run the site goal from the latest version of the site plugin", i.e. it is equivalent to mvn site:site, which is in turn shorthand for mvn org.apache.maven.plugins:maven-site-plugin:LATEST:site

Maven will attempt to contact the central repository, determine the LATEST version from the maven-metadata.xml, then download it (and any of its dependencies that are also missing) before executing it.

If you are behind a proxy you may see an error message in your build log like this:

[INFO] The plugin 'org.apache.maven.plugins:maven-site-plugin' does not exist or no valid version could be found

To address this you can declare proxy settings in your Maven settings.xml (in [MVN_HOME]/conf/settings.xml). They are commented out by defualt, but look something like this:

<proxy>
  <id>optional</id>
  <active>true</active>
  <protocol>http</protocol>
  <username>proxyuser</username>
  <password>proxypass</password>
  <host>proxy.host.net</host>
  <port>80</port>
  <nonProxyHosts>local.net,some.host.com</nonProxyHosts>
</proxy>

Replace the username, password, host, and port values with the relevant for your environment and Maven will be able to download the required dependencies.

For more details on using Maven, check out the Maven: The Definitive Guide by Sonatype, it is online and free.

Rich Seller
+1 for that great answer
bastianneu
stop it, you'll make me blush
Rich Seller