diff --git a/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java b/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java index 034831aac..f0251e19d 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java +++ b/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java @@ -475,7 +475,7 @@ default String getMavenArgs() { } /** - * @return the String value for the variable M2_REPO, or null if called outside an IDEasy installation. + * @return the String value for the variable M2_REPO, or falls back to the default USER_HOME/.m2 location if called outside an IDEasy installation. */ default Path getMavenRepository() { @@ -486,6 +486,9 @@ default Path getMavenRepository() { Path m2LegacyFolder = confPath.resolve(Mvn.MVN_CONFIG_LEGACY_FOLDER); if (Files.isDirectory(m2LegacyFolder)) { m2Folder = m2LegacyFolder; + } else { + // fallback to USER_HOME/.m2 folder + m2Folder = getUserHome().resolve(Mvn.MVN_CONFIG_LEGACY_FOLDER); } } return m2Folder.resolve("repository"); diff --git a/cli/src/test/java/com/devonfw/tools/ide/tool/mvn/MvnTest.java b/cli/src/test/java/com/devonfw/tools/ide/tool/mvn/MvnTest.java index 0837cec15..3591f11c1 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/tool/mvn/MvnTest.java +++ b/cli/src/test/java/com/devonfw/tools/ide/tool/mvn/MvnTest.java @@ -13,6 +13,7 @@ import com.devonfw.tools.ide.commandlet.InstallCommandlet; import com.devonfw.tools.ide.context.AbstractIdeContextTest; import com.devonfw.tools.ide.context.IdeTestContext; +import com.devonfw.tools.ide.variable.IdeVariables; /** * Integration test of {@link Mvn}. @@ -85,6 +86,22 @@ private void checkInstallation(IdeTestContext context) throws IOException { assertFileContent(settingsSecurityFile, List.of("masterPassword")); } + /** + * Tests if the user is starting IDEasy without a Maven repository, IDEasy should fall back to USER_HOME/.m2/repository. + *

+ * See: #463 + */ + @Test + public void testMavenRepositoryPathFallsBackToUserHome() { + // arrange + String path = "project/workspaces"; + // act + IdeTestContext context = newContext(PROJECT_MVN, path, false); + Path mavenRepository = context.getUserHome().resolve(".m2").resolve("repository"); + // assert + assertThat(IdeVariables.M2_REPO.get(context)).isEqualTo(mavenRepository); + } + private void assertFileContent(Path filePath, List expectedValues) throws IOException { String content = new String(Files.readAllBytes(filePath)); diff --git a/cli/src/test/resources/ide-projects/mvn/project/home/environment.properties b/cli/src/test/resources/ide-projects/mvn/project/home/environment.properties new file mode 100644 index 000000000..c0a59da37 --- /dev/null +++ b/cli/src/test/resources/ide-projects/mvn/project/home/environment.properties @@ -0,0 +1,2 @@ +# if this file is present, it will replace and mock System.getenv in the text context +PATH=${IDE_ROOT}/_ide/bin