2020-03-13 14:32:37 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-06-01 09:24:36 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-06-18 11:57:00 +02:00
|
|
|
"strings"
|
2021-06-01 09:24:36 +02:00
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
2020-03-13 14:32:37 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/maven"
|
2021-06-01 09:24:36 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
2020-03-13 14:32:37 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
2021-06-01 09:24:36 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
2020-03-13 14:32:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func mavenBuild(config mavenBuildOptions, telemetryData *telemetry.CustomData) {
|
2020-11-10 18:14:55 +02:00
|
|
|
utils := maven.NewUtilsBundle()
|
2020-03-13 14:32:37 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
err := runMavenBuild(&config, telemetryData, utils)
|
2020-03-13 14:32:37 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("step execution failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func runMavenBuild(config *mavenBuildOptions, telemetryData *telemetry.CustomData, utils maven.Utils) error {
|
2021-06-01 09:24:36 +02:00
|
|
|
|
2020-03-13 14:32:37 +02:00
|
|
|
var flags = []string{"-update-snapshots", "--batch-mode"}
|
|
|
|
|
2021-06-18 11:57:00 +02:00
|
|
|
if len(config.Profiles) > 0 {
|
|
|
|
flags = append(flags, "--activate-profiles", strings.Join(config.Profiles, ","))
|
|
|
|
}
|
|
|
|
|
2020-03-26 09:23:21 +02:00
|
|
|
exists, _ := utils.FileExists("integration-tests/pom.xml")
|
2020-03-13 14:32:37 +02:00
|
|
|
if exists {
|
|
|
|
flags = append(flags, "-pl", "!integration-tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
var defines []string
|
|
|
|
var goals []string
|
|
|
|
|
|
|
|
goals = append(goals, "org.jacoco:jacoco-maven-plugin:prepare-agent")
|
|
|
|
|
|
|
|
if config.Flatten {
|
|
|
|
goals = append(goals, "flatten:flatten")
|
|
|
|
defines = append(defines, "-Dflatten.mode=resolveCiFriendliesOnly", "-DupdatePomFile=true")
|
|
|
|
}
|
|
|
|
|
2021-02-25 16:01:19 +02:00
|
|
|
if config.CreateBOM {
|
|
|
|
goals = append(goals, "org.cyclonedx:cyclonedx-maven-plugin:makeAggregateBom")
|
|
|
|
createBOMConfig := []string{
|
|
|
|
"-DschemaVersion=1.2",
|
|
|
|
"-DincludeBomSerialNumber=true",
|
|
|
|
"-DincludeCompileScope=true",
|
|
|
|
"-DincludeProvidedScope=true",
|
|
|
|
"-DincludeRuntimeScope=true",
|
|
|
|
"-DincludeSystemScope=true",
|
|
|
|
"-DincludeTestScope=false",
|
|
|
|
"-DincludeLicenseText=false",
|
|
|
|
"-DoutputFormat=xml",
|
|
|
|
}
|
|
|
|
defines = append(defines, createBOMConfig...)
|
|
|
|
}
|
|
|
|
|
2020-03-13 14:32:37 +02:00
|
|
|
if config.Verify {
|
|
|
|
goals = append(goals, "verify")
|
|
|
|
} else {
|
|
|
|
goals = append(goals, "install")
|
|
|
|
}
|
|
|
|
|
|
|
|
mavenOptions := maven.ExecuteOptions{
|
|
|
|
Flags: flags,
|
|
|
|
Goals: goals,
|
|
|
|
Defines: defines,
|
|
|
|
PomPath: config.PomPath,
|
|
|
|
ProjectSettingsFile: config.ProjectSettingsFile,
|
|
|
|
GlobalSettingsFile: config.GlobalSettingsFile,
|
|
|
|
M2Path: config.M2Path,
|
|
|
|
LogSuccessfulMavenTransfers: config.LogSuccessfulMavenTransfers,
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
_, err := maven.Execute(&mavenOptions, utils)
|
2021-06-01 09:24:36 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if config.Publish && !config.Verify {
|
|
|
|
log.Entry().Infof("publish detected, running mvn deploy")
|
|
|
|
|
|
|
|
if (len(config.AltDeploymentRepositoryID) > 0) && (len(config.AltDeploymentRepositoryPassword) > 0) && (len(config.AltDeploymentRepositoryUser) > 0) {
|
|
|
|
projectSettingsFilePath, err := createOrUpdateProjectSettingsXML(config.ProjectSettingsFile, config.AltDeploymentRepositoryID, config.AltDeploymentRepositoryUser, config.AltDeploymentRepositoryPassword, utils)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Could not create or update project settings xml")
|
|
|
|
}
|
|
|
|
mavenOptions.ProjectSettingsFile = projectSettingsFilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
deployFlags := []string{"-Dmaven.main.skip=true", "-Dmaven.test.skip=true", "-Dmaven.install.skip=true"}
|
|
|
|
if (len(config.AltDeploymentRepositoryID) > 0) && (len(config.AltDeploymentRepositoryURL) > 0) {
|
|
|
|
deployFlags = append(deployFlags, "-DaltDeploymentRepository="+config.AltDeploymentRepositoryID+"::default::"+config.AltDeploymentRepositoryURL)
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:06:46 +02:00
|
|
|
downloadClient := &piperhttp.Client{}
|
|
|
|
runner := &command.Command{}
|
|
|
|
fileUtils := &piperutils.Files{}
|
|
|
|
if len(config.CustomTLSCertificateLinks) > 0 {
|
|
|
|
if err := loadRemoteRepoCertificates(config.CustomTLSCertificateLinks, downloadClient, &deployFlags, runner, fileUtils, config.JavaCaCertFilePath); err != nil {
|
|
|
|
log.SetErrorCategory(log.ErrorInfrastructure)
|
|
|
|
return err
|
|
|
|
}
|
2021-06-01 09:24:36 +02:00
|
|
|
}
|
2021-07-22 11:06:46 +02:00
|
|
|
|
2021-06-01 09:24:36 +02:00
|
|
|
mavenOptions.Flags = deployFlags
|
|
|
|
mavenOptions.Goals = []string{"deploy"}
|
|
|
|
mavenOptions.Defines = []string{}
|
|
|
|
_, err := maven.Execute(&mavenOptions, utils)
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
log.Entry().Infof("publish not detected, ignoring maven deploy")
|
|
|
|
}
|
|
|
|
}
|
2020-03-13 14:32:37 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-06-01 09:24:36 +02:00
|
|
|
|
|
|
|
func createOrUpdateProjectSettingsXML(projectSettingsFile string, altDeploymentRepositoryID string, altDeploymentRepositoryUser string, altDeploymentRepositoryPassword string, utils maven.Utils) (string, error) {
|
|
|
|
if len(projectSettingsFile) > 0 {
|
|
|
|
projectSettingsFilePath, err := maven.UpdateProjectSettingsXML(projectSettingsFile, altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Could not update settings xml")
|
|
|
|
}
|
|
|
|
return projectSettingsFilePath, nil
|
|
|
|
} else {
|
|
|
|
projectSettingsFilePath, err := maven.CreateNewProjectSettingsXML(altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Could not create settings xml")
|
|
|
|
}
|
|
|
|
return projectSettingsFilePath, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:06:46 +02:00
|
|
|
func loadRemoteRepoCertificates(certificateList []string, client piperhttp.Downloader, flags *[]string, runner command.ExecRunner, fileUtils piperutils.FileUtils, javaCaCertFilePath string) error {
|
|
|
|
existingJavaCaCerts := filepath.Join(os.Getenv("JAVA_HOME"), "jre", "lib", "security", "cacerts")
|
|
|
|
|
|
|
|
if len(javaCaCertFilePath) > 0 {
|
|
|
|
existingJavaCaCerts = javaCaCertFilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := fileUtils.FileExists(existingJavaCaCerts)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Could not find the existing java cacerts")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return errors.Wrap(err, "Could not find the existing java cacerts")
|
|
|
|
}
|
|
|
|
|
|
|
|
trustStore := filepath.Join(getWorkingDirForTrustStore(), ".pipeline", "mavenCaCerts")
|
|
|
|
|
|
|
|
log.Entry().Infof("copying java cacerts : %s to new cacerts : %s", existingJavaCaCerts, trustStore)
|
|
|
|
_, fileUtilserr := fileUtils.Copy(existingJavaCaCerts, trustStore)
|
|
|
|
|
|
|
|
if fileUtilserr != nil {
|
|
|
|
return errors.Wrap(err, "Could not copy existing cacerts into new cacerts location ")
|
|
|
|
}
|
|
|
|
|
2021-06-01 09:24:36 +02:00
|
|
|
log.Entry().Infof("using trust store %s", trustStore)
|
|
|
|
|
|
|
|
if exists, _ := fileUtils.FileExists(trustStore); exists {
|
2021-07-22 11:06:46 +02:00
|
|
|
maven_opts := "-Djavax.net.ssl.trustStore=.pipeline/mavenCaCerts -Djavax.net.ssl.trustStorePassword=changeit"
|
2021-06-01 09:24:36 +02:00
|
|
|
err := os.Setenv("MAVEN_OPTS", maven_opts)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Could not create MAVEN_OPTS environment variable ")
|
|
|
|
}
|
|
|
|
log.Entry().WithField("trust store", trustStore).Info("Using local trust store")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(certificateList) > 0 {
|
|
|
|
keytoolOptions := []string{
|
|
|
|
"-import",
|
|
|
|
"-noprompt",
|
|
|
|
"-storepass", "changeit",
|
|
|
|
"-keystore", trustStore,
|
|
|
|
}
|
|
|
|
tmpFolder := getTempDirForCertFile()
|
|
|
|
defer os.RemoveAll(tmpFolder) // clean up
|
|
|
|
|
|
|
|
for _, certificate := range certificateList {
|
|
|
|
filename := path.Base(certificate) // decode?
|
|
|
|
target := filepath.Join(tmpFolder, filename)
|
|
|
|
|
|
|
|
log.Entry().WithField("source", certificate).WithField("target", target).Info("Downloading TLS certificate")
|
|
|
|
// download certificate
|
|
|
|
if err := client.DownloadFile(certificate, target, nil, nil); err != nil {
|
|
|
|
return errors.Wrapf(err, "Download of TLS certificate failed")
|
|
|
|
}
|
|
|
|
options := append(keytoolOptions, "-file", target)
|
|
|
|
options = append(options, "-alias", filename)
|
|
|
|
// add certificate to keystore
|
|
|
|
if err := runner.RunExecutable("keytool", options...); err != nil {
|
|
|
|
return errors.Wrap(err, "Adding certificate to keystore failed")
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 11:06:46 +02:00
|
|
|
log.Entry().Infof("custom tls certificates successfully added to the trust store %s", trustStore)
|
2021-06-01 09:24:36 +02:00
|
|
|
} else {
|
|
|
|
log.Entry().Debug("Download of TLS certificates skipped")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getWorkingDirForTrustStore() string {
|
|
|
|
workingDir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).WithField("path", workingDir).Debug("Retrieving of work directory failed")
|
|
|
|
}
|
|
|
|
return workingDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTempDirForCertFile() string {
|
|
|
|
tmpFolder, err := ioutil.TempDir(".", "temp-")
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).WithField("path", tmpFolder).Debug("Creating temp directory failed")
|
|
|
|
}
|
|
|
|
return tmpFolder
|
|
|
|
}
|