Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Gradle system properties with spaces #219

Merged
merged 11 commits into from
Dec 20, 2023
23 changes: 19 additions & 4 deletions build/gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
gradleExtractorRemotePath = "org/jfrog/buildinfo/build-info-extractor-gradle/%s"
gradleExtractor4DependencyVersion = "4.33.7"
gradleExtractor5DependencyVersion = "5.1.12"
projectPropertiesFlag = "-P"
systemPropertiesFlag = "-D"
)

var versionRegex = regexp.MustCompile(`Gradle (\d+\.\d+(?:\.\d+|-\w+-\d+)?)`)
Expand Down Expand Up @@ -140,7 +142,7 @@ func (gm *GradleModule) downloadGradleExtractor(gradleExecPath string) (err erro
func (gm *GradleModule) getExtractorVersionAndInitScript(gradleExecPath string) (string, string, error) {
gradleRunConfig := &gradleRunConfig{
gradle: gradleExecPath,
tasks: "--version",
tasks: []string{"--version"},
logger: gm.containingBuild.logger,
}

Expand Down Expand Up @@ -187,7 +189,7 @@ func (gm *GradleModule) createGradleRunConfig(gradleExecPath string) (*gradleRun
env: gm.gradleExtractorDetails.props,
gradle: gradleExecPath,
extractorPropsFile: extractorPropsFile,
tasks: strings.Join(gm.gradleExtractorDetails.tasks, " "),
tasks: gm.gradleExtractorDetails.tasks,
initScript: gm.gradleExtractorDetails.initScript,
logger: gm.containingBuild.logger,
}, nil
Expand Down Expand Up @@ -243,7 +245,7 @@ func GetGradleExecPath(useWrapper bool) (string, error) {
type gradleRunConfig struct {
gradle string
extractorPropsFile string
tasks string
tasks []string
initScript string
env map[string]string
logger utils.Log
Expand All @@ -255,11 +257,24 @@ func (config *gradleRunConfig) GetCmd() *exec.Cmd {
if config.initScript != "" {
cmd = append(cmd, "--init-script", config.initScript)
}
cmd = append(cmd, strings.Split(config.tasks, " ")...)
cmd = append(cmd, handleGradleCommandProperties(config.tasks)...)
config.logger.Info("Running gradle command:", strings.Join(cmd, " "))
return exec.Command(cmd[0], cmd[1:]...)
}

func handleGradleCommandProperties(tasks []string) []string {
var cmdArgs []string
for _, task := range tasks {
if !strings.HasPrefix(task, systemPropertiesFlag) && !strings.HasPrefix(task, projectPropertiesFlag) {
cmdArgs = append(cmdArgs, task)
continue
}
propertyParts := strings.SplitN(task, "=", 2)
cmdArgs = append(cmdArgs, fmt.Sprintf(`%s="%s"`, propertyParts[0], propertyParts[1]))
}
return cmdArgs
}

func (config *gradleRunConfig) runCmd(stdout, stderr io.Writer) error {
command := config.GetCmd()
command.Env = os.Environ()
Expand Down
29 changes: 29 additions & 0 deletions build/gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,32 @@ func TestParseGradleVersion(t *testing.T) {
})
}
}

func TestHandleGradleCommandProperties(t *testing.T) {
tests := []struct {
input []string
expected []string
}{
{
input: []string{"clean", "-Dparam=value", "build", "-Pkey=value"},
expected: []string{"clean", `-Dparam="value"`, "build", `-Pkey="value"`},
},
{
input: []string{"-Dprop1=value1", "test", "-Dprop2=value2"},
expected: []string{`-Dprop1="value1"`, "test", `-Dprop2="value2"`},
},
{
input: []string{"-Dparam1=value1", "-Pkey1=value1", "-Dparam2=value2", "-Pkey2=value2"},
expected: []string{`-Dparam1="value1"`, `-Pkey1="value1"`, `-Dparam2="value2"`, `-Pkey2="value2"`},
},
{
input: []string{"-Dparam1=value1", "run", "-Pkey2=value2", "-Dparam2=value2"},
expected: []string{`-Dparam1="value1"`, "run", `-Pkey2="value2"`, `-Dparam2="value2"`},
},
}

for _, test := range tests {
result := handleGradleCommandProperties(test.input)
assert.ElementsMatch(t, test.expected, result)
}
}
Loading