Maven: groovy importer in CE
authorAnton.Makeev <Anton.Makeev@jetbrains.com>
Thu, 1 Oct 2009 13:39:07 +0000 (17:39 +0400)
committerAnton.Makeev <Anton.Makeev@jetbrains.com>
Mon, 12 Oct 2009 11:54:04 +0000 (15:54 +0400)
plugins/maven/maven.iml
plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/GroovyImporter.java [new file with mode: 0644]
plugins/maven/src/main/resources/META-INF/groovy-support.xml [new file with mode: 0644]
plugins/maven/src/main/resources/META-INF/plugin.xml
plugins/maven/src/test/java/org/jetbrains/idea/maven/GroovyImporterTest.java [new file with mode: 0644]

index 3ae593a8f389e3132c7ec1838838cb35f22f9532..6491dcc186f881ae02e7eb435496ff7d4d13c1fd 100644 (file)
     <orderEntry type="library" name="jgoodies-forms" level="project" />
     <orderEntry type="library" exported="" scope="TEST" name="JUnit4" level="project" />
     <orderEntry type="module" module-name="testFramework-java" exported="" scope="TEST" />
+    <orderEntry type="module" module-name="jetgroovy" scope="TEST" />
     <orderEntry type="module" module-name="java-impl" />
   </component>
   <component name="copyright">
diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/GroovyImporter.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/GroovyImporter.java
new file mode 100644 (file)
index 0000000..091a959
--- /dev/null
@@ -0,0 +1,84 @@
+package org.jetbrains.idea.maven.importing;
+
+import com.intellij.openapi.module.Module;
+import org.jdom.Element;
+import org.jetbrains.idea.maven.project.*;
+
+import java.util.List;
+import java.util.Map;
+
+public class GroovyImporter extends MavenImporter {
+  private static final String ourPluginGroupID = "org.codehaus.groovy.maven";
+  private static final String ourPluginArtifactID = "gmaven-plugin";
+
+  public boolean isApplicable(MavenProject project) {
+    return project.findPlugin(ourPluginGroupID, ourPluginArtifactID) != null;
+  }
+
+  @Override
+  public boolean isSupportedDependency(MavenArtifact artifact) {
+    return false;
+  }
+
+  @Override
+  public void preProcess(Module module, MavenProject mavenProject, MavenProjectChanges changes, MavenModifiableModelsProvider modifiableModelsProvider) {
+  }
+
+  @Override
+  public void process(MavenModifiableModelsProvider modifiableModelsProvider, Module module, MavenRootModelAdapter rootModel,
+                      MavenProjectsTree mavenModel, MavenProject mavenProject, MavenProjectChanges changes, Map<MavenProject, String> mavenProjectToModuleName,
+                      List<MavenProjectsProcessorTask> postTasks) {
+  }
+
+  @Override
+  public void collectSourceFolders(MavenProject mavenProject, List<String> result) {
+    collectSourceOrTestFolders(mavenProject, "compile", "src/main/groovy", result);
+  }
+
+  @Override
+  public void collectTestFolders(MavenProject mavenProject, List<String> result) {
+    collectSourceOrTestFolders(mavenProject, "testCompile", "src/test/groovy", result);
+  }
+
+  protected static Element findGoalConfigNode(MavenProject p, String goal, String path) {
+    return p.findPluginGoalConfigurationElement(ourPluginGroupID, ourPluginArtifactID, goal, path);
+  }
+
+
+  private void collectSourceOrTestFolders(MavenProject mavenProject, String goal, String defaultDir, List<String> result) {
+    Element sourcesElement = findGoalConfigNode(mavenProject, goal, "sources");
+    if (sourcesElement == null) {
+      result.add(mavenProject.getDirectory() + "/" + defaultDir);
+      return;
+    }
+
+    for (Element each : (Iterable<? extends Element>)sourcesElement.getChildren("fileset")) {
+      String dir = findChildElementValue(each, "directory", null);
+      if (dir == null) continue;
+      result.add(dir);
+    }
+  }
+
+  private static String findChildElementValue(Element parent, String childName, String defaultValue) {
+    if (parent == null) return defaultValue;
+    Element child = parent.getChild(childName);
+    return child == null ? defaultValue : child.getValue();
+  }
+
+
+  private static String findGoalConfigValue(MavenProject p, String goal, String path) {
+    return p.findPluginGoalConfigurationValue(ourPluginGroupID, ourPluginArtifactID, goal, path);
+  }
+
+  @Override
+  public void collectExcludedFolders(MavenProject mavenProject, List<String> result) {
+    String stubsDir = findGoalConfigValue(mavenProject, "generateStubs", "outputDirectory");
+    String testStubsDir = findGoalConfigValue(mavenProject, "generateTestStubs", "outputDirectory");
+
+    // exclude common parent of /groovy-stubs/main and /groovy-stubs/test
+    String defaultStubsDir = mavenProject.getGeneratedSourcesDirectory() + "/groovy-stubs";
+
+    result.add(stubsDir == null ? defaultStubsDir : stubsDir);
+    result.add(testStubsDir == null ? defaultStubsDir : testStubsDir);
+  }
+}
diff --git a/plugins/maven/src/main/resources/META-INF/groovy-support.xml b/plugins/maven/src/main/resources/META-INF/groovy-support.xml
new file mode 100644 (file)
index 0000000..3c91b4f
--- /dev/null
@@ -0,0 +1,6 @@
+<!DOCTYPE idea-plugin PUBLIC "Plugin/DTD" "http://plugins.intellij.net/plugin.dtd">
+<idea-plugin url="http://www.jetbrains.com/idea">
+  <extensions xmlns="org.jetbrains.idea.maven">
+    <importer implementation="org.jetbrains.idea.maven.importing.GroovyImporter"/>
+  </extensions>
+</idea-plugin>
\ No newline at end of file
index 17513a4613bfbb9a9f57f07490858d25bc311407..38309d9df6edcc3d283a45ef96c02cee687f923c 100644 (file)
@@ -11,6 +11,7 @@
   </extensionPoints>
 
   <depends>com.intellij.properties</depends>
+  <depends optional="true" config-file="groovy-support.xml">org.intellij.groovy</depends>
 
   <extensions defaultExtensionNs="com.intellij">
     <projectImportProvider implementation="org.jetbrains.idea.maven.wizards.MavenProjectImportProvider"/>
diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/GroovyImporterTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/GroovyImporterTest.java
new file mode 100644 (file)
index 0000000..e1fd98c
--- /dev/null
@@ -0,0 +1,418 @@
+/*
+ * Copyright (c) 2000-2007 JetBrains s.r.o. All Rights Reserved.
+ */
+
+package org.jetbrains.idea.maven;
+
+import com.intellij.openapi.roots.OrderRootType;
+import com.intellij.openapi.roots.libraries.Library;
+import org.jetbrains.idea.maven.importing.MavenDefaultModifiableModelsProvider;
+import org.jetbrains.idea.maven.importing.MavenRootModelAdapter;
+import org.jetbrains.plugins.groovy.config.GroovyConfigUtils;
+
+import java.io.File;
+
+public class GroovyImporterTest extends MavenImportingTestCase {
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+    setRepositoryPath(new File(myDir, "repo").getPath());
+  }
+
+  public void testConfiguringFacetWithoutLibrary() throws Exception {
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <groupId>org.codehaus.groovy.maven</groupId>" +
+                  "      <artifactId>gmaven-plugin</artifactId>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    assertModules("project");
+
+    assertUnorderedElementsAreEqual(GroovyConfigUtils.getInstance().getSDKLibrariesByModule(getModule("project")));
+  }
+
+  public void testConfiguringFacetWithLibrary() throws Exception {
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<dependencies>" +
+                  "  <dependency>" +
+                  "    <groupId>org.codehaus.groovy.maven.runtime</groupId>" +
+                  "    <artifactId>gmaven-runtime-default</artifactId>" +
+                  "    <version>1.0-rc-1</version>" +
+                  "  </dependency>" +
+                  "</dependencies>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <groupId>org.codehaus.groovy.maven</groupId>" +
+                  "      <artifactId>gmaven-plugin</artifactId>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    assertModules("project");
+
+    Library[] libraries = GroovyConfigUtils.getInstance().getSDKLibrariesByModule(getModule("project"));
+    assertTrue("unexpected groovy libs configuration: " + libraries.length, libraries.length > 0);
+    Library library = libraries[0];
+    assertUnorderedElementsAreEqual(
+      library.getUrls(OrderRootType.CLASSES),
+      "jar://" + getRepositoryPath() + "/org/codehaus/groovy/groovy-all-minimal/1.5.6/groovy-all-minimal-1.5.6.jar!/");
+  }
+
+  public void testAddingGroovySpecificSources() throws Exception {
+    createStdProjectFolders();
+    createProjectSubDirs("src/main/groovy",
+                         "src/test/groovy");
+
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <groupId>org.codehaus.groovy.maven</groupId>" +
+                  "      <artifactId>gmaven-plugin</artifactId>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    assertModules("project");
+
+    assertSources("project",
+                  "src/main/java",
+                  "src/main/resources",
+                  "src/main/groovy");
+    assertTestSources("project",
+                      "src/test/java",
+                      "src/test/resources",
+                      "src/test/groovy");
+  }
+
+  public void testAddingCustomGroovySpecificSources() throws Exception {
+    createStdProjectFolders();
+    createProjectSubDirs("src/main/groovy",
+                         "src/foo1",
+                         "src/foo2",
+                         "src/test/groovy",
+                         "src/test-foo1",
+                         "src/test-foo2");
+
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <groupId>org.codehaus.groovy.maven</groupId>" +
+                  "      <artifactId>gmaven-plugin</artifactId>" +
+                  "      <executions>" +
+                  "        <execution>" +
+                  "          <id>one</id>" +
+                  "          <goals>" +
+                  "            <goal>compile</goal>" +
+                  "          </goals>" +
+                  "          <configuration>" +
+                  "            <sources>" +
+                  "              <fileset>" +
+                  "                <directory>${pom.basedir}/src/foo1</directory>" +
+                  "              </fileset>" +
+                  "              <fileset>" +
+                  "                <directory>${pom.basedir}/src/foo2</directory>" +
+                  "              </fileset>" +
+                  "            </sources>" +
+                  "          </configuration>" +
+                  "        </execution>" +
+                  "        <execution>" +
+                  "          <id>two</id>" +
+                  "          <goals>" +
+                  "            <goal>testCompile</goal>" +
+                  "          </goals>" +
+                  "          <configuration>" +
+                  "            <sources>" +
+                  "              <fileset>" +
+                  "                <directory>${pom.basedir}/src/test-foo1</directory>" +
+                  "              </fileset>" +
+                  "              <fileset>" +
+                  "                <directory>${pom.basedir}/src/test-foo2</directory>" +
+                  "              </fileset>" +
+                  "            </sources>" +
+                  "          </configuration>" +
+                  "        </execution>" +
+                  "      </executions>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    assertModules("project");
+
+    assertSources("project",
+                  "src/main/java",
+                  "src/main/resources",
+                  "src/foo1",
+                  "src/foo2");
+    assertTestSources("project",
+                      "src/test/java",
+                      "src/test/resources",
+                      "src/test-foo1",
+                      "src/test-foo2");
+  }
+
+  public void testAddingCustomGroovySpecificSourcesByRelativePath() throws Exception {
+    createProjectSubDirs("src/foo",
+                         "src/test-foo");
+
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <groupId>org.codehaus.groovy.maven</groupId>" +
+                  "      <artifactId>gmaven-plugin</artifactId>" +
+                  "      <executions>" +
+                  "        <execution>" +
+                  "          <id>one</id>" +
+                  "          <goals>" +
+                  "            <goal>compile</goal>" +
+                  "          </goals>" +
+                  "          <configuration>" +
+                  "            <sources>" +
+                  "              <fileset>" +
+                  "                <directory>src/foo</directory>" +
+                  "              </fileset>" +
+                  "            </sources>" +
+                  "          </configuration>" +
+                  "        </execution>" +
+                  "        <execution>" +
+                  "          <id>two</id>" +
+                  "          <goals>" +
+                  "            <goal>testCompile</goal>" +
+                  "          </goals>" +
+                  "          <configuration>" +
+                  "            <sources>" +
+                  "              <fileset>" +
+                  "                <directory>src/test-foo</directory>" +
+                  "              </fileset>" +
+                  "            </sources>" +
+                  "          </configuration>" +
+                  "        </execution>" +
+                  "      </executions>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    assertModules("project");
+
+    assertSources("project", "src/foo");
+    assertTestSources("project", "src/test-foo");
+  }
+
+  public void testDoNotAddGroovySpecificGeneratedSources() throws Exception {
+    createStdProjectFolders();
+    createProjectSubDirs("target/generated-sources/xxx/yyy",
+                         "target/generated-sources/groovy-stubs/main/foo",
+                         "target/generated-sources/groovy-stubs/test/bar");
+
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <groupId>org.codehaus.groovy.maven</groupId>" +
+                  "      <artifactId>gmaven-plugin</artifactId>" +
+                  "      <executions>" +
+                  "        <execution>" +
+                  "          <goals>" +
+                  "            <goal>generateStubs</goal>" +
+                  "            <goal>compile</goal>" +
+                  "            <goal>generateTestStubs</goal>" +
+                  "            <goal>testCompile</goal>" +
+                  "          </goals>" +
+                  "        </execution>" +
+                  "      </executions>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    assertModules("project");
+
+    assertSources("project",
+                  "src/main/java",
+                  "src/main/resources",
+                  "target/generated-sources/xxx");
+    assertTestSources("project",
+                      "src/test/java",
+                      "src/test/resources");
+
+    assertExcludes("project",
+                   "target/generated-sources/groovy-stubs");
+  }
+
+  public void testDoNotAddCustomGroovySpecificGeneratedSources() throws Exception {
+    createStdProjectFolders();
+    createProjectSubDirs("target/generated-sources/xxx/yyy",
+                         "target/generated-sources/foo/aaa",
+                         "target/generated-sources/bar/bbb");
+
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <groupId>org.codehaus.groovy.maven</groupId>" +
+                  "      <artifactId>gmaven-plugin</artifactId>" +
+                  "      <executions>" +
+                  "        <execution>" +
+                  "          <id>one</id>" +
+                  "          <goals>" +
+                  "            <goal>generateStubs</goal>" +
+                  "          </goals>" +
+                  "          <configuration>" +
+                  "            <outputDirectory>${project.build.directory}/generated-sources/foo</outputDirectory>" +
+                  "          </configuration>" +
+                  "        </execution>" +
+                  "        <execution>" +
+                  "          <id>two</id>" +
+                  "          <goals>" +
+                  "            <goal>generateTestStubs</goal>" +
+                  "          </goals>" +
+                  "          <configuration>" +
+                  "            <outputDirectory>${project.build.directory}/generated-sources/bar</outputDirectory>" +
+                  "          </configuration>" +
+                  "        </execution>" +
+                  "      </executions>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    assertModules("project");
+
+    assertSources("project",
+                  "src/main/java",
+                  "src/main/resources",
+                  "target/generated-sources/xxx");
+    assertTestSources("project",
+                      "src/test/java",
+                      "src/test/resources");
+
+    assertExcludes("project",
+                   "target/generated-sources/foo",
+                   "target/generated-sources/bar");
+  }
+
+  public void testDoNotAddCustomGroovySpecificGeneratedSourcesByRelativePath() throws Exception {
+    createProjectSubDirs("target/generated-sources/xxx/yyy",
+                         "target/generated-sources/foo/aaa",
+                         "target/generated-sources/bar/bbb");
+
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <groupId>org.codehaus.groovy.maven</groupId>" +
+                  "      <artifactId>gmaven-plugin</artifactId>" +
+                  "      <executions>" +
+                  "        <execution>" +
+                  "          <id>one</id>" +
+                  "          <goals>" +
+                  "            <goal>generateStubs</goal>" +
+                  "          </goals>" +
+                  "          <configuration>" +
+                  "            <outputDirectory>target/generated-sources/foo</outputDirectory>" +
+                  "          </configuration>" +
+                  "        </execution>" +
+                  "        <execution>" +
+                  "          <id>two</id>" +
+                  "          <goals>" +
+                  "            <goal>generateTestStubs</goal>" +
+                  "          </goals>" +
+                  "          <configuration>" +
+                  "            <outputDirectory>target/generated-sources/bar</outputDirectory>" +
+                  "          </configuration>" +
+                  "        </execution>" +
+                  "      </executions>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    assertModules("project");
+
+    assertSources("project",
+                  "target/generated-sources/xxx");
+    assertTestSources("project");
+
+    assertExcludes("project",
+                   "target/generated-sources/foo",
+                   "target/generated-sources/bar");
+  }
+
+  public void testUpdatingGroovySpecificGeneratedSourcesOnFoldersUpdate() throws Exception {
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <groupId>org.codehaus.groovy.maven</groupId>" +
+                  "      <artifactId>gmaven-plugin</artifactId>" +
+                  "      <executions>" +
+                  "        <execution>" +
+                  "          <goals>" +
+                  "            <goal>generateStubs</goal>" +
+                  "            <goal>generateTestStubs</goal>" +
+                  "          </goals>" +
+                  "        </execution>" +
+                  "      </executions>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    MavenRootModelAdapter a = new MavenRootModelAdapter(myProjectsTree.findProject(myProjectPom),
+                                                        getModule("project"),
+                                                        new MavenDefaultModifiableModelsProvider(myProject));
+    a.unregisterAll(getProjectPath() + "/target", true, true);
+    a.getRootModel().commit();
+
+    assertSources("project");
+    assertTestSources("project");
+    assertExcludes("project");
+
+    createProjectSubDirs("src/main/groovy",
+                         "src/test/groovy",
+                         "target/generated-sources/xxx/yyy",
+                         "target/generated-sources/groovy-stubs/main/foo",
+                         "target/generated-sources/groovy-stubs/test/bar");
+
+    resolveFoldersAndImport();
+
+    assertSources("project",
+                  "src/main/groovy",
+                  "target/generated-sources/xxx");
+    assertTestSources("project",
+                      "src/test/groovy");
+    assertExcludes("project",
+                   "target/generated-sources/groovy-stubs");
+  }
+}
\ No newline at end of file