mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1168066444
##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
}
private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
Configuration currenConfig = oldPlugin.getConfiguration();
+ Plugin newPlugin = factory.createPlugin();
+ newPlugin.setGroupId(oldPlugin.getGroupId());
+ newPlugin.setArtifactId(oldPlugin.getArtifactId());
+ newPlugin.setVersion(oldPlugin.getVersion());
+
newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+ return newPlugin;
+ }
+
+ private Configuration updateMavenCompilerPluginConfiguration(final
Configuration currenConfig) {
Configuration newConfiguration = createConfiguration();
- boolean isCompilerArgsElementPresent = false;
if (currenConfig != null) {
+ boolean isCompilerArgsElementPresent = false;
+ boolean isSourceElementPresent = false;
+ boolean isTargetElementPresent = false;
for (POMExtensibilityElement element :
currenConfig.getConfigurationElements()) {
POMExtensibilityElement newElement =
factory.createPOMExtensibilityElement(element.getQName());
String elementText = element.getElementText();
if (elementText.trim().length() > 0) {
newElement.setElementText(element.getElementText());
}
+ if (newElement.getQName().getLocalPart().equals(SOURCE) &&
newSourceLevel != null) {
+ newElement.setElementText(newSourceLevel);
+ isSourceElementPresent = true;
+ }
+ if (newElement.getQName().getLocalPart().equals(TARGET) &&
newSourceLevel != null) {
+ newElement.setElementText(newSourceLevel);
+ isTargetElementPresent = true;
+ }
Review Comment:
consider using this patch:
```diff
diff --git
a/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java
b/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java
index bfdc4b1..ca3b2af 100644
---
a/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java
+++
b/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java
@@ -38,6 +38,7 @@
import org.netbeans.modules.maven.model.pom.POMModel;
import org.netbeans.modules.maven.model.pom.POMQName;
import org.netbeans.modules.maven.model.pom.Plugin;
+import org.netbeans.modules.maven.model.pom.Properties;
import org.netbeans.spi.project.ProjectConfiguration;
import org.netbeans.spi.project.ProjectConfigurationProvider;
import org.openide.filesystems.FileSystem;
@@ -122,6 +123,7 @@
private static final String COMPILER_ID_PROPERTY = "compilerId"; //
NOI18N
private static final String SOURCE = "source"; // NOI18N
private static final String TARGET = "target"; // NOI18N
+ private static final String RELEASE = "release"; // NOI18N
private static final String COMPILER_ARG = "compilerArgs"; // NOI18N
private static final String MAVEN_COMPILER_VERSION = "3.11.0"; //
NOI18N
private static final String ARG = "arg";// NOI18N
@@ -144,10 +146,10 @@
Plugin oldPlugin = searchMavenCompilerPlugin(build);
if (oldPlugin == null) {
- build.addPlugin(createMavenCompilerPlugin());
+ build.addPlugin(createMavenCompilerPlugin(model));
} else {
- Plugin newPlugin = updateMavenCompilerPlugin(oldPlugin);
+ Plugin newPlugin = updateMavenCompilerPlugin(model,
oldPlugin);
build.removePlugin(oldPlugin);
build.addPlugin(newPlugin);
@@ -167,12 +169,12 @@
return null;
}
- private Plugin createMavenCompilerPlugin() {
+ private Plugin createMavenCompilerPlugin(POMModel model) {
Plugin plugin = factory.createPlugin();
plugin.setGroupId(MAVEN_COMPILER_GROUP_ID);
plugin.setArtifactId(MAVEN_COMPILER_ARTIFACT_ID);
plugin.setVersion(MAVEN_COMPILER_VERSION);
-
plugin.setConfiguration(updateMavenCompilerPluginConfiguration(createConfiguration()));
+
plugin.setConfiguration(updateMavenCompilerPluginConfiguration(model,
createConfiguration()));
return plugin;
}
@@ -181,36 +183,65 @@
return configuration;
}
- private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
+ private Plugin updateMavenCompilerPlugin(POMModel model, final
Plugin oldPlugin) {
Configuration currenConfig = oldPlugin.getConfiguration();
Plugin newPlugin = factory.createPlugin();
newPlugin.setGroupId(oldPlugin.getGroupId());
newPlugin.setArtifactId(oldPlugin.getArtifactId());
newPlugin.setVersion(oldPlugin.getVersion());
-
newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+
newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(model,
currenConfig));
return newPlugin;
}
- private Configuration updateMavenCompilerPluginConfiguration(final
Configuration currenConfig) {
+ private Configuration
updateMavenCompilerPluginConfiguration(POMModel model, final Configuration
currentConfig) {
Configuration newConfiguration = createConfiguration();
- if (currenConfig != null) {
+ if (currentConfig != null) {
+
+ Properties props = model.getProject().getProperties();
+
+ boolean useProps = false;
+ if (props != null && (
props.getProperty("maven.compiler.release") != null // NOI18N
+ ||
props.getProperty("maven.compiler.source") != null // NOI18N
+ ||
props.getProperty("maven.compiler.target") != null)) { // NOI18N
+ if (newSourceLevel != null) {
+ if (props.getProperty("maven.compiler.release") !=
null) { // NOI18N
+ props.setProperty("maven.compiler.release",
newSourceLevel); // NOI18N
+ }
+ if (props.getProperty("maven.compiler.source") !=
null) { // NOI18N
+ props.setProperty("maven.compiler.source",
newSourceLevel); // NOI18N
+ }
+ if (props.getProperty("maven.compiler.target") !=
null) { // NOI18N
+ props.setProperty("maven.compiler.target",
newSourceLevel); // NOI18N
+ }
+ }
+ useProps = true;
+ }
+
boolean isCompilerArgsElementPresent = false;
boolean isSourceElementPresent = false;
boolean isTargetElementPresent = false;
- for (POMExtensibilityElement element :
currenConfig.getConfigurationElements()) {
+ boolean isReleaseElementPresent = false;
+
+ for (POMExtensibilityElement element :
currentConfig.getConfigurationElements()) {
POMExtensibilityElement newElement =
factory.createPOMExtensibilityElement(element.getQName());
String elementText = element.getElementText();
if (elementText.trim().length() > 0) {
newElement.setElementText(element.getElementText());
}
- if (newElement.getQName().getLocalPart().equals(SOURCE)
&& newSourceLevel != null) {
- newElement.setElementText(newSourceLevel);
- isSourceElementPresent = true;
- }
- if (newElement.getQName().getLocalPart().equals(TARGET)
&& newSourceLevel != null) {
- newElement.setElementText(newSourceLevel);
- isTargetElementPresent = true;
+ if (newSourceLevel != null) {
+ if
(newElement.getQName().getLocalPart().equals(SOURCE)) {
+ newElement.setElementText(newSourceLevel);
+ isSourceElementPresent = true;
+ }
+ if
(newElement.getQName().getLocalPart().equals(TARGET)) {
+ newElement.setElementText(newSourceLevel);
+ isTargetElementPresent = true;
+ }
+ if
(newElement.getQName().getLocalPart().equals(RELEASE)) {
+ newElement.setElementText(newSourceLevel);
+ isReleaseElementPresent = true;
+ }
}
if
(newElement.getQName().getLocalPart().equals(COMPILER_ARG)) {
isCompilerArgsElementPresent = true;
@@ -228,15 +259,17 @@
newConfiguration.addExtensibilityElement(newElement);
}
- if (!isSourceElementPresent && newSourceLevel != null) {
- POMExtensibilityElement source =
factory.createPOMExtensibilityElement(POMQName.createQName(SOURCE));
- source.setElementText(newSourceLevel);
- newConfiguration.addExtensibilityElement(source);
- }
- if (!isTargetElementPresent && newSourceLevel != null) {
- POMExtensibilityElement target =
factory.createPOMExtensibilityElement(POMQName.createQName(TARGET));
- target.setElementText(newSourceLevel);
- newConfiguration.addExtensibilityElement(target);
+ if (!useProps && newSourceLevel != null &&
!isReleaseElementPresent) {
+ if (!isSourceElementPresent) {
+ POMExtensibilityElement source =
factory.createPOMExtensibilityElement(POMQName.createQName(SOURCE));
+ source.setElementText(newSourceLevel);
+ newConfiguration.addExtensibilityElement(source);
+ }
+ if (!isTargetElementPresent) {
+ POMExtensibilityElement target =
factory.createPOMExtensibilityElement(POMQName.createQName(TARGET));
+ target.setElementText(newSourceLevel);
+ newConfiguration.addExtensibilityElement(target);
+ }
}
if (!isCompilerArgsElementPresent) {
POMExtensibilityElement compilerArgs =
factory.createPOMExtensibilityElement(POMQName.createQName(COMPILER_ARG));
```
this mitigates some issues.
- adds support for properties
- will work for projects > JDK 8 which are encouraged to use `release`
This hint will need some more work later since it doesn't support maven
features like `<pluginManagement>` etc, but at least it won't break poms now
(hopefully).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists