How to prevent a Java project from building when TODO present

前端 未结 3 1020
挽巷
挽巷 2021-01-24 02:33

Recently code containing a debugging override was release to production. The code was clearly marked

// TODO - Remove before releasing to production

<
3条回答
  •  孤独总比滥情好
    2021-01-24 03:02

    You could configure a maven checkstyle plugin to fail the build if it find TODO comments in your code.


    To get checkstyle to fail a build

    To get checkstyle to fail a build I followed the advice of this answer to a similar question: https://stackoverflow.com/a/42276077/7421645 they found they needed to add the configuration warning and the TodoComment needs to include a regex format property to Fail the build.

      
        org.apache.maven.plugins
        maven-checkstyle-plugin
        2.17
        
          
            com.puppycrawl.tools
            checkstyle
            7.5.1
          
        
        
          
            validate
            validate
            
              check
            
            
              true
              
              
                
                  
                    
                  
                
              
              
              UTF-8
              true
              true
              true
              warning
            
          
        
      
    

    You can also include more checks by choosing a pre-built checkstyle.xml


    Always run checkstyle, but only fail if the profile is activated -Pci-build

    To run:

    mvn clean install -Pci-build

    If you were using checkstyle, I'd expect you would want to gather more value from it than just checking for TODO comments. It doesn't look like you can configure multiple checkstyle configurations, e.g. inline and a configLocation for a Jenkins build job. But if you modify a checkstyle.xml that is suitable for your project, you can modify the severity of modules that you want to be errors:

    
      
      
    
    

    And you can use a property to turn on failure when you want, e.g. for server builds but not local in your maven pom.xml:

      
        false
      
    
      
        
          ci-build
          
            true
          
        
      
    

    And then you can use that as a property in your build config:

      
         my_google_checks.xml
         UTF-8
         true
         false
         ${fail.on.error}
         warning
      
    

    I changed the failOnViolation to false to allow warnings in the checkstyle config to occur. And I'm using a modified version of the google checkstyle, but there is no reason why you couldn't apply the same with the inline config if you only wanted to check for TODO or a few other things.

    This approach of failing on a builder server can be turned on when the profile "ci-build" is passed to maven.

    When the ci-build profile isn't sent, checkstyle still runs, but just produces a report. Of course you could set it up so it still failed on anything style concerns that we deemed worthy of an error.


    Only run checkstyle if the profile is activate -Pci-build

    To run:

    mvn clean install -Pci-build

    In this case, you don't want checkstyle to run at all by default. So we just activate the checkstyle build profile when we want it.

     
        
          ci-build
          
            
              
                org.apache.maven.plugins
                maven-checkstyle-plugin
                2.17
                
                  
                    com.puppycrawl.tools
                    checkstyle
                    7.5.1
                  
                
                
                  
                    validate
                    validate
                    
                      check
                    
                    
                      my_google_checks.xml
                      UTF-8
                      true
                      true
                      true
                      warning
                    
                  
                
              
            
          
        
      
    

提交回复
热议问题