github.com/xgoffin/jenkins-library@v1.154.0/cmd/mavenExecuteStaticCodeChecks.go (about) 1 package cmd 2 3 import ( 4 "github.com/SAP/jenkins-library/pkg/log" 5 "github.com/SAP/jenkins-library/pkg/maven" 6 "github.com/SAP/jenkins-library/pkg/telemetry" 7 "strconv" 8 ) 9 10 func mavenExecuteStaticCodeChecks(config mavenExecuteStaticCodeChecksOptions, telemetryData *telemetry.CustomData) { 11 err := runMavenStaticCodeChecks(&config, telemetryData, maven.NewUtilsBundle()) 12 if err != nil { 13 log.Entry().WithError(err).Fatal("step execution failed") 14 } 15 } 16 17 func runMavenStaticCodeChecks(config *mavenExecuteStaticCodeChecksOptions, telemetryData *telemetry.CustomData, utils maven.Utils) error { 18 var defines []string 19 var goals []string 20 21 if !config.SpotBugs && !config.Pmd { 22 log.Entry().Warnf("Neither SpotBugs nor Pmd are configured. Skipping step execution") 23 return nil 24 } 25 26 if config.InstallArtifacts { 27 err := maven.InstallMavenArtifacts(&maven.EvaluateOptions{ 28 M2Path: config.M2Path, 29 ProjectSettingsFile: config.ProjectSettingsFile, 30 GlobalSettingsFile: config.GlobalSettingsFile, 31 }, utils) 32 if err != nil { 33 return err 34 } 35 } 36 37 if testModulesExcludes := maven.GetTestModulesExcludes(utils); testModulesExcludes != nil { 38 defines = append(defines, testModulesExcludes...) 39 } 40 if config.MavenModulesExcludes != nil { 41 for _, module := range config.MavenModulesExcludes { 42 defines = append(defines, "-pl") 43 defines = append(defines, "!"+module) 44 } 45 } 46 47 if config.SpotBugs { 48 spotBugsMavenParameters := getSpotBugsMavenParameters(config) 49 defines = append(defines, spotBugsMavenParameters.Defines...) 50 goals = append(goals, spotBugsMavenParameters.Goals...) 51 } 52 if config.Pmd { 53 pmdMavenParameters := getPmdMavenParameters(config) 54 defines = append(defines, pmdMavenParameters.Defines...) 55 goals = append(goals, pmdMavenParameters.Goals...) 56 } 57 finalMavenOptions := maven.ExecuteOptions{ 58 Goals: goals, 59 Defines: defines, 60 ProjectSettingsFile: config.ProjectSettingsFile, 61 GlobalSettingsFile: config.GlobalSettingsFile, 62 M2Path: config.M2Path, 63 LogSuccessfulMavenTransfers: config.LogSuccessfulMavenTransfers, 64 } 65 _, err := maven.Execute(&finalMavenOptions, utils) 66 return err 67 } 68 69 func getSpotBugsMavenParameters(config *mavenExecuteStaticCodeChecksOptions) *maven.ExecuteOptions { 70 var defines []string 71 if config.SpotBugsIncludeFilterFile != "" { 72 defines = append(defines, "-Dspotbugs.includeFilterFile="+config.SpotBugsIncludeFilterFile) 73 } 74 if config.SpotBugsExcludeFilterFile != "" { 75 defines = append(defines, "-Dspotbugs.excludeFilterFile="+config.SpotBugsExcludeFilterFile) 76 } 77 if config.SpotBugsMaxAllowedViolations != 0 { 78 defines = append(defines, "-Dspotbugs.maxAllowedViolations="+strconv.Itoa(config.SpotBugsMaxAllowedViolations)) 79 } 80 81 mavenOptions := maven.ExecuteOptions{ 82 // check goal executes spotbugs goal first and fails the build if any bugs were found 83 Goals: []string{"com.github.spotbugs:spotbugs-maven-plugin:4.1.4:check"}, 84 Defines: defines, 85 } 86 87 return &mavenOptions 88 } 89 90 func getPmdMavenParameters(config *mavenExecuteStaticCodeChecksOptions) *maven.ExecuteOptions { 91 var defines []string 92 if config.PmdMaxAllowedViolations != 0 { 93 defines = append(defines, "-Dpmd.maxAllowedViolations="+strconv.Itoa(config.PmdMaxAllowedViolations)) 94 } 95 if config.PmdFailurePriority >= 1 && config.PmdFailurePriority <= 5 { 96 defines = append(defines, "-Dpmd.failurePriority="+strconv.Itoa(config.PmdFailurePriority)) 97 } else if config.PmdFailurePriority != 0 { 98 log.Entry().Warningf("Pmd failure priority must be a value between 1 and 5. %v was configured. Defaulting to 5.", config.PmdFailurePriority) 99 } 100 101 mavenOptions := maven.ExecuteOptions{ 102 // check goal executes pmd goal first and fails the build if any violations were found 103 Goals: []string{"org.apache.maven.plugins:maven-pmd-plugin:3.14.0:check"}, 104 Defines: defines, 105 } 106 107 return &mavenOptions 108 }