如何在TODO存在时阻止Java项目的构建

Tod*_*odd 1 java comments maven todo

最近包含调试覆盖的代码已发布到生产中.代码清楚标明

// TODO - Remove before releasing to production

但我们没有集成到Maven中,这会阻止项目的建立.我见过一个名为Taglist的maven插件,可以生成一个报告.但不会停止产生构建错误.

你们如何捕获调试代码并阻止构建?

Jam*_*mes 5

如果在代码中找到TODO注释,您可以配置maven checkstyle插件以使构建失败.


让checkstyle失败的构建

为了让checkstyle失败,我按照这个答案的建议来回答类似的问题:https://stackoverflow.com/a/42276077/7421645他们发现他们需要添加配置<violationSeverity>warning</violationSeverity>TodoComment需要包含正则表达式格式属性构建失败.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.17</version>
    <dependencies>
      <dependency>
        <groupId>com.puppycrawl.tools</groupId>
        <artifactId>checkstyle</artifactId>
        <version>7.5.1</version>
      </dependency>
    </dependencies>
    <executions>
      <execution>
        <id>validate</id>
        <phase>validate</phase>
        <goals>
          <goal>check</goal>
        </goals>
        <configuration>
          <logViolationsToConsole>true</logViolationsToConsole>
          <checkstyleRules>
          <module name="Checker">
            <module name="TreeWalker">
              <module name="TodoComment">
                <property name="format" value="(TODO)"/>
              </module>
            </module>
          </module>
          </checkstyleRules>
          <encoding>UTF-8</encoding>
          <consoleOutput>true</consoleOutput>
          <failOnViolation>true</failOnViolation>
          <failsOnError>true</failsOnError>
          <violationSeverity>warning</violationSeverity>
        </configuration>
      </execution>
    </executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

您还可以通过选择预先构建的checkstyle.xml来包含更多检查


始终运行checkstyle,但只有在激活配置文件时才会失败-Pci-build

跑步:

mvn clean install -Pci-build

如果你使用checkstyle,我希望你能从中收集更多的价值,而不仅仅是检查TODO评论.看起来您不能配置多个checkstyle配置,例如Jenkins构建作业的内联和configLocation.但是,如果修改适合项目的checkstyle.xml,则可以修改要作为错误的模块的严重性:

<module name="TodoComment">
  <property name="severity" value="error"/>
  <property name="format" value="(TODO)|(FIXME)"/>
</module>
Run Code Online (Sandbox Code Playgroud)

并且您可以使用属性在需要时打开故障,例如,对于服务器构建而不是maven中的本地pom.xml:

  <properties>
    <fail.on.error>false</fail.on.error>
  </properties>

  <profiles>
    <profile>
      <id>ci-build</id>
      <properties>
        <fail.on.error>true</fail.on.error>
      </properties>
    </profile>
  </profiles>
Run Code Online (Sandbox Code Playgroud)

然后您可以将其用作构建配置中的属性:

  <configuration>
     <configLocation>my_google_checks.xml</configLocation>
     <encoding>UTF-8</encoding>
     <consoleOutput>true</consoleOutput>
     <failOnViolation>false</failOnViolation>
     <failsOnError>${fail.on.error}</failsOnError>
     <violationSeverity>warning</violationSeverity>
  </configuration>
Run Code Online (Sandbox Code Playgroud)

我将其更改failOnViolation为false以允许在checkstyle配置中发出警告.我正在使用google checkstyle的修改版本,但如果您只想检查TODO或其他一些内容,则没有理由不能将其应用于内联配置.

当配置文件"ci-build"传递给maven时,可以打开这种在构建器服务器上失败的方法.

如果未发送ci-build配置文件,则checkstyle仍会运行,但只会生成报告.当然你可以设置它,所以它仍然失败了我们认为值得出错的任何风格问题.


仅在配置文件激活-Pci-build时才运行checkstyle

跑步:

mvn clean install -Pci-build

在这种情况下,您不希望默认情况下运行checkstyle.因此,我们只需在需要时激活checkstyle构建配置文件.

 <profiles>
    <profile>
      <id>ci-build</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.17</version>
            <dependencies>
              <dependency>
                <groupId>com.puppycrawl.tools</groupId>
                <artifactId>checkstyle</artifactId>
                <version>7.5.1</version>
              </dependency>
            </dependencies>
            <executions>
              <execution>
                <id>validate</id>
                <phase>validate</phase>
                <goals>
                  <goal>check</goal>
                </goals>
                <configuration>
                  <configLocation>my_google_checks.xml</configLocation>
                  <encoding>UTF-8</encoding>
                  <consoleOutput>true</consoleOutput>
                  <failOnViolation>true</failOnViolation>
                  <failsOnError>true</failsOnError>
                  <violationSeverity>warning</violationSeverity>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
Run Code Online (Sandbox Code Playgroud)