github.com/jfrog/jfrog-cli-core/v2@v2.52.0/utils/mvn/utils.go (about)

     1  package mvnutils
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/jfrog/build-info-go/build"
    10  	"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
    11  
    12  	buildUtils "github.com/jfrog/jfrog-cli-core/v2/common/build"
    13  	"github.com/jfrog/jfrog-cli-core/v2/common/project"
    14  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
    15  	"github.com/jfrog/jfrog-cli-core/v2/utils/dependencies"
    16  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    17  	"github.com/spf13/viper"
    18  )
    19  
    20  type MvnUtils struct {
    21  	vConfig                   *viper.Viper
    22  	buildConf                 *buildUtils.BuildConfiguration
    23  	buildArtifactsDetailsFile string
    24  	goals                     []string
    25  	threads                   int
    26  	insecureTls               bool
    27  	disableDeploy             bool
    28  	outputWriter              io.Writer
    29  }
    30  
    31  func NewMvnUtils() *MvnUtils {
    32  	return &MvnUtils{buildConf: &buildUtils.BuildConfiguration{}}
    33  }
    34  
    35  func (mu *MvnUtils) SetBuildConf(buildConf *buildUtils.BuildConfiguration) *MvnUtils {
    36  	mu.buildConf = buildConf
    37  	return mu
    38  }
    39  
    40  func (mu *MvnUtils) SetBuildArtifactsDetailsFile(buildArtifactsDetailsFile string) *MvnUtils {
    41  	mu.buildArtifactsDetailsFile = buildArtifactsDetailsFile
    42  	return mu
    43  }
    44  
    45  func (mu *MvnUtils) SetGoals(goals []string) *MvnUtils {
    46  	mu.goals = goals
    47  	return mu
    48  }
    49  
    50  func (mu *MvnUtils) SetThreads(threads int) *MvnUtils {
    51  	mu.threads = threads
    52  	return mu
    53  }
    54  
    55  func (mu *MvnUtils) SetInsecureTls(insecureTls bool) *MvnUtils {
    56  	mu.insecureTls = insecureTls
    57  	return mu
    58  }
    59  
    60  func (mu *MvnUtils) SetDisableDeploy(disableDeploy bool) *MvnUtils {
    61  	mu.disableDeploy = disableDeploy
    62  	return mu
    63  }
    64  
    65  func (mu *MvnUtils) SetConfig(vConfig *viper.Viper) *MvnUtils {
    66  	mu.vConfig = vConfig
    67  	return mu
    68  }
    69  
    70  func (mu *MvnUtils) SetOutputWriter(writer io.Writer) *MvnUtils {
    71  	mu.outputWriter = writer
    72  	return mu
    73  }
    74  
    75  func RunMvn(mu *MvnUtils) error {
    76  	buildInfoService := buildUtils.CreateBuildInfoService()
    77  	buildName, err := mu.buildConf.GetBuildName()
    78  	if err != nil {
    79  		return err
    80  	}
    81  	buildNumber, err := mu.buildConf.GetBuildNumber()
    82  	if err != nil {
    83  		return err
    84  	}
    85  	mvnBuild, err := buildInfoService.GetOrCreateBuildWithProject(buildName, buildNumber, mu.buildConf.GetProject())
    86  	if err != nil {
    87  		return errorutils.CheckError(err)
    88  	}
    89  	mavenModule, err := mvnBuild.AddMavenModule("")
    90  	if err != nil {
    91  		return errorutils.CheckError(err)
    92  	}
    93  	props, useWrapper, err := createMvnRunProps(mu.vConfig, mu.buildArtifactsDetailsFile, mu.threads, mu.insecureTls, mu.disableDeploy)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	var mvnOpts []string
    98  	if v := os.Getenv("MAVEN_OPTS"); v != "" {
    99  		mvnOpts = strings.Fields(v)
   100  	}
   101  	if v, ok := props["buildInfoConfig.artifactoryResolutionEnabled"]; ok {
   102  		mvnOpts = append(mvnOpts, "-DbuildInfoConfig.artifactoryResolutionEnabled="+v)
   103  	}
   104  	dependencyLocalPath, err := getMavenDependencyLocalPath()
   105  	if err != nil {
   106  		return err
   107  	}
   108  	mavenModule.SetExtractorDetails(dependencyLocalPath,
   109  		filepath.Join(coreutils.GetCliPersistentTempDirPath(), buildUtils.PropertiesTempPath),
   110  		mu.goals,
   111  		dependencies.DownloadExtractor,
   112  		props,
   113  		useWrapper).
   114  		SetOutputWriter(mu.outputWriter)
   115  	mavenModule.SetMavenOpts(mvnOpts...)
   116  	return coreutils.ConvertExitCodeError(mavenModule.CalcDependencies())
   117  }
   118  
   119  func getMavenDependencyLocalPath() (string, error) {
   120  	dependenciesPath, err := config.GetJfrogDependenciesPath()
   121  	if err != nil {
   122  		return "", err
   123  	}
   124  	return filepath.Join(dependenciesPath, "maven", build.MavenExtractorDependencyVersion), nil
   125  }
   126  
   127  func createMvnRunProps(vConfig *viper.Viper, buildArtifactsDetailsFile string, threads int, insecureTls, disableDeploy bool) (props map[string]string, useWrapper bool, err error) {
   128  	useWrapper = vConfig.GetBool("useWrapper")
   129  	vConfig.Set(buildUtils.InsecureTls, insecureTls)
   130  	if threads > 0 {
   131  		vConfig.Set(buildUtils.ForkCount, threads)
   132  	}
   133  
   134  	if disableDeploy {
   135  		setDeployFalse(vConfig)
   136  	}
   137  
   138  	if vConfig.IsSet("resolver") {
   139  		vConfig.Set("buildInfoConfig.artifactoryResolutionEnabled", "true")
   140  	}
   141  	buildInfoProps, err := buildUtils.CreateBuildInfoProps(buildArtifactsDetailsFile, vConfig, project.Maven)
   142  
   143  	return buildInfoProps, useWrapper, err
   144  }
   145  
   146  func setDeployFalse(vConfig *viper.Viper) {
   147  	vConfig.Set(buildUtils.DeployerPrefix+buildUtils.DeployArtifacts, "false")
   148  	if vConfig.GetString(buildUtils.DeployerPrefix+buildUtils.Url) == "" {
   149  		vConfig.Set(buildUtils.DeployerPrefix+buildUtils.Url, "http://empty_url")
   150  	}
   151  	if vConfig.GetString(buildUtils.DeployerPrefix+buildUtils.ReleaseRepo) == "" {
   152  		vConfig.Set(buildUtils.DeployerPrefix+buildUtils.ReleaseRepo, "empty_repo")
   153  	}
   154  	if vConfig.GetString(buildUtils.DeployerPrefix+buildUtils.SnapshotRepo) == "" {
   155  		vConfig.Set(buildUtils.DeployerPrefix+buildUtils.SnapshotRepo, "empty_repo")
   156  	}
   157  }