1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

Add Evaluate() method to maven package (#1268)

Useful for evaluating properties from pom files using the Maven evaluate plugin.
This commit is contained in:
Stephan Aßmus 2020-03-13 14:54:49 +01:00 committed by GitHub
parent a923e4cfc2
commit e0c789a791
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package maven
import (
"bytes"
"fmt"
"io"
"strings"
@ -10,6 +11,7 @@ import (
"github.com/SAP/jenkins-library/pkg/piperutils"
)
// ExecuteOptions are used by Execute() to construct the Maven command line.
type ExecuteOptions struct {
PomPath string `json:"pomPath,omitempty"`
ProjectSettingsFile string `json:"projectSettingsFile,omitempty"`
@ -30,6 +32,8 @@ type mavenExecRunner interface {
const mavenExecutable = "mvn"
// Execute constructs a mvn command line from the given options, and uses the provided
// mavenExecRunner to execute it.
func Execute(options *ExecuteOptions, command mavenExecRunner) (string, error) {
stdOutBuf, stdOut := evaluateStdOut(options)
command.Stdout(stdOut)
@ -51,6 +55,27 @@ func Execute(options *ExecuteOptions, command mavenExecRunner) (string, error) {
return string(stdOutBuf.Bytes()), nil
}
// Evaluate constructs ExecuteOptions for using the maven-help-plugin's 'evaluate' goal to
// evaluate a given expression from a pom file. This allows to retrieve the value of - for
// example - 'project.version' from a pom file exactly as Maven itself evaluates it.
func Evaluate(pomFile, expression string, command mavenExecRunner) (string, error) {
expressionDefine := "-Dexpression=" + expression
options := ExecuteOptions{
PomPath: pomFile,
Goals: []string{"org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"},
Defines: []string{expressionDefine, "-DforceStdout", "-q"},
ReturnStdout: true,
}
value, err := Execute(&options, command)
if err != nil {
return "", err
}
if strings.HasPrefix(value, "null object or invalid expression") {
return "", fmt.Errorf("expression '%s' in file '%s' could not be resolved", expression, pomFile)
}
return value, nil
}
func evaluateStdOut(config *ExecuteOptions) (*bytes.Buffer, io.Writer) {
var stdOutBuf *bytes.Buffer
var stdOut io.Writer

View File

@ -65,6 +65,27 @@ func TestExecute(t *testing.T) {
})
}
func TestEvaluate(t *testing.T) {
t.Run("should evaluate expression", func(t *testing.T) {
execMockRunner := mock.ExecMockRunner{}
execMockRunner.StdoutReturn = map[string]string{"mvn --file pom.xml -Dexpression=project.groupId -DforceStdout -q --batch-mode org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate": "com.awesome"}
result, err := Evaluate("pom.xml", "project.groupId", &execMockRunner)
if assert.NoError(t, err) {
assert.Equal(t, "com.awesome", result)
}
})
t.Run("should not evaluate expression", func(t *testing.T) {
execMockRunner := mock.ExecMockRunner{}
execMockRunner.StdoutReturn = map[string]string{"mvn --file pom.xml -Dexpression=project.groupId -DforceStdout -q --batch-mode org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate": "null object or invalid expression"}
result, err := Evaluate("pom.xml", "project.groupId", &execMockRunner)
if assert.EqualError(t, err, "expression 'project.groupId' in file 'pom.xml' could not be resolved") {
assert.Equal(t, "", result)
}
})
}
type mockDownloader struct {
shouldFail bool
}