From 6db200d89ed512603aa65344796127ad5d389c82 Mon Sep 17 00:00:00 2001 From: Emily McMullan Date: Tue, 29 Oct 2024 14:52:19 -0400 Subject: [PATCH] global settings file for java provider Signed-off-by: Emily McMullan --- .../pkg/java_external_provider/provider.go | 40 +++++++++++++++++++ .../java_external_provider/service_client.go | 4 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/external-providers/java-external-provider/pkg/java_external_provider/provider.go b/external-providers/java-external-provider/pkg/java_external_provider/provider.go index 5c9c527e..278b3d41 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/provider.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/provider.go @@ -41,6 +41,7 @@ const ( BUNDLES_INIT_OPTION = "bundles" WORKSPACE_INIT_OPTION = "workspace" MVN_SETTINGS_FILE_INIT_OPTION = "mavenSettingsFile" + GLOBAL_SETTINGS_INIT_OPTION = "mavenCacheDir" JVM_MAX_MEM_INIT_OPTION = "jvmMaxMem" ) @@ -237,6 +238,17 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide if !ok { mavenSettingsFile = "" } + var globalSettingsFile string + var returnError error + globalM2, ok := config.ProviderSpecificConfig[GLOBAL_SETTINGS_INIT_OPTION].(string) + if !ok { + globalM2 = "" + } else { + globalSettingsFile, returnError = p.BuildSettingsFile(globalM2) + if returnError != nil { + return nil, additionalBuiltinConfig, returnError + } + } lspServerPath, ok := config.ProviderSpecificConfig[provider.LspServerPathConfigKey].(string) if !ok || lspServerPath == "" { @@ -387,6 +399,7 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide depToLabels: map[string]*depLabelItem{}, isLocationBinary: isBinary, mvnSettingsFile: mavenSettingsFile, + globalSettings: globalSettingsFile, depsLocationCache: make(map[string]int), includedPaths: provider.GetIncludedPathsFromConfig(config, false), } @@ -884,3 +897,30 @@ func (p *javaProvider) GetDependencies(ctx context.Context) (map[uri.URI][]*prov func (p *javaProvider) GetDependenciesDAG(ctx context.Context) (map[uri.URI][]provider.DepDAGItem, error) { return provider.FullDepDAGResponse(ctx, p.clients) } + +func (p *javaProvider) BuildSettingsFile(m2CacheDir string) (settingsFile string, err error) { + fileContentTemplate := ` + + %v + + ` + var settingsFilePath string + m2Home := os.Getenv("M2_HOME") + if m2Home != "" { + settingsFilePath = filepath.Join(m2Home, "conf", "globalSettings.xml") + f, err := os.Create(settingsFilePath) + if err != nil { + return "", err + } + defer func() { + _ = f.Close() + }() + _, err = f.Write([]byte(fmt.Sprintf(fileContentTemplate, m2CacheDir))) + if err != nil { + return "", err + } + } + + return settingsFilePath, nil +} diff --git a/external-providers/java-external-provider/pkg/java_external_provider/service_client.go b/external-providers/java-external-provider/pkg/java_external_provider/service_client.go index f3d8d2f8..ed2a6129 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/service_client.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/service_client.go @@ -32,6 +32,7 @@ type javaServiceClient struct { depToLabels map[string]*depLabelItem isLocationBinary bool mvnSettingsFile string + globalSettings string depsMutex sync.RWMutex depsCache map[uri.URI][]*provider.Dep depsLocationCache map[string]int @@ -241,7 +242,8 @@ func (p *javaServiceClient) initialization(ctx context.Context) { "java": map[string]interface{}{ "configuration": map[string]interface{}{ "maven": map[string]interface{}{ - "userSettings": p.mvnSettingsFile, + "userSettings": p.mvnSettingsFile, + "globalSettings": p.globalSettings, }, }, "autobuild": map[string]interface{}{