IDEA-108202 Create maven configured workspace.
authorSergey Evdokimov <sergey.evdokimov@jetbrains.com>
Mon, 14 Oct 2013 17:06:37 +0000 (21:06 +0400)
committerSergey Evdokimov <sergey.evdokimov@jetbrains.com>
Mon, 14 Oct 2013 17:07:20 +0000 (21:07 +0400)
Support 'jdkName' property.

plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/configurers/MavenIdeaPluginConfigurer.java [new file with mode: 0644]
plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/configurers/MavenModuleConfigurer.java
plugins/maven/src/test/java/org/jetbrains/idea/maven/plugins/MavenIdeaPluginTest.groovy [new file with mode: 0644]

diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/configurers/MavenIdeaPluginConfigurer.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/configurers/MavenIdeaPluginConfigurer.java
new file mode 100644 (file)
index 0000000..d4fe0fd
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2000-2013 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.idea.maven.importing.configurers;
+
+import com.intellij.openapi.module.Module;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.projectRoots.JavaSdk;
+import com.intellij.openapi.projectRoots.ProjectJdkTable;
+import com.intellij.openapi.projectRoots.Sdk;
+import com.intellij.openapi.roots.ModifiableRootModel;
+import com.intellij.openapi.roots.ModuleRootManager;
+import com.intellij.openapi.roots.ProjectRootManager;
+import com.intellij.openapi.util.text.StringUtil;
+import org.jdom.Element;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.idea.maven.project.MavenProject;
+
+/**
+ * @author Sergey Evdokimov
+ */
+public class MavenIdeaPluginConfigurer extends MavenModuleConfigurer {
+  @Override
+  public void configure(@NotNull MavenProject mavenProject, @NotNull Project project, @Nullable Module module) {
+    if (module == null) return;
+
+    Element cfg = mavenProject.getPluginConfiguration("com.googlecode", "maven-idea-plugin");
+    if (cfg == null) return;
+
+
+    // Configure SDK name
+    configureJdk(cfg, module);
+  }
+
+  private static void configureJdk(Element cfg, @NotNull Module module) {
+    String jdkName = cfg.getChildTextTrim("jdkName");
+    if (StringUtil.isEmptyOrSpaces(jdkName)) return;
+
+    ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
+
+    String currentSdkName = null;
+    Sdk sdk = rootManager.getSdk();
+    if (sdk != null) {
+      currentSdkName = sdk.getName();
+    }
+
+    if (!jdkName.equals(currentSdkName)) {
+      ModifiableRootModel model = rootManager.getModifiableModel();
+
+      if (jdkName.equals(ProjectRootManager.getInstance(model.getProject()).getProjectSdkName())) {
+        model.inheritSdk();
+      }
+      else {
+        Sdk jdk = ProjectJdkTable.getInstance().findJdk(jdkName);
+        if (jdk != null) {
+          model.setSdk(jdk);
+        }
+        else {
+          model.setInvalidSdk(jdkName, JavaSdk.getInstance().getName());
+        }
+      }
+
+      model.commit();
+    }
+  }
+}
index eb60d8253c2c0155c4465672b45e6e4def820f0c..3834cc742ba950329b4cd90ce3dedb9442a66c68 100644 (file)
@@ -41,7 +41,10 @@ public abstract class MavenModuleConfigurer {
       for (MavenModuleConfigurer configurer : new MavenModuleConfigurer[]{
         new MavenCompilerConfigurer(),
         new MavenEncodingConfigurer(),
-        new MavenAnnotationProcessorConfigurer()}) {
+        new MavenAnnotationProcessorConfigurer(),
+
+        new MavenIdeaPluginConfigurer()
+      }) {
 
         if (!Boolean.parseBoolean(System.getProperty("idea.maven.disable." + configurer.getClass().getSimpleName()))) {
           configurers.add(configurer);
diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/plugins/MavenIdeaPluginTest.groovy b/plugins/maven/src/test/java/org/jetbrains/idea/maven/plugins/MavenIdeaPluginTest.groovy
new file mode 100644 (file)
index 0000000..8fddc39
--- /dev/null
@@ -0,0 +1,38 @@
+package org.jetbrains.idea.maven.plugins
+
+import com.intellij.openapi.roots.ModuleRootManager
+import org.jetbrains.idea.maven.dom.MavenDomTestCase
+
+/**
+ * @author Sergey Evdokimov
+ */
+class MavenIdeaPluginTest extends MavenDomTestCase {
+
+  public void testConfigureJdk() {
+    importProject("""
+<groupId>test</groupId>
+<artifactId>project</artifactId>
+<version>1</version>
+<build>
+  <plugins>
+    <plugin>
+        <groupId>com.googlecode</groupId>
+        <artifactId>maven-idea-plugin</artifactId>
+        <version>1.6.1</version>
+
+        <configuration>
+          <jdkName>invalidJdk</jdkName>
+        </configuration>
+    </plugin>
+  </plugins>
+</build>
+""")
+
+    def module = getModule("project")
+    assert module != null;
+
+    assert !ModuleRootManager.getInstance(module).sdkInherited
+    assert ModuleRootManager.getInstance(module).sdk == null
+  }
+
+}