mavenjenkinssonarqubeversions-maven-plugin

report an update in parent version to sonar or jenkins


When a maven build has a parent that is not the latest release version, the goal of the versions plugin display-parent-updates is reporting it nicely but I would like to report this somehow:

  1. mark the build with a WARNING or UNSTABLE status (to have a colored visual in jenkins)
  2. add this in the jacoco report (no idea if this is possible, would be great)-> to be visible in sonar
  3. maybe use the enforcer plugin (don't know if this would help)

Any suggestions?


Solution

  • I achieved this in 2 steps : 
    
    1. Use of the following plugin 
    
    `<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>display-dkv-boot-starter-parent-updates</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>display-parent-updates</goal>
                    </goals>
                    <configuration>
                        <allowSnapshots>true</allowSnapshots>
                    </configuration>
                </execution>
            </executions>
        </plugin>`
    
    1. Use of the enforce plugin with a custom rule

      public class MyCustomRule implements EnforcerRule2 {

           private boolean fail = false;
      
           @Override
           public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
               Log log = helper.getLog();
               List<String> lines = Collections.emptyList();
               try {
                   Path path = Paths.get((String) helper.evaluate("${versions.outputFile}"));
                   lines = Files.readAllLines(path);
                   fail = lines.stream()
                               .anyMatch(l -> l.contains("The parent project has a newer version"));
               } catch (Exception ex) {
                   log.warn(ex);
                   fail = false;
               }
      
               if (this.fail) {
                   throw new EnforcerRuleException(lines.toString());
               }
           }
      
           @Override
           public boolean isCacheable() {
               return false;
           }
      
           @Override
           public boolean isResultValid(EnforcerRule enforcerRule) {
               return fail;
           }
      
           @Override
           public String getCacheId() {
               return null;
           }
      
           @Override
           public EnforcerLevel getLevel() {
               return EnforcerLevel.WARN;
           }
       }