Maven: update repository path on settings.xml file change
authorAnton.Makeev <Anton.Makeev@jetbrains.com>
Mon, 7 Dec 2009 13:26:51 +0000 (16:26 +0300)
committerAnton.Makeev <Anton.Makeev@jetbrains.com>
Mon, 7 Dec 2009 13:42:15 +0000 (16:42 +0300)
Maven: expand env veriables in repository path in setting.xml (IDEADEV-41873)

plugins/maven/src/main/java/org/jetbrains/idea/maven/embedder/MavenEmbedderFactory.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenGeneralSettings.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProjectsManagerWatcher.java
plugins/maven/src/main/resources/META-INF/plugin.xml
plugins/maven/src/test/java/org/jetbrains/idea/maven/project/MavenProjectsManagerSettingsXmlTest.java [deleted file]
plugins/maven/src/test/java/org/jetbrains/idea/maven/project/MavenProjectsManagerTest.java

index 7669d05283d20225c824aeb8c0dc2c2b09986e7a..2c2f57c72523b21f7f3c5c66209196dd4ae02434 100644 (file)
@@ -164,8 +164,19 @@ public class MavenEmbedderFactory {
     return new File(new File(userHome, DOT_M2_DIR), MavenConstants.SETTINGS_XML);
   }
 
-  @Nullable
+  @NotNull
   public static File resolveLocalRepository(@Nullable String mavenHome, @Nullable String userSettings, @Nullable String override) {
+    File result = doResolveLocalRepository(mavenHome, userSettings, override);
+    try {
+      return result.getCanonicalFile();
+    }
+    catch (IOException e) {
+      return result;
+    }
+  }
+
+  @NotNull
+  private static File doResolveLocalRepository(String mavenHome, String userSettings, String override) {
     if (!StringUtil.isEmpty(override)) {
       return new File(override);
     }
@@ -194,7 +205,7 @@ public class MavenEmbedderFactory {
       FileInputStream is = new FileInputStream(file);
       try {
         JDOMReader reader = new JDOMReader(is);
-        return reader.getChildText(reader.getRootElement(), LOCAL_REPOSITORY_TAG);
+        return expandProperties(reader.getChildText(reader.getRootElement(), LOCAL_REPOSITORY_TAG));
       }
       finally {
         is.close();
@@ -205,6 +216,15 @@ public class MavenEmbedderFactory {
     }
   }
 
+  private static String expandProperties(String text) {
+    if (StringUtil.isEmptyOrSpaces(text)) return text;
+    Properties props = collectSystemProperties();
+    for (Map.Entry<Object, Object> each : props.entrySet()) {
+      text = text.replace("${" + each.getKey() + "}", (CharSequence)each.getValue());
+    }
+    return text;
+  }
+
   public static List<String> getBasicPhasesList() {
     return Arrays.asList(basicPhases);
   }
index 4468d717de8df35c6df750b675ac60aad64f695c..d180cad050d794ed87a5c1f68fbe439a3b101475 100644 (file)
@@ -119,12 +119,16 @@ public class MavenGeneralSettings implements Cloneable {
       if (!Comparing.equal(this.localRepository, localRepository)) {
         this.localRepository = localRepository;
 
-        myEffectiveLocalRepositoryCache = null;
-        firePathChanged();
+        localRepositoryChanged();
       }
     }
   }
 
+  public void localRepositoryChanged() {
+    myEffectiveLocalRepositoryCache = null;
+    firePathChanged();
+  }
+
   @NotNull
   public String getMavenHome() {
     return mavenHome;
index e043840fe89911720241a967dbcb7ee72c45c491..e59e49ed661959dc0d66c121c206840828f54231 100644 (file)
@@ -232,6 +232,11 @@ public class MavenProjectsManagerWatcher {
     scheduleUpdateAll(true, false);
   }
 
+  private void onSettingsXmlChange() {
+    myGeneralSettings.localRepositoryChanged();
+    // onSettingsChange() will be called indirectly by pathsChanged listener on GeneralSettings object
+  }
+
   private class MyRootChangesListener implements ModuleRootListener {
     public void beforeRootsChange(ModuleRootEvent event) {
     }
@@ -332,7 +337,7 @@ public class MavenProjectsManagerWatcher {
       // can not be started since the window has already been closed.
       if (areFileSetsInitialised()) {
         if (settingsHaveChanged) {
-          onSettingsChange();
+          onSettingsXmlChange();
         }
         else {
           filesToUpdate.removeAll(filesToRemove);
index cff9e8d818d24f6a8e4f9134d0f3d2c16a94d57d..40cb2496fd522f1cf570b07dfa2c04064fcad23a 100644 (file)
       <keyboard-shortcut first-keystroke="DELETE" keymap="$default"/>
     </action>
 
-    <action id="Maven.OpenProfilesXml"
-            class="org.jetbrains.idea.maven.project.actions.OpenOrCreateProfilesXmlAction"
-            text="Open profiles.xml"
-            description="Open profiles.xml for selected projects"/>
-
     <action id="Maven.OpenSettingsXml"
             class="org.jetbrains.idea.maven.project.actions.OpenOrCreateSettingsXmlAction"
             text="Open settings.xml"
             description="Open Maven settings.xml"/>
 
+    <action id="Maven.OpenProfilesXml"
+            class="org.jetbrains.idea.maven.project.actions.OpenOrCreateProfilesXmlAction"
+            text="Open profiles.xml"
+            description="Open profiles.xml for selected projects"/>
+
     <action id="Maven.IgnoreProjects"
             class="org.jetbrains.idea.maven.project.actions.ToggleIgnoredProjectsAction"
             text="_Ignore Projects"
       <reference id="Maven.IgnoreProjects"/>
       <reference id="Maven.RemoveManagedFiles"/>
       <separator/>
-      <reference id="Maven.OpenProfilesXml"/>
       <reference id="Maven.OpenSettingsXml"/>
+      <reference id="Maven.OpenProfilesXml"/>
       <separator/>
     </group>
 
diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/project/MavenProjectsManagerSettingsXmlTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/project/MavenProjectsManagerSettingsXmlTest.java
deleted file mode 100644 (file)
index ede5b01..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright 2000-2009 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.project;
-
-import org.jetbrains.idea.maven.MavenImportingTestCase;
-
-public class MavenProjectsManagerSettingsXmlTest extends MavenImportingTestCase {
-  public void testUpdatingProjectsOnSettingsXmlCreationAndDeletion() throws Exception {
-    deleteSettingsXml();
-    initProjectsManager(true);
-    createProjectPom("<groupId>test</groupId>" +
-                     "<artifactId>project</artifactId>" +
-                     "<version>1</version>");
-
-    importProject();
-    assertUnorderedElementsAreEqual(myProjectsTree.getAvailableProfiles());
-
-    updateSettingsXml("<profiles>" +
-                      "  <profile>" +
-                      "    <id>one</id>" +
-                      "  </profile>" +
-                      "</profiles>");
-    waitForReadingCompletion();
-    assertUnorderedElementsAreEqual(myProjectsTree.getAvailableProfiles(), "one");
-
-    deleteSettingsXml();
-    waitForReadingCompletion();
-    assertUnorderedElementsAreEqual(myProjectsTree.getAvailableProfiles());
-  }
-}
\ No newline at end of file
index 245fc4539a1c6c4f5dca16eef280f43148093a6b..7d5fa24fa596aa8a0a71cd053350b0c2bd6ea2f2 100644 (file)
@@ -446,6 +446,69 @@ public class MavenProjectsManagerTest extends MavenImportingTestCase {
     assertUnorderedElementsAreEqual(childNode.getSources(), FileUtil.toSystemDependentName(getProjectPath() + "/m/value1"));
   }
 
+  public void testUpdatingProjectsOnSettingsXmlCreationAndDeletion() throws Exception {
+    deleteSettingsXml();
+    createProjectPom("<groupId>test</groupId>" +
+                     "<artifactId>project</artifactId>" +
+                     "<version>1</version>");
+
+    importProject();
+    assertUnorderedElementsAreEqual(myProjectsTree.getAvailableProfiles());
+
+    updateSettingsXml("<profiles>" +
+                      "  <profile>" +
+                      "    <id>one</id>" +
+                      "  </profile>" +
+                      "</profiles>");
+    waitForReadingCompletion();
+    assertUnorderedElementsAreEqual(myProjectsTree.getAvailableProfiles(), "one");
+
+    deleteSettingsXml();
+    waitForReadingCompletion();
+    assertUnorderedElementsAreEqual(myProjectsTree.getAvailableProfiles());
+  }
+
+  public void testUpdatingMavenPathsWhenSettingsChanges() throws Exception {
+    createProjectPom("<groupId>test</groupId>" +
+                     "<artifactId>project</artifactId>" +
+                     "<version>1</version>");
+
+    File repo1 = new File(myDir, "localRepo1");
+    updateSettingsXml("<localRepository>" + repo1.getPath() + "</localRepository>");
+
+    waitForReadingCompletion();
+    assertEquals(repo1, getMavenGeneralSettings().getEffectiveLocalRepository());
+
+    File repo2 = new File(myDir, "localRepo2");
+    updateSettingsXml("<localRepository>" + repo2.getPath() + "</localRepository>");
+
+    waitForReadingCompletion();
+    assertEquals(repo2, getMavenGeneralSettings().getEffectiveLocalRepository());
+  }
+
+  public void testResolvingEnvVariableInRepositoryPath() throws Exception {
+    String temp = System.getenv("TMP");
+    updateSettingsXml("<localRepository>${env.TEMP}/tmpRepo</localRepository>");
+
+    File repo = new File(temp + "/tmpRepo").getCanonicalFile();
+    assertEquals(repo.getPath(), getMavenGeneralSettings().getEffectiveLocalRepository().getPath());
+
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<dependencies>" +
+                  "  <dependency>" +
+                  "    <groupId>junit</groupId>" +
+                  "    <artifactId>junit</artifactId>" +
+                  "    <version>4.0</version>" +
+                  "  </dependency>" +
+                  "</dependencies>");
+    
+    assertModuleLibDep("project", "Maven: junit:junit:4.0",
+                       "jar://" + FileUtil.toSystemIndependentName(repo.getPath()) + "/junit/junit/4.0/junit-4.0.jar!/");
+  }
+
   public void testUpdatingProjectsOnProfilesXmlChange() throws Exception {
     createProjectPom("<groupId>test</groupId>" +
                      "<artifactId>project</artifactId>" +