Skip to content

Commit d03b61d

Browse files
authored
[MNG-8713] SourceRoot.directory() default value should include the module when present (#2278)
The default directory is `src/${module}/${scope}/${lang}`. When no module is defined, the previous default is unchanged.
1 parent ed2a20f commit d03b61d

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,26 @@ public interface SourceRoot {
3939
* The path is relative to the <abbr>POM</abbr> file.
4040
*
4141
* <h4>Default implementation</h4>
42-
* The default value is <code>src/{@linkplain #scope() scope}/{@linkplain #language() language}</code>
43-
* as a relative path. Implementation classes may override this default with an absolute path instead.
42+
* The default value depends on whether a {@linkplain #module() module name} is specified in this source root:
43+
* <ul>
44+
* <li>
45+
* If no module name, then the default directory is
46+
* <code>src/{@linkplain #scope() scope}/{@linkplain #language() language}</code>.
47+
* </li><li>
48+
* If a module name is present, then the default directory is
49+
* <code>src/{@linkplain #module() module}/{@linkplain #scope() scope}/{@linkplain #language() language}</code>.
50+
* </li>
51+
* </ul>
52+
*
53+
* The default value is relative.
54+
* Implementation may override with absolute path instead.
4455
*/
4556
default Path directory() {
46-
return Path.of("src", scope().id(), language().id());
57+
Path src = Path.of("src");
58+
return module().map(src::resolve)
59+
.orElse(src)
60+
.resolve(scope().id())
61+
.resolve(language().id());
4762
}
4863

4964
/**

api/maven-api-model/src/main/mdo/maven.mdo

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,9 +2014,15 @@
20142014
usage (main code, tests, <i>etc.</i>) is specified by the {@code scope} element.
20152015
20162016
<h2>Default source directories</h2>
2017-
If no source directories are specified, the defaults are {@code src/${scope}/${lang}} where
2018-
{@code ${scope}} is the value of the {@link #scope} element (typically {@code main} or {@code test}) and
2019-
{@code ${lang}} is the value of the {@link #lang} element (typically {@code java} or {@code resources}).
2017+
If no source directories are specified, the default values depend on whether module names are specified:
2018+
<ul>
2019+
<li>{@code src/${scope}/${lang}} if no module is specified</li>
2020+
<li>{@code src/${module}/${scope}/${lang}} if a module is specified</li>
2021+
</ul>
2022+
where
2023+
{@code ${scope}} is the value of the {@link #scope} element (typically {@code main} or {@code test}),
2024+
{@code ${lang}} is the value of the {@link #lang} element (typically {@code java} or {@code resources}),
2025+
and {@code ${module}} is the optional {@link #module} element.
20202026
]]>
20212027
</description>
20222028
<version>4.1.0+</version>

impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ public DefaultSourceRoot(final Session session, final Path baseDir, final Source
8181
if (value != null) {
8282
directory = baseDir.resolve(value);
8383
} else {
84-
directory = baseDir.resolve("src").resolve(scope.id()).resolve(language.id());
84+
Path src = baseDir.resolve("src");
85+
if (moduleName != null) {
86+
src = src.resolve(language.id());
87+
}
88+
directory = src.resolve(scope.id()).resolve(language.id());
8589
}
8690

8791
value = nonBlank(source.getTargetVersion());

0 commit comments

Comments
 (0)