【问题标题】:How to prevent a Java project from building when TODO present当 TODO 存在时如何防止构建 Java 项目
【发布时间】:2017-07-13 04:31:34
【问题描述】:

最近包含调试覆盖的代码已发布到生产环境。代码标记清楚

// TODO - Remove before releasing to production

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

你们如何捕捉调试代码并防止构建?

【问题讨论】:

    标签: java maven comments todo


    【解决方案1】:

    如果在您的代码中找到 TODO comments,您可以配置 maven checkstyle plugin 以使构建失败。


    让 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>
    

    您还可以通过选择 pre-built checkstyle.xml 来包含更多检查


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

    运行:

    mvn clean install -Pci-build

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

    <module name="TodoComment">
      <property name="severity" value="error"/>
      <property name="format" value="(TODO)|(FIXME)"/>
    </module>
    

    并且您可以在需要时使用属性来打开失败,例如用于服务器构建但不是本地的 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>
    

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

      <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>
    

    我将 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>
    

    【讨论】:

    • 正是我想要的!谢谢。是否有允许在跳过 checkstyle 插件的同时进行构建的覆盖?我希望能够在 dev 中构建(跳过 checkstyle),但在构建 prod 时不跳过?
    • 我也是这么想的,你有几个选择,你可以使用配置文件或属性。您是否希望它在 ci-build 时总是失败,例如在 pull request 或在 Jenkins 等构建时提供快速反馈?
    • 哇!伟大而完整的答案。谢谢。
    【解决方案2】:

    我正在使用 maven checkstyle 插件:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.15</version>
        <executions>
            <execution>
                <id>validate</id>
                <phase>validate</phase>
                <configuration>
                    <configLocation>src/main/resources/config/checkstyle.xml</configLocation>
                    <encoding>UTF-8</encoding>
                    <consoleOutput>true</consoleOutput>
                    <failsOnError>false</failsOnError>
                    <linkXRef>false</linkXRef>
                </configuration>
                <goals>
                    <goal>check</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    配置文件应该包含一个TodoComment 模块这将防止构建一个内部带有“TODO”cmets 的代码。您可以对其进行配置,使其查看其他注释正则表达式,例如“FIXME”或其他内容。 Here你有更多的细节。

    【讨论】:

      【解决方案3】:

      您可以使用以下插件来检测 TODO 和/或 FIXME(或您定义的任何其他注释文本)并将构建标记为不稳定或失败(如果发现): Task Scanner Plugin

      但是,此分析将在提取代码后执行,因此这将取决于您的移动到生产流程的设置方式。该插件允许您将构建作业标记为失败(一旦将其添加为构建后步骤),然后如果检测到 TODO,您可以取消任何下游作业。如果您的迁移到生产作业在此构建作业的下游,这将解决您的问题。

      【讨论】:

        猜你喜欢
        • 2012-04-24
        • 1970-01-01
        • 2017-06-11
        • 1970-01-01
        • 1970-01-01
        • 2013-07-01
        • 2017-01-01
        • 1970-01-01
        • 2011-10-09
        相关资源
        最近更新 更多