This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new ec98e0dd13 [MNG-8662] Add missing methods for removing project source 
roots (#2205)
ec98e0dd13 is described below

commit ec98e0dd13731fcdc8c409ce2ca8e4047401d4a6
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Wed Apr 2 18:09:08 2025 +0200

    [MNG-8662] Add missing methods for removing project source roots (#2205)
---
 .../org/apache/maven/project/MavenProject.java     | 65 +++++++++++++++++++---
 1 file changed, 56 insertions(+), 9 deletions(-)

diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java 
b/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java
index b0759586f0..5e7e48059f 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java
@@ -42,6 +42,7 @@
 import org.apache.maven.api.Language;
 import org.apache.maven.api.ProjectScope;
 import org.apache.maven.api.SourceRoot;
+import org.apache.maven.api.annotations.Nonnull;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.ArtifactUtils;
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
@@ -343,8 +344,10 @@ public void addSourceRoot(SourceRoot source) {
      *
      * @since 4.0.0
      */
-    public void addSourceRoot(ProjectScope scope, Language language, Path 
directory) {
-        directory = getBaseDirectory().resolve(directory).normalize();
+    public void addSourceRoot(@Nonnull ProjectScope scope, @Nonnull Language 
language, @Nonnull Path directory) {
+        directory = getBaseDirectory()
+                .resolve(Objects.requireNonNull(directory, "directory cannot 
be null"))
+                .normalize();
         addSourceRoot(new DefaultSourceRoot(scope, language, directory));
     }
 
@@ -360,13 +363,43 @@ public void addSourceRoot(ProjectScope scope, Language 
language, Path directory)
      *
      * @since 4.0.0
      */
-    public void addSourceRoot(ProjectScope scope, Language language, String 
directory) {
-        if (directory != null) {
-            directory = directory.trim();
-            if (!directory.isBlank()) {
-                Path path = getBaseDirectory().resolve(directory).normalize();
-                addSourceRoot(scope, language, path);
-            }
+    public void addSourceRoot(@Nonnull ProjectScope scope, @Nonnull Language 
language, @Nonnull String directory) {
+        directory =
+                Objects.requireNonNull(directory, "directory cannot be 
null").trim();
+        if (!directory.isBlank()) {
+            Path path = getBaseDirectory().resolve(directory).normalize();
+            addSourceRoot(scope, language, path);
+        }
+    }
+
+    /**
+     * Removes a source root from the project.
+     *
+     * @param scope the scope of the source root
+     * @param language the language of the source root
+     * @param directory the directory of the source root
+     */
+    public void removeSourceRoot(@Nonnull ProjectScope scope, @Nonnull 
Language language, @Nonnull Path directory) {
+        Path path = getBaseDirectory()
+                .resolve(Objects.requireNonNull(directory, "directory cannot 
be null"))
+                .normalize();
+        sources.removeIf(source -> source.scope() == scope
+                && source.language() == language
+                && source.directory().equals(path));
+    }
+
+    /**
+     * Removes a source root from the project.
+     *
+     * @param scope the scope of the source root
+     * @param language the language of the source root
+     * @param directory the directory of the source root
+     */
+    public void removeSourceRoot(@Nonnull ProjectScope scope, @Nonnull 
Language language, @Nonnull String directory) {
+        directory =
+                Objects.requireNonNull(directory, "directory cannot be 
null").trim();
+        if (!directory.isBlank()) {
+            removeSourceRoot(scope, language, Path.of(directory));
         }
     }
 
@@ -378,6 +411,14 @@ public void addCompileSourceRoot(String path) {
         addSourceRoot(ProjectScope.MAIN, Language.JAVA_FAMILY, path);
     }
 
+    /**
+     * @deprecated Replaced by {@code removeSourceRoot(ProjectScope.MAIN, 
Language.JAVA_FAMILY, path)}.
+     */
+    @Deprecated(since = "4.0.0")
+    public void removeCompileSourceRoot(String path) {
+        removeSourceRoot(ProjectScope.MAIN, Language.JAVA_FAMILY, path);
+    }
+
     /**
      * @deprecated Replaced by {@code addSourceRoot(ProjectScope.TEST, 
Language.JAVA_FAMILY, path)}.
      */
@@ -386,6 +427,12 @@ public void addTestCompileSourceRoot(String path) {
         addSourceRoot(ProjectScope.TEST, Language.JAVA_FAMILY, path);
     }
 
+    /**
+     * @deprecated Replaced by {@code removeSourceRoot(ProjectScope.TEST, 
Language.JAVA_FAMILY, path)}.
+     */
+    public void removeTestCompileSourceRoot(String path) {
+        removeSourceRoot(ProjectScope.TEST, Language.JAVA_FAMILY, path);
+    }
     /**
      * {@return all source root directories, including the disabled ones, for 
all languages and scopes}.
      * The iteration order is the order in which the sources are declared in 
the POM file.

Reply via email to