2020-04-03 16:34:40 +02:00
|
|
|
package versioning
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/maven"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mavenExecRunner interface {
|
|
|
|
Stdout(out io.Writer)
|
|
|
|
Stderr(err io.Writer)
|
|
|
|
RunExecutable(e string, p ...string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type mavenRunner interface {
|
|
|
|
Execute(*maven.ExecuteOptions, mavenExecRunner) (string, error)
|
|
|
|
Evaluate(string, string, mavenExecRunner) (string, error)
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
// Maven defines a maven artifact used for versioning
|
2020-04-03 16:34:40 +02:00
|
|
|
type Maven struct {
|
2020-04-15 13:12:43 +02:00
|
|
|
pomPath string
|
|
|
|
runner mavenRunner
|
|
|
|
execRunner mavenExecRunner
|
|
|
|
projectSettingsFile string
|
|
|
|
globalSettingsFile string
|
|
|
|
m2Path string
|
2020-04-03 16:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Maven) init() {
|
2020-04-15 13:12:43 +02:00
|
|
|
if len(m.pomPath) == 0 {
|
|
|
|
m.pomPath = "pom.xml"
|
2020-04-03 16:34:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
if m.execRunner == nil {
|
|
|
|
m.execRunner = &command.Command{}
|
2020-04-03 16:34:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
// VersioningScheme returns the relevant versioning scheme
|
2020-04-03 16:34:40 +02:00
|
|
|
func (m *Maven) VersioningScheme() string {
|
|
|
|
return "maven"
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
// GetVersion returns the current version of the artifact
|
2020-04-03 16:34:40 +02:00
|
|
|
func (m *Maven) GetVersion() (string, error) {
|
|
|
|
m.init()
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
version, err := m.runner.Evaluate(m.pomPath, "project.version", m.execRunner)
|
2020-04-03 16:34:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Maven - getting version failed")
|
|
|
|
}
|
|
|
|
//ToDo: how to deal with SNAPSHOT replacement?
|
|
|
|
return version, nil
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
// SetVersion updates the version of the artifact
|
2020-04-03 16:34:40 +02:00
|
|
|
func (m *Maven) SetVersion(version string) error {
|
|
|
|
m.init()
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
groupID, err := m.runner.Evaluate(m.pomPath, "project.groupId", m.execRunner)
|
2020-04-03 16:34:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Maven - getting groupId failed")
|
|
|
|
}
|
|
|
|
opts := maven.ExecuteOptions{
|
2020-04-15 13:12:43 +02:00
|
|
|
PomPath: m.pomPath,
|
|
|
|
ProjectSettingsFile: m.projectSettingsFile,
|
|
|
|
GlobalSettingsFile: m.globalSettingsFile,
|
|
|
|
M2Path: m.m2Path,
|
2020-04-03 16:34:40 +02:00
|
|
|
Goals: []string{"org.codehaus.mojo:versions-maven-plugin:2.7:set"},
|
|
|
|
Defines: []string{
|
|
|
|
fmt.Sprintf("-DnewVersion=%v", version),
|
|
|
|
fmt.Sprintf("-DgroupId=%v", groupID),
|
|
|
|
"-DartifactId=*",
|
|
|
|
"-DoldVersion=*",
|
|
|
|
"-DgenerateBackupPoms=false",
|
|
|
|
},
|
|
|
|
}
|
2020-04-15 13:12:43 +02:00
|
|
|
_, err = m.runner.Execute(&opts, m.execRunner)
|
2020-04-03 16:34:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Maven - setting version %v failed", version)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|