slawekjaranowski commented on code in PR #441:
URL: https://github.com/apache/maven-help-plugin/pull/441#discussion_r4516400141
##########
src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java:
##########
@@ -146,12 +147,36 @@ private void addProjectPomProfiles(
getLog().debug("Attempting to read profiles from pom.xml...");
+ // Keep the current project's activation context while walking its
parent models.
+ Map<String, List<String>> injectedProfileIds =
project.getInjectedProfileIds();
+ if (project.getActiveProfiles() != null) {
+ for (Profile profile : project.getActiveProfiles()) {
+ activeProfiles.put(profile.getId(), profile);
+ }
+ }
+
while (project != null) {
+ String modelId = project.getGroupId() + ":" +
project.getArtifactId() + ":" + project.getVersion();
+ List<String> activeProfileIds = injectedProfileIds.get(modelId);
+ Model originalModel = project.getOriginalModel();
+ if (activeProfileIds == null && originalModel != null) {
+ // Model building can retain uninterpolated coordinates for
more distant ancestors.
+ String groupId = originalModel.getGroupId();
+ String version = originalModel.getVersion();
+ if (originalModel.getParent() != null) {
+ if (groupId == null) {
+ groupId = originalModel.getParent().getGroupId();
+ }
+ if (version == null) {
+ version = originalModel.getParent().getVersion();
+ }
+ }
+ modelId = groupId + ":" + originalModel.getArtifactId() + ":"
+ version;
+ activeProfileIds = injectedProfileIds.get(modelId);
+ }
for (Profile profile : project.getModel().getProfiles()) {
allProfiles.put(profile.getId(), profile);
- }
- if (project.getActiveProfiles() != null) {
- for (Profile profile : project.getActiveProfiles()) {
+ if (activeProfileIds != null &&
activeProfileIds.contains(profile.getId())) {
Review Comment:
Rebuilding the model ID (interpolated coordinates, `originalModel` fallback,
inherited groupId/version, `${revision}`) is fragile and needs three dedicated
tests. Since `allProfiles` / `activeProfiles` are keyed by profile ID anyway,
matching per model does not add precision to the output — checking against all
injected profile IDs is enough.
```suggestion
for (Profile profile : project.getModel().getProfiles()) {
allProfiles.put(profile.getId(), profile);
if (injectedProfileIds.contains(profile.getId())) {
```
##########
src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java:
##########
@@ -146,12 +147,36 @@ private void addProjectPomProfiles(
getLog().debug("Attempting to read profiles from pom.xml...");
+ // Keep the current project's activation context while walking its
parent models.
+ Map<String, List<String>> injectedProfileIds =
project.getInjectedProfileIds();
Review Comment:
```suggestion
Set<String> injectedProfileIds = new HashSet<>();
for (List<String> profileIds :
project.getInjectedProfileIds().values()) {
injectedProfileIds.addAll(profileIds);
}
```
##########
src/test/java/org/apache/maven/plugins/help/AllProfilesMojoTest.java:
##########
@@ -141,25 +147,151 @@ void testProfileFromPom(AllProfilesMojo mojo) throws
Exception {
}
/**
- * Tests the case when active profiles are present in the parent POM.
+ * Tests that a parent profile injected into the current project is active.
*
* @throws Exception in case of errors.
*/
@Test
@InjectMojo(goal = "all-profiles")
@MojoParameter(name = "output", value = "${outputPath}")
- void testProfileFromParentPom(AllProfilesMojo mojo) throws Exception {
- Model parentModel = mock(Model.class);
-
when(parentModel.getProfiles()).thenReturn(Collections.singletonList(newPomProfile("pro-1",
"pom")));
- MavenProject parentProject = mock(MavenProject.class);
- when(parentProject.getModel()).thenReturn(parentModel);
-
when(parentProject.getActiveProfiles()).thenReturn(Collections.singletonList(newPomProfile("pro-1",
"pom")));
+ void testInjectedProfileFromParentPom(AllProfilesMojo mojo) throws
Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put("org.apache.maven.its.help:parent:1.0",
Collections.singletonList("pro-1"));
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ }
+
+ /**
+ * Tests that inherited group and version are used in an injected
profile's model identifier.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void testInjectedProfileWithInheritedCoordinates(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ Parent parent = new Parent();
+ parent.setGroupId("org.apache.maven.its.help");
+ parent.setArtifactId("grandparent");
+ parent.setVersion("1.0");
+ parentProject.getModel().setParent(parent);
+ parentProject.getModel().setGroupId(null);
+ parentProject.getModel().setVersion(null);
when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put("org.apache.maven.its.help:parent:1.0",
Collections.singletonList("pro-1"));
mojo.execute();
String file = readOutput();
assertTrue(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ }
+
+ /**
+ * Tests that an ancestor can be identified by its uninterpolated version.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void testInjectedProfileWithUninterpolatedVersion(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ Model originalModel = parentProject.getModel().clone();
+ Parent parent = new Parent();
+ parent.setGroupId("org.apache.maven.its.help");
+ parent.setArtifactId("grandparent");
+ parent.setVersion("${revision}");
+ originalModel.setParent(parent);
+ originalModel.setGroupId(null);
+ originalModel.setVersion(null);
+ parentProject.setOriginalModel(originalModel);
+ when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put(
+ "org.apache.maven.its.help:parent:${revision}",
Collections.singletonList("pro-1"));
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ }
+
+ /**
+ * Tests that an explicitly empty entry takes precedence over
uninterpolated coordinates.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void
testEmptyInjectedProfilesDoNotUseUninterpolatedVersion(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ Model originalModel = parentProject.getModel().clone();
+ originalModel.setVersion("${revision}");
+ parentProject.setOriginalModel(originalModel);
+ when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put("org.apache.maven.its.help:parent:1.0",
Collections.emptyList());
+ projectInjectedProfileIds.put(
+ "org.apache.maven.its.help:parent:${revision}",
Collections.singletonList("pro-1"));
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ }
+
Review Comment:
These three tests cover only the model-ID fallbacks, which go away with the
simplification.
```suggestion
```
##########
src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java:
##########
@@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
+import org.apache.maven.model.Model;
Review Comment:
Imports for the simplification below (`Model` is no longer needed).
```suggestion
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
```
##########
src/test/java/org/apache/maven/plugins/help/AllProfilesMojoTest.java:
##########
@@ -25,14 +25,17 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import org.apache.maven.api.di.Provides;
import org.apache.maven.api.plugin.testing.InjectMojo;
import org.apache.maven.api.plugin.testing.MojoParameter;
import org.apache.maven.api.plugin.testing.MojoTest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
+import org.apache.maven.model.Parent;
Review Comment:
No longer needed with the simplification.
```suggestion
```
##########
src/test/java/org/apache/maven/plugins/help/AllProfilesMojoTest.java:
##########
@@ -141,25 +147,151 @@ void testProfileFromPom(AllProfilesMojo mojo) throws
Exception {
}
/**
- * Tests the case when active profiles are present in the parent POM.
+ * Tests that a parent profile injected into the current project is active.
*
* @throws Exception in case of errors.
*/
@Test
@InjectMojo(goal = "all-profiles")
@MojoParameter(name = "output", value = "${outputPath}")
- void testProfileFromParentPom(AllProfilesMojo mojo) throws Exception {
- Model parentModel = mock(Model.class);
-
when(parentModel.getProfiles()).thenReturn(Collections.singletonList(newPomProfile("pro-1",
"pom")));
- MavenProject parentProject = mock(MavenProject.class);
- when(parentProject.getModel()).thenReturn(parentModel);
-
when(parentProject.getActiveProfiles()).thenReturn(Collections.singletonList(newPomProfile("pro-1",
"pom")));
+ void testInjectedProfileFromParentPom(AllProfilesMojo mojo) throws
Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put("org.apache.maven.its.help:parent:1.0",
Collections.singletonList("pro-1"));
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ }
+
+ /**
+ * Tests that inherited group and version are used in an injected
profile's model identifier.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void testInjectedProfileWithInheritedCoordinates(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ Parent parent = new Parent();
+ parent.setGroupId("org.apache.maven.its.help");
+ parent.setArtifactId("grandparent");
+ parent.setVersion("1.0");
+ parentProject.getModel().setParent(parent);
+ parentProject.getModel().setGroupId(null);
+ parentProject.getModel().setVersion(null);
when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put("org.apache.maven.its.help:parent:1.0",
Collections.singletonList("pro-1"));
mojo.execute();
String file = readOutput();
assertTrue(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ }
+
+ /**
+ * Tests that an ancestor can be identified by its uninterpolated version.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void testInjectedProfileWithUninterpolatedVersion(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ Model originalModel = parentProject.getModel().clone();
+ Parent parent = new Parent();
+ parent.setGroupId("org.apache.maven.its.help");
+ parent.setArtifactId("grandparent");
+ parent.setVersion("${revision}");
+ originalModel.setParent(parent);
+ originalModel.setGroupId(null);
+ originalModel.setVersion(null);
+ parentProject.setOriginalModel(originalModel);
+ when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put(
+ "org.apache.maven.its.help:parent:${revision}",
Collections.singletonList("pro-1"));
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ }
+
+ /**
+ * Tests that an explicitly empty entry takes precedence over
uninterpolated coordinates.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void
testEmptyInjectedProfilesDoNotUseUninterpolatedVersion(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ Model originalModel = parentProject.getModel().clone();
+ originalModel.setVersion("${revision}");
+ parentProject.setOriginalModel(originalModel);
+ when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put("org.apache.maven.its.help:parent:1.0",
Collections.emptyList());
+ projectInjectedProfileIds.put(
+ "org.apache.maven.its.help:parent:${revision}",
Collections.singletonList("pro-1"));
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ }
+
+ /**
+ * Tests that a profile active only in the parent's build context is
inactive for the current project.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void testParentActiveProfileNotInjectedIntoProject(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+
parentProject.setActiveProfiles(parentProject.getModel().getProfiles());
+ when(project.getParent()).thenReturn(parentProject);
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ }
+
+ /**
+ * Tests that injected profile identifiers are matched to the declaring
ancestor's model.
Review Comment:
```suggestion
* Tests that only injected profiles of more distant ancestors are
active.
```
##########
src/test/java/org/apache/maven/plugins/help/AllProfilesMojoTest.java:
##########
@@ -141,25 +147,151 @@ void testProfileFromPom(AllProfilesMojo mojo) throws
Exception {
}
/**
- * Tests the case when active profiles are present in the parent POM.
+ * Tests that a parent profile injected into the current project is active.
*
* @throws Exception in case of errors.
*/
@Test
@InjectMojo(goal = "all-profiles")
@MojoParameter(name = "output", value = "${outputPath}")
- void testProfileFromParentPom(AllProfilesMojo mojo) throws Exception {
- Model parentModel = mock(Model.class);
-
when(parentModel.getProfiles()).thenReturn(Collections.singletonList(newPomProfile("pro-1",
"pom")));
- MavenProject parentProject = mock(MavenProject.class);
- when(parentProject.getModel()).thenReturn(parentModel);
-
when(parentProject.getActiveProfiles()).thenReturn(Collections.singletonList(newPomProfile("pro-1",
"pom")));
+ void testInjectedProfileFromParentPom(AllProfilesMojo mojo) throws
Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put("org.apache.maven.its.help:parent:1.0",
Collections.singletonList("pro-1"));
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ }
+
+ /**
+ * Tests that inherited group and version are used in an injected
profile's model identifier.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void testInjectedProfileWithInheritedCoordinates(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ Parent parent = new Parent();
+ parent.setGroupId("org.apache.maven.its.help");
+ parent.setArtifactId("grandparent");
+ parent.setVersion("1.0");
+ parentProject.getModel().setParent(parent);
+ parentProject.getModel().setGroupId(null);
+ parentProject.getModel().setVersion(null);
when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put("org.apache.maven.its.help:parent:1.0",
Collections.singletonList("pro-1"));
mojo.execute();
String file = readOutput();
assertTrue(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ }
+
+ /**
+ * Tests that an ancestor can be identified by its uninterpolated version.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void testInjectedProfileWithUninterpolatedVersion(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ Model originalModel = parentProject.getModel().clone();
+ Parent parent = new Parent();
+ parent.setGroupId("org.apache.maven.its.help");
+ parent.setArtifactId("grandparent");
+ parent.setVersion("${revision}");
+ originalModel.setParent(parent);
+ originalModel.setGroupId(null);
+ originalModel.setVersion(null);
+ parentProject.setOriginalModel(originalModel);
+ when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put(
+ "org.apache.maven.its.help:parent:${revision}",
Collections.singletonList("pro-1"));
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ }
+
+ /**
+ * Tests that an explicitly empty entry takes precedence over
uninterpolated coordinates.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void
testEmptyInjectedProfilesDoNotUseUninterpolatedVersion(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+ Model originalModel = parentProject.getModel().clone();
+ originalModel.setVersion("${revision}");
+ parentProject.setOriginalModel(originalModel);
+ when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put("org.apache.maven.its.help:parent:1.0",
Collections.emptyList());
+ projectInjectedProfileIds.put(
+ "org.apache.maven.its.help:parent:${revision}",
Collections.singletonList("pro-1"));
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ }
+
+ /**
+ * Tests that a profile active only in the parent's build context is
inactive for the current project.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void testParentActiveProfileNotInjectedIntoProject(AllProfilesMojo mojo)
throws Exception {
+ MavenProject parentProject = newParentProject("parent", "pro-1");
+
parentProject.setActiveProfiles(parentProject.getModel().getProfiles());
+ when(project.getParent()).thenReturn(parentProject);
+
+ mojo.execute();
+
+ String file = readOutput();
+ assertTrue(file.contains("Profile Id: pro-1 (Active: false, Source:
pom)"));
+ assertFalse(file.contains("Profile Id: pro-1 (Active: true, Source:
pom)"));
+ }
+
+ /**
+ * Tests that injected profile identifiers are matched to the declaring
ancestor's model.
+ *
+ * @throws Exception in case of errors.
+ */
+ @Test
+ @InjectMojo(goal = "all-profiles")
+ @MojoParameter(name = "output", value = "${outputPath}")
+ void testInjectedProfileFromGrandparentPom(AllProfilesMojo mojo) throws
Exception {
+ MavenProject parentProject = newParentProject("parent",
"parent-profile");
+ MavenProject grandparentProject = newParentProject("grandparent",
"grandparent-profile");
+ parentProject.setParent(grandparentProject);
+ when(project.getParent()).thenReturn(parentProject);
+ projectInjectedProfileIds.put(
+ "org.apache.maven.its.help:grandparent:1.0",
Collections.singletonList("grandparent-profile"));
+ projectInjectedProfileIds.put("other:model:1.0",
Collections.singletonList("parent-profile"));
Review Comment:
With matching by profile ID, the model key no longer matters;
`parent-profile` should simply not be injected.
```suggestion
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]