Skip to content
Open
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 @@ -404,6 +404,7 @@ public static class LanguageServerImpl implements LanguageServer, LanguageClient

private static final String NETBEANS_FORMAT = "format";
private static final String NETBEANS_JAVA_IMPORTS = "java.imports";
private static final String NETBEANS_MAVEN_USER_SETTINGS = "maven.userSettings";
private static final String NETBEANS_PROJECT_JDKHOME = "project.jdkhome";
private static final String NETBEANS_JAVA_HINTS = "hints";

Expand Down Expand Up @@ -1050,6 +1051,16 @@ private void collectProjectCandidates(FileObject fo, List<FileObject> candidates
}

private void initializeOptions() {
// Request maven user settings from the client before projects open,
// so embedders are created with the correct settings.xml path.
client.getClientConfigurationManager().getConfiguration(NETBEANS_MAVEN_USER_SETTINGS).thenAccept(c -> {
JsonPrimitive newMavenUserSettingsPath = null;
if (c instanceof JsonPrimitive) {
newMavenUserSettingsPath = (JsonPrimitive) c;
}
Comment on lines +1057 to +1060
Copy link
Member

Choose a reason for hiding this comment

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

nitpick:

        JsonPrimitive newMavenUserSettingsPath = c instanceof JsonPrimitive jp ? jp : null;

workspaceService.updateMavenUserSettingsPreferences(newMavenUserSettingsPath);
});

getWorkspaceProjects().thenAccept(projects -> {
List<String> defaultConfigs = List.of(NETBEANS_JAVA_HINTS, NETBEANS_PROJECT_JDKHOME);
List<String> projectConfigs = List.of(NETBEANS_FORMAT, NETBEANS_JAVA_IMPORTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.NbPreferences;
import org.openide.util.Pair;
import org.openide.util.RequestProcessor;
import org.openide.util.WeakListeners;
Expand All @@ -166,6 +167,8 @@ public final class WorkspaceServiceImpl implements WorkspaceService, LanguageCli
private static final RequestProcessor WORKER = new RequestProcessor(WorkspaceServiceImpl.class.getName(), 1, false, false);
private static final RequestProcessor PROJECT_WORKER = new RequestProcessor(WorkspaceServiceImpl.class.getName(), 5, false, false);
private static final String NETBEANS_JAVA_HINTS = "hints";
private static final String MAVEN_PREFERENCES_NODE = "org/netbeans/modules/maven"; // NOI18N
private static final String MAVEN_USER_SETTINGS_XML = "userSettingsXml"; // NOI18N

private final Gson gson = new Gson();
private final LspServerState server;
Expand Down Expand Up @@ -1425,13 +1428,33 @@ void registerConfigChangeListeners() {



BiConsumer<String, JsonElement> mavenUserSettingsListener = (config, newValue)
-> updateMavenUserSettingsPreferences(newValue.isJsonPrimitive() ? newValue.getAsJsonPrimitive() : null);

confManager.registerConfigChangeListener(fullConfigPrefix + "hints", hintPrefsListener);
confManager.registerConfigChangeListener(fullConfigPrefix + "project.jdkhome", projectJdkHomeListener);
confManager.registerConfigChangeListener(fullConfigPrefix + "format", formatPrefsListener);
confManager.registerConfigChangeListener(fullConfigPrefix + "java.imports", importPrefsListener);
confManager.registerConfigChangeListener(fullConfigPrefix + "maven.userSettings", mavenUserSettingsListener);
confManager.registerConfigChangeListener(fullAltConfigPrefix + "runConfig", getRunConfigChangeListener());
}

void updateMavenUserSettingsPreferences(JsonPrimitive configuration) {
String userSettingsPath = configuration != null ? configuration.getAsString().trim() : "";
Preferences mavenPrefs = NbPreferences.root().node(MAVEN_PREFERENCES_NODE);
String current = mavenPrefs.get(MAVEN_USER_SETTINGS_XML, null);
if (userSettingsPath.isEmpty()) {
if (current != null) {
mavenPrefs.remove(MAVEN_USER_SETTINGS_XML);
}
} else {
String normalized = FileUtil.normalizeFile(new File(userSettingsPath)).getAbsolutePath();
if (!normalized.equals(current)) {
mavenPrefs.put(MAVEN_USER_SETTINGS_XML, normalized);
}
}
}

void updateJavaFormatPreferences(FileObject fo, JsonObject configuration) {
if (configuration != null && client.getNbCodeCapabilities().wantsJavaSupport()) {
JsonElement pathElement = configuration.get("settingsPath");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @author mkleint
*/
record EmbedderConfiguration(PlexusContainer container, Properties systemProps, Properties userProps, boolean offline, File settingsXml) {
record EmbedderConfiguration(PlexusContainer container, Properties systemProps, Properties userProps, boolean offline, File settingsXml, File userSettingsXml) {

Properties getSystemProperties() {
return systemProps();
Expand All @@ -49,4 +49,8 @@ File getSettingsXml() {
return settingsXml();
}

File getUserSettingsXml() {
return userSettingsXml();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import org.apache.maven.cli.configuration.SettingsXmlConfigurationProcessor;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.*;
Expand Down Expand Up @@ -57,9 +58,10 @@
public final class EmbedderFactory {

public static final String PROP_COMMANDLINE_PATH = "commandLineMavenPath";
public static final String PROP_USER_SETTINGS_XML = "userSettingsXml";

//same prop constant in MavenSettings.java
static final String PROP_DEFAULT_OPTIONS = "defaultOptions";
static final String PROP_DEFAULT_OPTIONS = "defaultOptions";
private static final Set<String> forbidden = Set.of(
"netbeans.logger.console", //NOI18N
"java.util.logging.config.class", //NOI18N
Expand Down Expand Up @@ -113,6 +115,13 @@ public void projectGroupChanging(ProjectGroupChangeEvent event) {
public void projectGroupChanged(ProjectGroupChangeEvent event) {}
});
});
// Listen for external preference changes (e.g. from LSP module) and reset
// cached embedders so they pick up the new user settings XML path.
getPreferences().addPreferenceChangeListener((evt) -> {
if (PROP_USER_SETTINGS_XML.equals(evt.getKey())) {
resetCachedEmbedders();
}
});
// start initialization; guice can take a while the first time it runs
// if something calls getProjectEmbedder() in the mean time, this is becomes a no-op
warmupTask.schedule(100);
Expand Down Expand Up @@ -256,6 +265,33 @@ private static File getSettingsXml() {
return new File(getEffectiveMavenHome(), "conf/settings.xml");
}

public static @NonNull File getDefaultUserSettingsXmlFile() {
return SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE;
}

public static @NonNull File getUserSettingsXmlFile() {
String str = getPreferences().get(PROP_USER_SETTINGS_XML, null);
if (str != null) {
return FileUtil.normalizeFile(new File(str));
} else {
return getDefaultUserSettingsXmlFile();
}
}

public static void setUserSettingsXmlFile(File path) {
File oldValue = getUserSettingsXmlFile();
File defValue = getDefaultUserSettingsXmlFile();
if (oldValue.equals(path) || path == null && oldValue.equals(defValue)) {
return;
}
if (path == null || path.equals(defValue)) {
getPreferences().remove(PROP_USER_SETTINGS_XML);
} else {
getPreferences().put(PROP_USER_SETTINGS_XML, FileUtil.normalizeFile(path).getAbsolutePath());
}
resetCachedEmbedders();
}

/**
* #191267: suppresses logging from embedded Maven, since interesting results normally appear elsewhere.
*/
Expand Down Expand Up @@ -322,7 +358,7 @@ private Logger logger() {

Properties userprops = new Properties();
userprops.putAll(getCustomGlobalUserProperties());
EmbedderConfiguration configuration = new EmbedderConfiguration(pc, cloneStaticProps(), userprops, true, getSettingsXml());
EmbedderConfiguration configuration = new EmbedderConfiguration(pc, cloneStaticProps(), userprops, true, getSettingsXml(), getUserSettingsXmlFile());

try {
return new MavenEmbedder(configuration);
Expand Down Expand Up @@ -425,7 +461,7 @@ public static boolean isProjectEmbedderLoaded() {

Properties userprops = new Properties();
userprops.putAll(getCustomGlobalUserProperties());
EmbedderConfiguration req = new EmbedderConfiguration(pc, cloneStaticProps(), userprops, false, getSettingsXml());
EmbedderConfiguration req = new EmbedderConfiguration(pc, cloneStaticProps(), userprops, false, getSettingsXml(), getUserSettingsXmlFile());

// //TODO remove explicit activation
// req.addActiveProfile("netbeans-public").addActiveProfile("netbeans-private"); //NOI18N
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ public synchronized Settings getSettings() {
return testSettings; // could instead make public void setSettings(Settings settingsOverride)
}
File settingsXml = embedderConfiguration.getSettingsXml();
long newSettingsTimestamp = settingsXml.hashCode() ^ settingsXml.lastModified() ^ SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.lastModified();
File userSettingsXml = embedderConfiguration.getUserSettingsXml();
long newSettingsTimestamp = settingsXml.hashCode() ^ settingsXml.lastModified() ^ userSettingsXml.lastModified();
// could be included but currently constant: hashCode() of those files; getSystemProperties.hashCode()
if (settings != null && settingsTimestamp == newSettingsTimestamp) {
LOG.log(Level.FINER, "settings.xml cache hit for {0}", this);
Expand All @@ -209,7 +210,7 @@ public synchronized Settings getSettings() {
LOG.log(Level.FINE, "settings.xml cache miss for {0}", this);
SettingsBuildingRequest req = new DefaultSettingsBuildingRequest();
req.setGlobalSettingsFile(settingsXml);
req.setUserSettingsFile(SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE);
req.setUserSettingsFile(userSettingsXml);
req.setSystemProperties(getSystemProperties());
req.setUserProperties(embedderConfiguration.getUserProperties());
try {
Expand Down Expand Up @@ -586,8 +587,9 @@ public MavenExecutionRequest createMavenExecutionRequest(){
if (settingsXml !=null && settingsXml.exists()) {
req.setGlobalSettingsFile(settingsXml);
}
if (SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE != null && SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.exists()) {
req.setUserSettingsFile(SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE);
File userSettingsXml = embedderConfiguration.getUserSettingsXml();
if (userSettingsXml != null && userSettingsXml.exists()) {
req.setUserSettingsFile(userSettingsXml);
}

req.setSystemProperties(getSystemProperties());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,11 @@ private static List<String> createMavenExecutionCommand(RunConfig config, Constr
if (config.isUpdateSnapshots()) {
toRet.add("--update-snapshots");//NOI18N
}
String userSettingsXml = MavenSettings.getDefault().getUserSettingsXml();
if (!userSettingsXml.equals(EmbedderFactory.getDefaultUserSettingsXmlFile().getAbsolutePath())) {
toRet.add("--settings");
toRet.add(userSettingsXml);
}
if (config.getReactorStyle() != RunConfig.ReactorStyle.NONE) {
File basedir = config.getExecutionDirectory();
MavenProject mp = NbMavenProject.getPartialProject(config.getMavenProject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,4 @@ SettingsPanel.lblIndexFilter.text=Index Filter:
SettingsPanel.rb2Years.text=2 years
SettingsPanel.rb5Years.text=5 years
SettingsPanel.rbFullIndex.text=Full Index (no filter)
SettingsPanel.lblMavenUserSettingsXml.text=&Maven User Settings XML:
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ public void setDefaultOptions(String options) {
}
}

public String getUserSettingsXml() {
return EmbedderFactory.getUserSettingsXmlFile().getAbsolutePath();
}

public void setUserSettingsXml(String path) {
File file = (path == null || path.trim().isEmpty()) ? null : new File(path.trim());
EmbedderFactory.setUserSettingsXmlFile(file);
}

public boolean isVMOptionsWrap() {
return getPreferences().getBoolean(PROP_VM_OPTIONS_WRAP, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="appearancePanel" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="354" max="32767" attributes="0"/>
<EmptySpace pref="397" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
Expand Down Expand Up @@ -196,7 +196,7 @@
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="dependenciesPanel" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="277" max="32767" attributes="0"/>
<EmptySpace pref="320" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
Expand Down Expand Up @@ -420,7 +420,7 @@
<Component id="btnIndex" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" min="-2" max="-2" attributes="0"/>
<Component id="permissionsTableScrollPane" pref="107" max="32767" attributes="0"/>
<Component id="permissionsTableScrollPane" pref="150" max="32767" attributes="0"/>
<EmptySpace type="unrelated" min="-2" max="-2" attributes="0"/>
<Component id="descriptionLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" max="-2" attributes="0"/>
Expand Down Expand Up @@ -617,7 +617,7 @@
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="cbPreferWrapper" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
<EmptySpace min="0" pref="137" max="32767" attributes="0"/>
</Group>
<Component id="comMavenHome" max="32767" attributes="0"/>
<Group type="102" attributes="0">
Expand Down Expand Up @@ -645,6 +645,13 @@
</Group>
</Group>
</Group>
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="lblMavenUserSettingsXml" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="4" max="-2" attributes="0"/>
<Component id="comMavenUserSettingsXml" max="32767" attributes="0"/>
<EmptySpace min="-2" pref="84" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
Expand Down Expand Up @@ -679,6 +686,11 @@
<EmptySpace min="3" pref="3" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="lblMavenUserSettingsXml" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="comMavenUserSettingsXml" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Component id="cbSkipTests" min="-2" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/>
Expand Down Expand Up @@ -883,6 +895,23 @@
</Property>
</Properties>
</Component>
<Component class="javax.swing.JComboBox" name="comMavenUserSettingsXml">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="0"/>
</Property>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="lblMavenUserSettingsXml">
<Properties>
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
<ResourceString bundle="org/netbeans/modules/maven/options/Bundle.properties" key="SettingsPanel.lblMavenUserSettingsXml.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
</Property>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_InitCodePre" type="java.lang.String" value="lblCommandLine.setLabelFor(comMavenHome);"/>
</AuxValues>
</Component>
</SubComponents>
</Container>
</SubComponents>
Expand Down
Loading
Loading