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

🐛 Global maven settings for Java provider #713

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -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 := `
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>%v</localRepository>
</settings>
`
var settingsFilePath string
m2Home := os.Getenv("M2_HOME")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I don't understand why M2_HOME is used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{}{
Expand Down
Loading