github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/versioning/maven.go (about) 1 package versioning 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/SAP/jenkins-library/pkg/maven" 8 9 "github.com/pkg/errors" 10 ) 11 12 type mavenExecRunner interface { 13 Stdout(out io.Writer) 14 Stderr(err io.Writer) 15 RunExecutable(e string, p ...string) error 16 } 17 18 type mavenRunner interface { 19 Execute(*maven.ExecuteOptions, maven.Utils) (string, error) 20 Evaluate(*maven.EvaluateOptions, string, maven.Utils) (string, error) 21 } 22 23 // Maven defines a maven artifact used for versioning 24 type Maven struct { 25 options maven.EvaluateOptions 26 runner mavenRunner 27 utils maven.Utils 28 } 29 30 func (m *Maven) init() { 31 if len(m.options.PomPath) == 0 { 32 m.options.PomPath = "pom.xml" 33 } 34 35 if m.utils == nil { 36 m.utils = maven.NewUtilsBundle() 37 } 38 } 39 40 // VersioningScheme returns the relevant versioning scheme 41 func (m *Maven) VersioningScheme() string { 42 return "maven" 43 } 44 45 // GetCoordinates reads the coordinates from the maven pom.xml descriptor file 46 func (m *Maven) GetCoordinates() (Coordinates, error) { 47 result := Coordinates{} 48 var err error 49 result.GroupID, err = m.GetGroupID() 50 if err != nil { 51 return result, err 52 } 53 result.ArtifactID, err = m.GetArtifactID() 54 if err != nil { 55 return result, err 56 } 57 result.Version, err = m.GetVersion() 58 if err != nil { 59 return result, err 60 } 61 result.Packaging, err = m.GetPackaging() 62 if err != nil { 63 return result, err 64 } 65 return result, nil 66 } 67 68 // GetPackaging returns the current ID of the Group 69 func (m *Maven) GetPackaging() (string, error) { 70 m.init() 71 72 packaging, err := m.runner.Evaluate(&m.options, "project.packaging", m.utils) 73 if err != nil { 74 return "", errors.Wrap(err, "Maven - getting packaging failed") 75 } 76 return packaging, nil 77 } 78 79 // GetGroupID returns the current ID of the Group 80 func (m *Maven) GetGroupID() (string, error) { 81 m.init() 82 83 groupID, err := m.runner.Evaluate(&m.options, "project.groupId", m.utils) 84 if err != nil { 85 return "", errors.Wrap(err, "Maven - getting groupId failed") 86 } 87 return groupID, nil 88 } 89 90 // GetArtifactID returns the current ID of the artifact 91 func (m *Maven) GetArtifactID() (string, error) { 92 m.init() 93 94 artifactID, err := m.runner.Evaluate(&m.options, "project.artifactId", m.utils) 95 if err != nil { 96 return "", errors.Wrap(err, "Maven - getting artifactId failed") 97 } 98 return artifactID, nil 99 } 100 101 // GetVersion returns the current version of the artifact 102 func (m *Maven) GetVersion() (string, error) { 103 m.init() 104 105 version, err := m.runner.Evaluate(&m.options, "project.version", m.utils) 106 if err != nil { 107 return "", errors.Wrap(err, "Maven - getting version failed") 108 } 109 //ToDo: how to deal with SNAPSHOT replacement? 110 return version, nil 111 } 112 113 // SetVersion updates the version of the artifact 114 func (m *Maven) SetVersion(version string) error { 115 m.init() 116 117 groupID, err := m.runner.Evaluate(&m.options, "project.groupId", m.utils) 118 if err != nil { 119 return errors.Wrap(err, "Maven - getting groupId failed") 120 } 121 opts := maven.ExecuteOptions{ 122 PomPath: m.options.PomPath, 123 ProjectSettingsFile: m.options.ProjectSettingsFile, 124 GlobalSettingsFile: m.options.GlobalSettingsFile, 125 M2Path: m.options.M2Path, 126 Goals: []string{"org.codehaus.mojo:versions-maven-plugin:2.7:set"}, 127 Defines: []string{ 128 fmt.Sprintf("-DnewVersion=%v", version), 129 fmt.Sprintf("-DgroupId=%v", groupID), 130 "-DartifactId=*", 131 "-DoldVersion=*", 132 "-DgenerateBackupPoms=false", 133 }, 134 } 135 _, err = m.runner.Execute(&opts, m.utils) 136 if err != nil { 137 return errors.Wrapf(err, "Maven - setting version %v failed", version) 138 } 139 return nil 140 }