mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-22058 Maven: Separate target level settings for modules
This commit is contained in:
+18
-36
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.jetbrains.idea.maven.importing;
|
||||
|
||||
import com.intellij.compiler.CompilerConfiguration;
|
||||
import com.intellij.compiler.CompilerConfigurationImpl;
|
||||
import com.intellij.compiler.impl.javaCompiler.javac.JavacSettings;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.module.*;
|
||||
@@ -102,7 +104,6 @@ public class MavenProjectImporter {
|
||||
hasChanges = true;
|
||||
importModules(postTasks);
|
||||
scheduleRefreshResolvedArtifacts(postTasks);
|
||||
configSettings();
|
||||
}
|
||||
|
||||
if (projectsHaveChanges || myImportModuleGroupsRequired) {
|
||||
@@ -118,6 +119,10 @@ public class MavenProjectImporter {
|
||||
|
||||
if (hasChanges) {
|
||||
myModelsProvider.commit();
|
||||
|
||||
if (projectsHaveChanges) {
|
||||
configSettings();
|
||||
}
|
||||
}
|
||||
else {
|
||||
myModelsProvider.dispose();
|
||||
@@ -368,51 +373,28 @@ public class MavenProjectImporter {
|
||||
private void configSettings() {
|
||||
MavenUtil.invokeAndWaitWriteAction(myProject, new Runnable() {
|
||||
public void run() {
|
||||
String level = calcTargetLevel();
|
||||
if (level == null) return;
|
||||
CompilerConfigurationImpl configuration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
|
||||
|
||||
String options = JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING;
|
||||
Pattern pattern = Pattern.compile("(-target (\\S+))");
|
||||
Matcher matcher = pattern.matcher(options);
|
||||
MavenProjectsManager projectsManager = MavenProjectsManager.getInstance(myProject);
|
||||
|
||||
if (matcher.find()) {
|
||||
String currentValue = MavenProject.normalizeCompilerLevel(matcher.group(2));
|
||||
for (MavenProject project : myAllProjects) {
|
||||
String targetLevel = project.getTargetLevel();
|
||||
|
||||
if (currentValue == null || compareCompilerLevel(level, currentValue) < 0) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
matcher.appendReplacement(buffer, "-target " + level);
|
||||
matcher.appendTail(buffer);
|
||||
options = buffer.toString();
|
||||
if (targetLevel != null) {
|
||||
Module module = projectsManager.findModule(project);
|
||||
if (module != null) {
|
||||
configuration.setBytecodeTargetLevel(module, targetLevel);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!StringUtil.isEmptyOrSpaces(options)) options += " ";
|
||||
options += "-target " + level;
|
||||
}
|
||||
|
||||
String options = JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING;
|
||||
options = options.replaceFirst("(-target (\\S+))", ""); // Old IDEAs saved
|
||||
JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING = options;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private String calcTargetLevel() {
|
||||
String maxSource = null;
|
||||
String minTarget = null;
|
||||
for (MavenProject each : myAllProjects) {
|
||||
String source = each.getSourceLevel();
|
||||
String target = each.getTargetLevel();
|
||||
if (source != null && (maxSource == null || compareCompilerLevel(maxSource, source) < 0)) maxSource = source;
|
||||
if (target != null && (minTarget == null || compareCompilerLevel(minTarget, target) > 0)) minTarget = target;
|
||||
}
|
||||
return (maxSource != null && compareCompilerLevel(minTarget, maxSource) < 0) ? maxSource : minTarget;
|
||||
}
|
||||
|
||||
private int compareCompilerLevel(String left, String right) {
|
||||
if (left == null && right == null) return 0;
|
||||
if (left == null) return -1;
|
||||
if (right == null) return 1;
|
||||
return left.compareTo(right);
|
||||
}
|
||||
|
||||
private void importModules(final List<MavenProjectsProcessorTask> postTasks) {
|
||||
Map<MavenProject, MavenProjectChanges> projectsWithChanges = myProjectsToImportWithChanges;
|
||||
Set<MavenProject> projects = projectsWithChanges.keySet();
|
||||
|
||||
+30
-105
@@ -15,9 +15,11 @@
|
||||
*/
|
||||
package org.jetbrains.idea.maven.importing;
|
||||
|
||||
import com.intellij.compiler.CompilerConfiguration;
|
||||
import com.intellij.compiler.impl.javaCompiler.javac.JavacSettings;
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.roots.LanguageLevelModuleExtension;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
@@ -836,7 +838,7 @@ public class StructureImportingTest extends MavenImportingTestCase {
|
||||
}
|
||||
|
||||
public void testSettingTargetLevel() throws Exception {
|
||||
JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING = "-Xmm500m -Xms128m";
|
||||
JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING = "-Xmm500m -Xms128m -target 1.5";
|
||||
|
||||
importProject("<groupId>test</groupId>" +
|
||||
"<artifactId>project</artifactId>" +
|
||||
@@ -853,31 +855,16 @@ public class StructureImportingTest extends MavenImportingTestCase {
|
||||
" </plugins>" +
|
||||
"</build>");
|
||||
|
||||
assertEquals("-Xmm500m -Xms128m -target 1.3", JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING);
|
||||
assertEquals("-Xmm500m -Xms128m", JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING.trim());
|
||||
|
||||
Module module = getModule("project");
|
||||
|
||||
String targetLevel = CompilerConfiguration.getInstance(myProject).getBytecodeTargetLevel(module);
|
||||
|
||||
assertEquals("1.3", targetLevel);
|
||||
}
|
||||
|
||||
public void testRewritingTargetLevel() throws Exception {
|
||||
JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING = "-Xmm500m -target 1.5 -Xms128m";
|
||||
|
||||
importProject("<groupId>test</groupId>" +
|
||||
"<artifactId>project</artifactId>" +
|
||||
"<version>1</version>" +
|
||||
|
||||
"<build>" +
|
||||
" <plugins>" +
|
||||
" <plugin>" +
|
||||
" <artifactId>maven-compiler-plugin</artifactId>" +
|
||||
" <configuration>" +
|
||||
" <target>1.3</target>" +
|
||||
" </configuration>" +
|
||||
" </plugin>" +
|
||||
" </plugins>" +
|
||||
"</build>");
|
||||
|
||||
assertEquals("-Xmm500m -target 1.3 -Xms128m", JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING);
|
||||
}
|
||||
|
||||
public void testSettingTargetLevelAtMinimalSpecified() throws Exception {
|
||||
public void testSettingTargetLevelFromParent() throws Exception {
|
||||
createProjectPom("<groupId>test</groupId>" +
|
||||
"<artifactId>project</artifactId>" +
|
||||
"<packaging>pom</packaging>" +
|
||||
@@ -886,27 +873,32 @@ public class StructureImportingTest extends MavenImportingTestCase {
|
||||
"<modules>" +
|
||||
" <module>m1</module>" +
|
||||
" <module>m2</module>" +
|
||||
"</modules>");
|
||||
"</modules>" +
|
||||
|
||||
"<properties>" +
|
||||
"<maven.compiler.target>1.3</maven.compiler.target>" +
|
||||
"</properties>");
|
||||
|
||||
createModulePom("m1", "<groupId>test</groupId>" +
|
||||
"<artifactId>m1</artifactId>" +
|
||||
"<version>1</version>" +
|
||||
|
||||
"<build>" +
|
||||
" <plugins>" +
|
||||
" <plugin>" +
|
||||
" <artifactId>maven-compiler-plugin</artifactId>" +
|
||||
" <configuration>" +
|
||||
" <target>1.3</target>" +
|
||||
" </configuration>" +
|
||||
" </plugin>" +
|
||||
" </plugins>" +
|
||||
"</build>");
|
||||
"<parent>" +
|
||||
"<groupId>test</groupId>" +
|
||||
"<artifactId>project</artifactId>" +
|
||||
"<version>1</version>" +
|
||||
"</parent>");
|
||||
|
||||
createModulePom("m2", "<groupId>test</groupId>" +
|
||||
"<artifactId>m2</artifactId>" +
|
||||
"<version>1</version>" +
|
||||
|
||||
"<parent>" +
|
||||
"<groupId>test</groupId>" +
|
||||
"<artifactId>project</artifactId>" +
|
||||
"<version>1</version>" +
|
||||
"</parent>" +
|
||||
|
||||
"<build>" +
|
||||
" <plugins>" +
|
||||
" <plugin>" +
|
||||
@@ -920,76 +912,9 @@ public class StructureImportingTest extends MavenImportingTestCase {
|
||||
|
||||
importProject();
|
||||
|
||||
assertEquals("-target 1.3", JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING);
|
||||
}
|
||||
|
||||
public void testSettingTargetLevelAtMinimalSpecifiedButNoLessThanMaximumSourceLevel() throws Exception {
|
||||
createProjectPom("<groupId>test</groupId>" +
|
||||
"<artifactId>project</artifactId>" +
|
||||
"<packaging>pom</packaging>" +
|
||||
"<version>1</version>" +
|
||||
|
||||
"<modules>" +
|
||||
" <module>m1</module>" +
|
||||
" <module>m2</module>" +
|
||||
"</modules>");
|
||||
|
||||
createModulePom("m1", "<groupId>test</groupId>" +
|
||||
"<artifactId>m1</artifactId>" +
|
||||
"<version>1</version>" +
|
||||
|
||||
"<build>" +
|
||||
" <plugins>" +
|
||||
" <plugin>" +
|
||||
" <artifactId>maven-compiler-plugin</artifactId>" +
|
||||
" <configuration>" +
|
||||
" <target>1.3</target>" +
|
||||
" <source>1.3</source>" +
|
||||
" </configuration>" +
|
||||
" </plugin>" +
|
||||
" </plugins>" +
|
||||
"</build>");
|
||||
|
||||
createModulePom("m2", "<groupId>test</groupId>" +
|
||||
"<artifactId>m2</artifactId>" +
|
||||
"<version>1</version>" +
|
||||
|
||||
"<build>" +
|
||||
" <plugins>" +
|
||||
" <plugin>" +
|
||||
" <artifactId>maven-compiler-plugin</artifactId>" +
|
||||
" <configuration>" +
|
||||
" <target>1.5</target>" +
|
||||
" <source>1.5</source>" +
|
||||
" </configuration>" +
|
||||
" </plugin>" +
|
||||
" </plugins>" +
|
||||
"</build>");
|
||||
|
||||
importProject();
|
||||
|
||||
assertEquals("-target 1.5", JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING);
|
||||
}
|
||||
|
||||
public void testRewritingIncorrectTargetLevel() throws Exception {
|
||||
JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING = "-Xmm500m -target ${undefined.variable} -Xms128m";
|
||||
|
||||
importProject("<groupId>test</groupId>" +
|
||||
"<artifactId>project</artifactId>" +
|
||||
"<version>1</version>" +
|
||||
|
||||
"<build>" +
|
||||
" <plugins>" +
|
||||
" <plugin>" +
|
||||
" <artifactId>maven-compiler-plugin</artifactId>" +
|
||||
" <configuration>" +
|
||||
" <target>1.3</target>" +
|
||||
" </configuration>" +
|
||||
" </plugin>" +
|
||||
" </plugins>" +
|
||||
"</build>");
|
||||
|
||||
assertEquals("-Xmm500m -target 1.3 -Xms128m", JavacSettings.getInstance(myProject).ADDITIONAL_OPTIONS_STRING);
|
||||
assertEquals("1.3", CompilerConfiguration.getInstance(myProject).getBytecodeTargetLevel(getModule("project")));
|
||||
assertEquals("1.3", CompilerConfiguration.getInstance(myProject).getBytecodeTargetLevel(getModule("m1")));
|
||||
assertEquals("1.5", CompilerConfiguration.getInstance(myProject).getBytecodeTargetLevel(getModule("m2")));
|
||||
}
|
||||
|
||||
public void testProjectWithBuiltExtension() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user