2020-03-09 09:58:59 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/maven"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
2020-03-30 15:59:59 +02:00
|
|
|
"strconv"
|
2020-03-09 09:58:59 +02:00
|
|
|
)
|
|
|
|
|
2020-03-12 16:45:57 +02:00
|
|
|
func mavenExecuteStaticCodeChecks(config mavenExecuteStaticCodeChecksOptions, telemetryData *telemetry.CustomData) {
|
2020-03-09 09:58:59 +02:00
|
|
|
c := command.Command{}
|
2020-05-06 13:35:40 +02:00
|
|
|
c.Stdout(log.Writer())
|
|
|
|
c.Stderr(log.Writer())
|
2020-03-09 09:58:59 +02:00
|
|
|
err := runMavenStaticCodeChecks(&config, telemetryData, &c)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("step execution failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 11:28:16 +02:00
|
|
|
func runMavenStaticCodeChecks(config *mavenExecuteStaticCodeChecksOptions, telemetryData *telemetry.CustomData, command command.ExecRunner) error {
|
2020-03-09 09:58:59 +02:00
|
|
|
var defines []string
|
|
|
|
var goals []string
|
|
|
|
|
|
|
|
if !config.SpotBugs && !config.Pmd {
|
2020-09-11 16:16:00 +02:00
|
|
|
log.Entry().Warnf("Neither SpotBugs nor Pmd are configured. Skipping step execution")
|
|
|
|
return nil
|
2020-03-09 09:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if testModulesExcludes := maven.GetTestModulesExcludes(); testModulesExcludes != nil {
|
|
|
|
defines = append(defines, testModulesExcludes...)
|
|
|
|
}
|
|
|
|
if config.MavenModulesExcludes != nil {
|
|
|
|
for _, module := range config.MavenModulesExcludes {
|
|
|
|
defines = append(defines, "-pl")
|
|
|
|
defines = append(defines, "!"+module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.SpotBugs {
|
|
|
|
spotBugsMavenParameters := getSpotBugsMavenParameters(config)
|
|
|
|
defines = append(defines, spotBugsMavenParameters.Defines...)
|
|
|
|
goals = append(goals, spotBugsMavenParameters.Goals...)
|
|
|
|
}
|
|
|
|
if config.Pmd {
|
|
|
|
pmdMavenParameters := getPmdMavenParameters(config)
|
|
|
|
defines = append(defines, pmdMavenParameters.Defines...)
|
|
|
|
goals = append(goals, pmdMavenParameters.Goals...)
|
|
|
|
}
|
|
|
|
finalMavenOptions := maven.ExecuteOptions{
|
2020-03-17 13:09:08 +02:00
|
|
|
Goals: goals,
|
|
|
|
Defines: defines,
|
|
|
|
ProjectSettingsFile: config.ProjectSettingsFile,
|
|
|
|
GlobalSettingsFile: config.GlobalSettingsFile,
|
|
|
|
M2Path: config.M2Path,
|
|
|
|
LogSuccessfulMavenTransfers: config.LogSuccessfulMavenTransfers,
|
2020-03-09 09:58:59 +02:00
|
|
|
}
|
|
|
|
_, err := maven.Execute(&finalMavenOptions, command)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:45:57 +02:00
|
|
|
func getSpotBugsMavenParameters(config *mavenExecuteStaticCodeChecksOptions) *maven.ExecuteOptions {
|
2020-03-09 09:58:59 +02:00
|
|
|
var defines []string
|
|
|
|
if config.SpotBugsIncludeFilterFile != "" {
|
|
|
|
defines = append(defines, "-Dspotbugs.includeFilterFile="+config.SpotBugsIncludeFilterFile)
|
|
|
|
}
|
|
|
|
if config.SpotBugsExcludeFilterFile != "" {
|
|
|
|
defines = append(defines, "-Dspotbugs.excludeFilterFile="+config.SpotBugsExcludeFilterFile)
|
|
|
|
}
|
2020-03-30 15:59:59 +02:00
|
|
|
if config.SpotBugsMaxAllowedViolations != 0 {
|
|
|
|
defines = append(defines, "-Dspotbugs.maxAllowedViolations="+strconv.Itoa(config.SpotBugsMaxAllowedViolations))
|
|
|
|
}
|
2020-03-09 09:58:59 +02:00
|
|
|
|
|
|
|
mavenOptions := maven.ExecuteOptions{
|
2020-03-30 15:59:59 +02:00
|
|
|
// check goal executes spotbugs goal first and fails the build if any bugs were found
|
2020-07-28 09:58:56 +02:00
|
|
|
Goals: []string{"com.github.spotbugs:spotbugs-maven-plugin:4.0.4:check"},
|
2020-03-09 09:58:59 +02:00
|
|
|
Defines: defines,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mavenOptions
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:45:57 +02:00
|
|
|
func getPmdMavenParameters(config *mavenExecuteStaticCodeChecksOptions) *maven.ExecuteOptions {
|
2020-03-09 09:58:59 +02:00
|
|
|
var defines []string
|
2020-03-30 15:59:59 +02:00
|
|
|
if config.PmdMaxAllowedViolations != 0 {
|
|
|
|
defines = append(defines, "-Dpmd.maxAllowedViolations="+strconv.Itoa(config.PmdMaxAllowedViolations))
|
2020-03-09 09:58:59 +02:00
|
|
|
}
|
2020-03-30 15:59:59 +02:00
|
|
|
if config.PmdFailurePriority >= 1 && config.PmdFailurePriority <= 5 {
|
|
|
|
defines = append(defines, "-Dpmd.failurePriority="+strconv.Itoa(config.PmdFailurePriority))
|
|
|
|
} else if config.PmdFailurePriority != 0 {
|
|
|
|
log.Entry().Warningf("Pmd failure priority must be a value between 1 and 5. %v was configured. Defaulting to 5.", config.PmdFailurePriority)
|
2020-03-09 09:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mavenOptions := maven.ExecuteOptions{
|
2020-03-30 15:59:59 +02:00
|
|
|
// check goal executes pmd goal first and fails the build if any violations were found
|
|
|
|
Goals: []string{"org.apache.maven.plugins:maven-pmd-plugin:3.13.0:check"},
|
2020-03-09 09:58:59 +02:00
|
|
|
Defines: defines,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mavenOptions
|
|
|
|
}
|