diff --git a/cmd/mavenBuild.go b/cmd/mavenBuild.go index 6080a3e2b3..0bce878050 100644 --- a/cmd/mavenBuild.go +++ b/cmd/mavenBuild.go @@ -192,12 +192,12 @@ func runMavenBuild(config *mavenBuildOptions, _ *telemetry.CustomData, utils mav mavenOptions.ProjectSettingsFile = projectSettingsFilePath } - deployFlags := []string{} + var 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) + deployFlags = append(deployFlags, "-DaltDeploymentRepository="+config.AltDeploymentRepositoryID+"::"+config.AltDeploymentRepositoryURL) } downloadClient := &piperhttp.Client{} diff --git a/cmd/mavenBuild_generated.go b/cmd/mavenBuild_generated.go index 489cca007f..2cf3edb1ee 100644 --- a/cmd/mavenBuild_generated.go +++ b/cmd/mavenBuild_generated.go @@ -301,7 +301,7 @@ func addMavenBuildFlags(cmd *cobra.Command, stepConfig *mavenBuildOptions) { cmd.Flags().StringVar(&stepConfig.AltDeploymentRepositoryPassword, "altDeploymentRepositoryPassword", os.Getenv("PIPER_altDeploymentRepositoryPassword"), "Password for the alternative deployment repository to which the project artifacts should be deployed ( other than those specified in ). This password will be updated in settings.xml . When no settings.xml is provided a new one is created corresponding with tag") cmd.Flags().StringVar(&stepConfig.AltDeploymentRepositoryUser, "altDeploymentRepositoryUser", os.Getenv("PIPER_altDeploymentRepositoryUser"), "User for the alternative deployment repository to which the project artifacts should be deployed ( other than those specified in ). This user will be updated in settings.xml . When no settings.xml is provided a new one is created corresponding with tag") cmd.Flags().StringVar(&stepConfig.AltDeploymentRepositoryURL, "altDeploymentRepositoryUrl", os.Getenv("PIPER_altDeploymentRepositoryUrl"), "Url for the alternative deployment repository to which the project artifacts should be deployed ( other than those specified in ). This Url will be updated in settings.xml . When no settings.xml is provided a new one is created corresponding with tag") - cmd.Flags().StringVar(&stepConfig.AltDeploymentRepositoryID, "altDeploymentRepositoryID", os.Getenv("PIPER_altDeploymentRepositoryID"), "Id for the alternative deployment repository to which the project artifacts should be deployed ( other than those specified in ). This id will be updated in settings.xml and will be used as a flag with DaltDeploymentRepository along with mavenAltDeploymentRepositoryUrl during maven deploy . When no settings.xml is provided a new one is created corresponding with tag") + cmd.Flags().StringVar(&stepConfig.AltDeploymentRepositoryID, "altDeploymentRepositoryID", os.Getenv("PIPER_altDeploymentRepositoryID"), "Id for the alternative deployment repository to which the project artifacts should be deployed ( other than those specified in ). This id will be updated in settings.xml and will be used as a flag with DaltDeploymentRepository along with mavenAltDeploymentRepositoryUrl during maven deploy . When no settings.xml is provided a new one is created corresponding with tag. Note: Piper emits the flag in Maven 3.x format (-DaltDeploymentRepository=id::url); maven-deploy-plugin 3.0.0 or newer is required. The legacy three-token id::layout::url form is no longer used.") cmd.Flags().StringSliceVar(&stepConfig.CustomTLSCertificateLinks, "customTlsCertificateLinks", []string{}, "List of download links to custom TLS certificates. This is required to ensure trusted connections to instances with repositories (like nexus) when publish flag is set to true.") cmd.Flags().BoolVar(&stepConfig.Publish, "publish", false, "Configures maven to run the deploy plugin to publish artifacts to a repository.") cmd.Flags().StringVar(&stepConfig.JavaCaCertFilePath, "javaCaCertFilePath", os.Getenv("PIPER_javaCaCertFilePath"), "path to the cacerts file used by Java. When maven publish is set to True and customTlsCertificateLinks (to deploy the artifact to a repository with a self signed cert) are provided to trust the self signed certs, Piper will extend the existing Java cacerts to include the new self signed certs. if not provided Piper will search for the cacerts in $JAVA_HOME/jre/lib/security/cacerts") diff --git a/cmd/mavenBuild_test.go b/cmd/mavenBuild_test.go index 0ad686891b..eb8d3cdca4 100644 --- a/cmd/mavenBuild_test.go +++ b/cmd/mavenBuild_test.go @@ -104,7 +104,46 @@ func TestMavenBuild(t *testing.T) { 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") + assert.Contains(t, mockedUtils.Calls[1].Params, "-DaltDeploymentRepository=ID::http://sampleRepo.com") + } + }) + + t.Run("mavenBuild with deploy must not set altDeploymentRepository when URL is missing", func(t *testing.T) { + mockedUtils := newMavenMockUtils() + + options := mavenBuildOptions{Publish: true, Verify: false, AltDeploymentRepositoryID: "ID"} + + err := runMavenBuild(&options, nil, &mockedUtils, &cpe) + + assert.Nil(t, err) + if assert.Equal(t, 2, len(mockedUtils.Calls), "Expected two Maven invocations (main and deploy)") { + assert.NotContains(t, mockedUtils.Calls[1].Params, "-DaltDeploymentRepository=ID::") + } + }) + + t.Run("mavenBuild with deploy must not set altDeploymentRepository when ID is missing", func(t *testing.T) { + mockedUtils := newMavenMockUtils() + + options := mavenBuildOptions{Publish: true, Verify: false, AltDeploymentRepositoryURL: "http://sampleRepo.com"} + + err := runMavenBuild(&options, nil, &mockedUtils, &cpe) + + assert.Nil(t, err) + if assert.Equal(t, 2, len(mockedUtils.Calls), "Expected two Maven invocations (main and deploy)") { + assert.NotContains(t, mockedUtils.Calls[1].Params, "-DaltDeploymentRepository=::http://sampleRepo.com") + } + }) + + t.Run("mavenBuild must not set altDeploymentRepository when Publish is false", func(t *testing.T) { + mockedUtils := newMavenMockUtils() + + options := mavenBuildOptions{Publish: false, AltDeploymentRepositoryID: "ID", AltDeploymentRepositoryURL: "http://sampleRepo.com"} + + err := runMavenBuild(&options, nil, &mockedUtils, &cpe) + + assert.Nil(t, err) + if assert.Equal(t, 1, len(mockedUtils.Calls), "Expected one Maven invocation (no deploy when Publish is false)") { + assert.NotContains(t, mockedUtils.Calls[0].Params, "-DaltDeploymentRepository=ID::http://sampleRepo.com") } }) diff --git a/resources/metadata/mavenBuild.yaml b/resources/metadata/mavenBuild.yaml index e30d3d4821..90120654ea 100644 --- a/resources/metadata/mavenBuild.yaml +++ b/resources/metadata/mavenBuild.yaml @@ -194,7 +194,7 @@ spec: param: custom/repositoryUrl - name: altDeploymentRepositoryID type: string - description: Id for the alternative deployment repository to which the project artifacts should be deployed ( other than those specified in ). This id will be updated in settings.xml and will be used as a flag with DaltDeploymentRepository along with mavenAltDeploymentRepositoryUrl during maven deploy . When no settings.xml is provided a new one is created corresponding with tag + description: "Id for the alternative deployment repository to which the project artifacts should be deployed ( other than those specified in ). This id will be updated in settings.xml and will be used as a flag with DaltDeploymentRepository along with mavenAltDeploymentRepositoryUrl during maven deploy . When no settings.xml is provided a new one is created corresponding with tag. Note: Piper emits the flag in Maven 3.x format (-DaltDeploymentRepository=id::url); maven-deploy-plugin 3.0.0 or newer is required. The legacy three-token id::layout::url form is no longer used." scope: - GENERAL - PARAMETERS