Cerber
Cerber

Reputation: 2939

How to make a maven build fail if source code contains a keyword / regex

Question

How to make a maven build fail if source code contains a keyword / regex?

Bonus

Solution

(Based on current answers 2013-09-26)

Best solution yet seems to be @BaptisteMathus answer that fully integrates with maven and is platform independant.

In my use case, @GregWhitaker answer is the good one because it's cheaper to implement as I don't care about platform independency (<= the required command is availiable on all my hosts). The code sample below is a solution based on this answer, it forbids usage of "FIXME" or "Auto-generated method stub" but is assuming that egrep is availiable.

Please see also @MarkOConnor answer that is cleaner in "SONAR enabled" project

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <configuration>
                        <executable>egrep</executable>
                        <successCodes>
                            <successCode>1</successCode>
                        </successCodes>
                        <arguments>
                            <argument>-rqm</argument>
                            <argument>1</argument>
                            <!-- Forbidden Keywords -->
                            <argument>FIXME|Auto-generated method stub</argument>
                            <!-- search path -->
                            <argument>src/main</argument>
                        </arguments>
                    </configuration>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Upvotes: 3

Views: 1127

Answers (4)

Robert Scholte
Robert Scholte

Reputation: 12345

I think the taglist-maven-plugin is exactly what you want.

Upvotes: 1

Baptiste Mathus
Baptiste Mathus

Reputation: 918

Going to SonarQube to do that is not a bad idea.

Anyway, if someone wants to use a maven-only solution, then the right way would then be by using a plugin dedicated to "enforce" things with maven builds. Using exec-maven-plugin is not that standard and certainly too much platform-dependent.

This plugin is logically named maven-enforcer-plugin and writing a custom enforcer rule is actually very simple.

Upvotes: 2

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77971

Sonar has a taglist plugin, which allows you to search for strings in your comment blocks and specify the severity handling. I'm assuming that is what you're looking for... Parsing the source code itself might require a custom rule for a tool like checkstyle, I haven't tried this approach but it's documented on the Sonar site.

This can be coupled with the build breaker plugin, which fails your build when an alert criteria is breached in your project's quality profile.

Upvotes: 1

gregwhitaker
gregwhitaker

Reputation: 13410

I would create a small program to run these checks and then execute it via the exec-maven-plugin. If you find the keyword in the main resources then just return a non-zero return code which will cause the plugin to fail the build.

Upvotes: 1

Related Questions