com.github.spotbugs spotbugs-maven-plugin in pom.xml <build> section or <reporting>

100 views Asked by At
  • What I am trying to achieve:

Run the maven spotbugs (findbugs) plugin during mvn site command, but not mvn clean install.

  • What did I try:

I tried putting the spotbugs plugin in the build section of the pom.xml

 <build>
        <plugins>
            <plugin>
<groupId>com.github.spotbugs</groupId>
                <artifactId>spotbugs-maven-plugin</artifactId>
                <version>4.8.2.0</version>
                <configuration>
                    <outputDirectory>target/reports/findbugs</outputDirectory>
                </configuration>
            </plugin>

and I also put it in the reporting section of the pom in the reporting section of the pom

<reporting>
        <plugins>
            <plugin>

Either way, it seems when running mvn site spotbugs:spotbugs, the plugin is running and trying to find bugs.

  • Question

I would like the plugin not to run at mvn clean install time, but to run at mvn site time.

Where is the correct place to add the plugin then? In the build section or the reporting section of the pom?

1

There are 1 answers

0
Turing85 On BEST ANSWER

The site lifecycle (maven.apache.org) has dedicated phases that are executed in the following order:

  • pre-site
  • site
  • post-site
  • site-deploy

These phases are only activated when we execute the site lifecycle (i.e. when we call mvn site ...).

If we want a plugin execution to execute only in the site lifecycle, we can bind the plugin's execution to one of those phases. For example, if we want to echo a message during the site lifecycle, we can use the following snippet:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    ... 
    <build>
        ...
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    ...
                    <execution>
                        <id>hello-world</id>
                        <!-- Here, we bind the execution to the "pre-site" phase -->
                        <phase>pre-site</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="We are executing the site lifecycle"/>
                            </target>
                        </configuration>
                    </execution>
                    ...
                </executions>
            </plugin>
            ...
        </plugins>
        ...
    </build>
</project>

The plugin execution will only be executed when we run mvn site ..., but not when we execute, for example, mvn install ....