mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-11-28 08:49:44 +02:00
Fix maven build failures due to makeBom goal (#5134)
This commit is contained in:
parent
f6231de55b
commit
c991c5b16d
@ -22,7 +22,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
mvnBomFilename = "bom-maven"
|
||||
mvnBomFilename = "bom-maven"
|
||||
mvnSimpleBomFilename = "bom-simple"
|
||||
)
|
||||
|
||||
func mavenBuild(config mavenBuildOptions, telemetryData *telemetry.CustomData, commonPipelineEnvironment *mavenBuildCommonPipelineEnvironment) {
|
||||
@ -39,6 +40,53 @@ func mavenBuild(config mavenBuildOptions, telemetryData *telemetry.CustomData, c
|
||||
}
|
||||
}
|
||||
|
||||
func executeMavenGoals(config *mavenBuildOptions, utils maven.Utils, flags []string, goals []string, defines []string) error {
|
||||
mavenOptions := maven.ExecuteOptions{
|
||||
Flags: flags,
|
||||
Goals: goals,
|
||||
Defines: defines,
|
||||
PomPath: config.PomPath,
|
||||
ProjectSettingsFile: config.ProjectSettingsFile,
|
||||
GlobalSettingsFile: config.GlobalSettingsFile,
|
||||
M2Path: config.M2Path,
|
||||
LogSuccessfulMavenTransfers: config.LogSuccessfulMavenTransfers,
|
||||
}
|
||||
|
||||
_, err := maven.Execute(&mavenOptions, utils)
|
||||
return err
|
||||
}
|
||||
|
||||
func runMakeBOMGoal(config *mavenBuildOptions, utils maven.Utils) error {
|
||||
var flags = []string{"-update-snapshots", "--batch-mode"}
|
||||
if len(config.Profiles) > 0 {
|
||||
flags = append(flags, "--activate-profiles", strings.Join(config.Profiles, ","))
|
||||
}
|
||||
exists, _ := utils.FileExists("integration-tests/pom.xml")
|
||||
if exists {
|
||||
flags = append(flags, "-pl", "!integration-tests")
|
||||
}
|
||||
|
||||
var defines []string
|
||||
|
||||
createBOMConfig := []string{
|
||||
"-DschemaVersion=1.4",
|
||||
"-DincludeBomSerialNumber=true",
|
||||
"-DincludeCompileScope=true",
|
||||
"-DincludeProvidedScope=true",
|
||||
"-DincludeRuntimeScope=true",
|
||||
"-DincludeSystemScope=true",
|
||||
"-DincludeTestScope=false",
|
||||
"-DincludeLicenseText=false",
|
||||
"-DoutputFormat=xml",
|
||||
"-DoutputName=" + mvnSimpleBomFilename,
|
||||
}
|
||||
defines = append(defines, createBOMConfig...)
|
||||
|
||||
goals := []string{"org.cyclonedx:cyclonedx-maven-plugin:2.7.8:makeBom"}
|
||||
|
||||
return executeMavenGoals(config, utils, flags, goals, defines)
|
||||
}
|
||||
|
||||
func runMavenBuild(config *mavenBuildOptions, telemetryData *telemetry.CustomData, utils maven.Utils, commonPipelineEnvironment *mavenBuildCommonPipelineEnvironment) error {
|
||||
|
||||
var flags = []string{"-update-snapshots", "--batch-mode"}
|
||||
@ -61,7 +109,13 @@ func runMavenBuild(config *mavenBuildOptions, telemetryData *telemetry.CustomDat
|
||||
}
|
||||
|
||||
if config.CreateBOM {
|
||||
goals = append(goals, "org.cyclonedx:cyclonedx-maven-plugin:2.7.8:makeBom", "org.cyclonedx:cyclonedx-maven-plugin:2.7.8:makeAggregateBom")
|
||||
// Separate run for makeBOM goal
|
||||
if err := runMakeBOMGoal(config, utils); err != nil {
|
||||
return errors.Wrap(err, "failed to execute makeBOM goal")
|
||||
}
|
||||
|
||||
// Append the makeAggregateBOM goal to the rest of the goals
|
||||
goals = append(goals, "org.cyclonedx:cyclonedx-maven-plugin:2.7.8:makeAggregateBom")
|
||||
createBOMConfig := []string{
|
||||
"-DschemaVersion=1.4",
|
||||
"-DincludeBomSerialNumber=true",
|
||||
@ -85,20 +139,7 @@ func runMavenBuild(config *mavenBuildOptions, telemetryData *telemetry.CustomDat
|
||||
goals = append(goals, "install")
|
||||
}
|
||||
|
||||
mavenOptions := maven.ExecuteOptions{
|
||||
Flags: flags,
|
||||
Goals: goals,
|
||||
Defines: defines,
|
||||
PomPath: config.PomPath,
|
||||
ProjectSettingsFile: config.ProjectSettingsFile,
|
||||
GlobalSettingsFile: config.GlobalSettingsFile,
|
||||
M2Path: config.M2Path,
|
||||
LogSuccessfulMavenTransfers: config.LogSuccessfulMavenTransfers,
|
||||
}
|
||||
|
||||
_, err := maven.Execute(&mavenOptions, utils)
|
||||
|
||||
if err != nil {
|
||||
if err := executeMavenGoals(config, utils, flags, goals, defines); err != nil {
|
||||
return errors.Wrapf(err, "failed to execute maven build for goal(s) '%v'", goals)
|
||||
}
|
||||
|
||||
@ -133,84 +174,96 @@ func runMavenBuild(config *mavenBuildOptions, telemetryData *telemetry.CustomDat
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Could not create or update project settings xml")
|
||||
}
|
||||
mavenOptions.ProjectSettingsFile = projectSettingsFilePath
|
||||
}
|
||||
|
||||
deployFlags := []string{}
|
||||
if len(config.DeployFlags) > 0 {
|
||||
deployFlags = append(deployFlags, config.DeployFlags...)
|
||||
}
|
||||
if (len(config.AltDeploymentRepositoryID) > 0) && (len(config.AltDeploymentRepositoryURL) > 0) {
|
||||
deployFlags = append(deployFlags, "-DaltDeploymentRepository="+config.AltDeploymentRepositoryID+"::default::"+config.AltDeploymentRepositoryURL)
|
||||
}
|
||||
|
||||
downloadClient := &piperhttp.Client{}
|
||||
downloadClient.SetOptions(piperhttp.ClientOptions{})
|
||||
runner := &command.Command{
|
||||
StepName: "mavenBuild",
|
||||
}
|
||||
fileUtils := &piperutils.Files{}
|
||||
if len(config.CustomTLSCertificateLinks) > 0 {
|
||||
if err := loadRemoteRepoCertificates(config.CustomTLSCertificateLinks, downloadClient, &deployFlags, runner, fileUtils, config.JavaCaCertFilePath); err != nil {
|
||||
log.SetErrorCategory(log.ErrorInfrastructure)
|
||||
return err
|
||||
mavenOptions := maven.ExecuteOptions{
|
||||
ProjectSettingsFile: projectSettingsFilePath,
|
||||
}
|
||||
}
|
||||
|
||||
mavenOptions.Flags = deployFlags
|
||||
mavenOptions.Goals = []string{"deploy"}
|
||||
mavenOptions.Defines = []string{}
|
||||
_, err := maven.Execute(&mavenOptions, utils)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if config.CreateBuildArtifactsMetadata {
|
||||
buildCoordinates := []versioning.Coordinates{}
|
||||
options := versioning.Options{}
|
||||
var utils versioning.Utils
|
||||
deployFlags := []string{}
|
||||
if len(config.DeployFlags) > 0 {
|
||||
deployFlags = append(deployFlags, config.DeployFlags...)
|
||||
}
|
||||
if (len(config.AltDeploymentRepositoryID) > 0) && (len(config.AltDeploymentRepositoryURL) > 0) {
|
||||
deployFlags = append(deployFlags, "-DaltDeploymentRepository="+config.AltDeploymentRepositoryID+"::default::"+config.AltDeploymentRepositoryURL)
|
||||
}
|
||||
|
||||
matches, _ := fileUtils.Glob("**/pom.xml")
|
||||
for _, match := range matches {
|
||||
downloadClient := &piperhttp.Client{}
|
||||
downloadClient.SetOptions(piperhttp.ClientOptions{})
|
||||
|
||||
artifact, err := versioning.GetArtifact("maven", match, &options, utils)
|
||||
if err != nil {
|
||||
log.Entry().Warnf("unable to get artifact metdata : %v", err)
|
||||
} else {
|
||||
coordinate, err := artifact.GetCoordinates()
|
||||
if err != nil {
|
||||
log.Entry().Warnf("unable to get artifact coordinates : %v", err)
|
||||
} else {
|
||||
coordinate.BuildPath = filepath.Dir(match)
|
||||
coordinate.URL = config.AltDeploymentRepositoryURL
|
||||
coordinate.PURL = getPurlForThePomAndDeleteIndividualBom(match)
|
||||
buildCoordinates = append(buildCoordinates, coordinate)
|
||||
}
|
||||
runner := &command.Command{
|
||||
StepName: "mavenBuild",
|
||||
}
|
||||
|
||||
fileUtils := &piperutils.Files{}
|
||||
if len(config.CustomTLSCertificateLinks) > 0 {
|
||||
if err := loadRemoteRepoCertificates(config.CustomTLSCertificateLinks, downloadClient, &deployFlags, runner, fileUtils, config.JavaCaCertFilePath); err != nil {
|
||||
log.SetErrorCategory(log.ErrorInfrastructure)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(buildCoordinates) == 0 {
|
||||
log.Entry().Warnf("unable to identify artifact coordinates for the maven packages published")
|
||||
return nil
|
||||
mavenOptions.Flags = deployFlags
|
||||
mavenOptions.Goals = []string{"deploy"}
|
||||
mavenOptions.Defines = []string{}
|
||||
_, err = maven.Execute(&mavenOptions, utils)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var buildArtifacts build.BuildArtifacts
|
||||
|
||||
buildArtifacts.Coordinates = buildCoordinates
|
||||
jsonResult, _ := json.Marshal(buildArtifacts)
|
||||
commonPipelineEnvironment.custom.mavenBuildArtifacts = string(jsonResult)
|
||||
if config.CreateBuildArtifactsMetadata {
|
||||
err2, done := createBuildArtifactsMetadata(config, commonPipelineEnvironment)
|
||||
if done {
|
||||
return err2
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
} else {
|
||||
log.Entry().Infof("publish not detected, ignoring maven deploy")
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func getPurlForThePomAndDeleteIndividualBom(pomFilePath string) string {
|
||||
bomPath := filepath.Join(filepath.Dir(pomFilePath) + "/target/" + mvnBomFilename + ".xml")
|
||||
func createBuildArtifactsMetadata(config *mavenBuildOptions, commonPipelineEnvironment *mavenBuildCommonPipelineEnvironment) (error, bool) {
|
||||
fileUtils := &piperutils.Files{}
|
||||
buildCoordinates := []versioning.Coordinates{}
|
||||
options := versioning.Options{}
|
||||
var utils versioning.Utils
|
||||
|
||||
matches, _ := fileUtils.Glob("**/pom.xml")
|
||||
for _, match := range matches {
|
||||
|
||||
artifact, err := versioning.GetArtifact("maven", match, &options, utils)
|
||||
if err != nil {
|
||||
log.Entry().Warnf("unable to get artifact metdata : %v", err)
|
||||
} else {
|
||||
coordinate, err := artifact.GetCoordinates()
|
||||
if err != nil {
|
||||
log.Entry().Warnf("unable to get artifact coordinates : %v", err)
|
||||
} else {
|
||||
coordinate.BuildPath = filepath.Dir(match)
|
||||
coordinate.URL = config.AltDeploymentRepositoryURL
|
||||
coordinate.PURL = getPurlForThePom(match)
|
||||
buildCoordinates = append(buildCoordinates, coordinate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(buildCoordinates) == 0 {
|
||||
log.Entry().Warnf("unable to identify artifact coordinates for the maven packages published")
|
||||
return nil, true
|
||||
}
|
||||
|
||||
var buildArtifacts build.BuildArtifacts
|
||||
|
||||
buildArtifacts.Coordinates = buildCoordinates
|
||||
jsonResult, _ := json.Marshal(buildArtifacts)
|
||||
commonPipelineEnvironment.custom.mavenBuildArtifacts = string(jsonResult)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func getPurlForThePom(pomFilePath string) string {
|
||||
bomPath := filepath.Join(filepath.Dir(pomFilePath) + "/target/" + mvnSimpleBomFilename + ".xml")
|
||||
exists, _ := piperutils.FileExists(bomPath)
|
||||
if !exists {
|
||||
log.Entry().Debugf("bom file doesn't exist and hence no pURL info: %v", bomPath)
|
||||
@ -225,26 +278,9 @@ func getPurlForThePomAndDeleteIndividualBom(pomFilePath string) string {
|
||||
log.Entry().Debugf("Found purl: %s for the bomPath: %s", bom.Metadata.Component.Purl, bomPath)
|
||||
purl := bom.Metadata.Component.Purl
|
||||
|
||||
// Check if the BOM is an aggregated BOM
|
||||
if !isAggregatedBOM(bom) {
|
||||
// Delete the individual BOM file
|
||||
err = os.Remove(bomPath)
|
||||
if err != nil {
|
||||
log.Entry().Warnf("failed to delete bom file %s: %v", bomPath, err)
|
||||
}
|
||||
}
|
||||
return purl
|
||||
}
|
||||
|
||||
func isAggregatedBOM(bom piperutils.Bom) bool {
|
||||
for _, property := range bom.Metadata.Properties {
|
||||
if property.Name == "maven.goal" && property.Value == "makeAggregateBom" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func createOrUpdateProjectSettingsXML(projectSettingsFile string, altDeploymentRepositoryID string, altDeploymentRepositoryUser string, altDeploymentRepositoryPassword string, utils maven.Utils) (string, error) {
|
||||
if len(projectSettingsFile) > 0 {
|
||||
projectSettingsFilePath, err := maven.UpdateProjectSettingsXML(projectSettingsFile, altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
|
||||
|
@ -12,9 +12,9 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMavenBuild(t *testing.T) {
|
||||
var cpe mavenBuildCommonPipelineEnvironment
|
||||
|
||||
cpe := mavenBuildCommonPipelineEnvironment{}
|
||||
func TestMavenBuild(t *testing.T) {
|
||||
|
||||
t.Run("mavenBuild should install the artifact", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
@ -22,108 +22,13 @@ func TestMavenBuild(t *testing.T) {
|
||||
config := mavenBuildOptions{}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
expectedParams := []string{"install"}
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, mockedUtils.Calls[0].Exec, "mvn")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "install")
|
||||
})
|
||||
|
||||
t.Run("mavenBuild should skip integration tests", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
mockedUtils.AddFile("integration-tests/pom.xml", []byte{})
|
||||
|
||||
config := mavenBuildOptions{}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, mockedUtils.Calls[0].Exec, "mvn")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-pl", "!integration-tests")
|
||||
})
|
||||
|
||||
t.Run("mavenBuild should flatten", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
|
||||
config := mavenBuildOptions{Flatten: true}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "flatten:flatten")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-Dflatten.mode=resolveCiFriendliesOnly")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DupdatePomFile=true")
|
||||
})
|
||||
|
||||
t.Run("mavenBuild should run only verify", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
|
||||
config := mavenBuildOptions{Verify: true}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "verify")
|
||||
assert.NotContains(t, mockedUtils.Calls[0].Params, "install")
|
||||
})
|
||||
|
||||
t.Run("mavenBuild should createBOM", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
|
||||
config := mavenBuildOptions{CreateBOM: true}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "org.cyclonedx:cyclonedx-maven-plugin:2.7.8:makeAggregateBom")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DschemaVersion=1.4")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeBomSerialNumber=true")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeCompileScope=true")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeProvidedScope=true")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeRuntimeScope=true")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeSystemScope=true")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeTestScope=false")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeLicenseText=false")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DoutputFormat=xml")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "-DoutputName=bom-maven")
|
||||
})
|
||||
|
||||
t.Run("mavenBuild include install and deploy when publish is true", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
|
||||
config := mavenBuildOptions{Publish: true, Verify: false}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "install")
|
||||
assert.NotContains(t, mockedUtils.Calls[0].Params, "verify")
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "deploy")
|
||||
|
||||
})
|
||||
|
||||
t.Run("mavenBuild with deploy must skip build, install and test", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
|
||||
config := mavenBuildOptions{Publish: true, Verify: false, DeployFlags: []string{"-Dmaven.main.skip=true", "-Dmaven.test.skip=true", "-Dmaven.install.skip=true"}}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "-Dmaven.main.skip=true")
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "-Dmaven.test.skip=true")
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "-Dmaven.install.skip=true")
|
||||
|
||||
})
|
||||
|
||||
t.Run("mavenBuild with deploy must include alt repo id and url when passed as parameter", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
|
||||
config := mavenBuildOptions{Publish: true, Verify: false, AltDeploymentRepositoryID: "ID", AltDeploymentRepositoryURL: "http://sampleRepo.com"}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "-DaltDeploymentRepository=ID::default::http://sampleRepo.com")
|
||||
if assert.Equal(t, 1, len(mockedUtils.Calls), "Expected one maven invocation for the main build") {
|
||||
assert.Equal(t, "mvn", mockedUtils.Calls[0].Exec)
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, expectedParams[0], "Call should contain install goal")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("mavenBuild accepts profiles", func(t *testing.T) {
|
||||
@ -134,14 +39,74 @@ func TestMavenBuild(t *testing.T) {
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "--activate-profiles")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "profile1,profile2")
|
||||
if assert.Equal(t, 1, len(mockedUtils.Calls), "Expected one maven invocation for the main build") {
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "--activate-profiles")
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "profile1,profile2")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("mavenBuild should create BOM", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
|
||||
config := mavenBuildOptions{CreateBOM: true}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
if assert.Equal(t, 2, len(mockedUtils.Calls), "Expected two Maven invocations (default + makeAggregateBom)") {
|
||||
assert.Equal(t, "mvn", mockedUtils.Calls[1].Exec)
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "org.cyclonedx:cyclonedx-maven-plugin:2.7.8:makeAggregateBom")
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "-DoutputName=bom-maven")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("mavenBuild include install and deploy when publish is true", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
|
||||
config := mavenBuildOptions{Publish: true, Verify: false, AltDeploymentRepositoryID: "ID", AltDeploymentRepositoryURL: "http://sampleRepo.com", AltDeploymentRepositoryUser: "user", AltDeploymentRepositoryPassword: "pass"}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
if assert.Equal(t, 2, len(mockedUtils.Calls), "Expected two Maven invocations (main and deploy)") {
|
||||
assert.Contains(t, mockedUtils.Calls[0].Params, "install")
|
||||
assert.NotContains(t, mockedUtils.Calls[0].Params, "verify")
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "deploy")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("mavenBuild with deploy must skip build, install and test", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
|
||||
config := mavenBuildOptions{Publish: true, Verify: false, DeployFlags: []string{"-Dmaven.main.skip=true", "-Dmaven.test.skip=true", "-Dmaven.install.skip=true"}, AltDeploymentRepositoryID: "ID", AltDeploymentRepositoryURL: "http://sampleRepo.com", AltDeploymentRepositoryUser: "user", AltDeploymentRepositoryPassword: "pass"}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
if assert.Equal(t, 2, len(mockedUtils.Calls), "Expected two Maven invocations (main and deploy)") {
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "-Dmaven.main.skip=true")
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "-Dmaven.test.skip=true")
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "-Dmaven.install.skip=true")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("mavenBuild with deploy must include alt repo id and url when passed as parameter", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
|
||||
config := mavenBuildOptions{Publish: true, Verify: false, AltDeploymentRepositoryID: "ID", AltDeploymentRepositoryURL: "http://sampleRepo.com", AltDeploymentRepositoryUser: "user", AltDeploymentRepositoryPassword: "pass"}
|
||||
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
|
||||
assert.Nil(t, err)
|
||||
if assert.Equal(t, 2, len(mockedUtils.Calls), "Expected two Maven invocations (main and deploy)") {
|
||||
assert.Contains(t, mockedUtils.Calls[1].Params, "-DaltDeploymentRepository=ID::default::http://sampleRepo.com")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("mavenBuild should not create build artifacts metadata when CreateBuildArtifactsMetadata is false and Publish is true", func(t *testing.T) {
|
||||
mockedUtils := newMavenMockUtils()
|
||||
mockedUtils.AddFile("pom.xml", []byte{})
|
||||
config := mavenBuildOptions{CreateBuildArtifactsMetadata: false, Publish: true}
|
||||
config := mavenBuildOptions{CreateBuildArtifactsMetadata: false, Publish: true, AltDeploymentRepositoryID: "ID", AltDeploymentRepositoryURL: "http://sampleRepo.com", AltDeploymentRepositoryUser: "user", AltDeploymentRepositoryPassword: "pass"}
|
||||
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, mockedUtils.Calls[0].Exec, "mvn")
|
||||
@ -161,30 +126,6 @@ func TestMavenBuild(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestIsAggregatedBOM(t *testing.T) {
|
||||
t.Run("is aggregated BOM", func(t *testing.T) {
|
||||
bom := piperutils.Bom{
|
||||
Metadata: piperutils.Metadata{
|
||||
Properties: []piperutils.BomProperty{
|
||||
{Name: "maven.goal", Value: "makeAggregateBom"},
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.True(t, isAggregatedBOM(bom))
|
||||
})
|
||||
|
||||
t.Run("is not aggregated BOM", func(t *testing.T) {
|
||||
bom := piperutils.Bom{
|
||||
Metadata: piperutils.Metadata{
|
||||
Properties: []piperutils.BomProperty{
|
||||
{Name: "some.property", Value: "someValue"},
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.False(t, isAggregatedBOM(bom))
|
||||
})
|
||||
}
|
||||
|
||||
func createTempFile(t *testing.T, dir string, filename string, content string) string {
|
||||
filePath := filepath.Join(dir, filename)
|
||||
err := os.WriteFile(filePath, []byte(content), 0666)
|
||||
@ -195,35 +136,6 @@ func createTempFile(t *testing.T, dir string, filename string, content string) s
|
||||
}
|
||||
|
||||
func TestGetPurlForThePomAndDeleteIndividualBom(t *testing.T) {
|
||||
t.Run("valid BOM file, non-aggregated", func(t *testing.T) {
|
||||
tempDir, err := piperutils.Files{}.TempDir("", "test")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp directory: %s", err)
|
||||
}
|
||||
|
||||
bomContent := `<bom>
|
||||
<metadata>
|
||||
<component>
|
||||
<purl>pkg:maven/com.example/mycomponent@1.0.0</purl>
|
||||
</component>
|
||||
<properties>
|
||||
<property name="name1" value="value1" />
|
||||
</properties>
|
||||
</metadata>
|
||||
</bom>`
|
||||
pomFilePath := createTempFile(t, tempDir, "pom.xml", "")
|
||||
bomDir := filepath.Join(tempDir, "target")
|
||||
if err := os.MkdirAll(bomDir, 0777); err != nil {
|
||||
t.Fatalf("Failed to create temp directory: %s", err)
|
||||
}
|
||||
bomFilePath := createTempFile(t, bomDir, mvnBomFilename+".xml", bomContent)
|
||||
defer os.Remove(bomFilePath)
|
||||
|
||||
purl := getPurlForThePomAndDeleteIndividualBom(pomFilePath)
|
||||
assert.Equal(t, "pkg:maven/com.example/mycomponent@1.0.0", purl)
|
||||
_, err = os.Stat(bomFilePath)
|
||||
assert.True(t, os.IsNotExist(err))
|
||||
})
|
||||
|
||||
t.Run("valid BOM file, aggregated BOM", func(t *testing.T) {
|
||||
tempDir, err := piperutils.Files{}.TempDir("", "test")
|
||||
@ -246,9 +158,9 @@ func TestGetPurlForThePomAndDeleteIndividualBom(t *testing.T) {
|
||||
if err := os.MkdirAll(bomDir, 0777); err != nil {
|
||||
t.Fatalf("Failed to create temp directory: %s", err)
|
||||
}
|
||||
bomFilePath := createTempFile(t, bomDir, mvnBomFilename+".xml", bomContent)
|
||||
bomFilePath := createTempFile(t, bomDir, mvnSimpleBomFilename+".xml", bomContent)
|
||||
|
||||
purl := getPurlForThePomAndDeleteIndividualBom(pomFilePath)
|
||||
purl := getPurlForThePom(pomFilePath)
|
||||
assert.Equal(t, "pkg:maven/com.example/aggregatecomponent@1.0.0", purl)
|
||||
_, err = os.Stat(bomFilePath)
|
||||
assert.False(t, os.IsNotExist(err)) // File should not be deleted
|
||||
@ -258,7 +170,7 @@ func TestGetPurlForThePomAndDeleteIndividualBom(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
pomFilePath := createTempFile(t, tempDir, "pom.xml", "") // Create a temp pom file
|
||||
|
||||
purl := getPurlForThePomAndDeleteIndividualBom(pomFilePath)
|
||||
purl := getPurlForThePom(pomFilePath)
|
||||
assert.Equal(t, "", purl)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user