Skip to content

Commit

Permalink
Define getActiveProfiles() instead
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Feb 26, 2025
1 parent 5308f55 commit d7c1fc4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
26 changes: 17 additions & 9 deletions api/maven-api-core/src/main/java/org/apache/maven/api/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.model.Build;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.Profile;

/**
* Interface representing a Maven project which can be created using the
Expand Down Expand Up @@ -240,15 +240,23 @@ default String getId() {
Optional<Project> getParent();

/**
* Gets the identifiers of all profiles that contributed to this project's effective model. This includes active
* profiles from the project's POM and all its parent POMs as well as from external sources like the
* {@code settings.xml}. The profile identifiers are grouped by the identifier of their source, e.g.
* {@code <groupId>:<artifactId>:<version>} for a POM profile or {@code external} for profiles from the
* {@code settings.xml}.
* Returns all active profiles for the current project build.
* <p>
* Active profiles are those that have been explicitly activated through one of the following means:
* <ul>
* <li>Command line activation using the -P flag</li>
* <li>Maven settings activation in settings.xml via &lt;activeProfiles&gt;</li>
* <li>Automatic activation via &lt;activation&gt; conditions</li>
* <li>The default active profile (marked with &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;)</li>
* </ul>
* <p>
* The active profiles control various aspects of the build configuration including but not
* limited to dependencies, plugins, properties, and build resources.
*
* @return The identifiers of all activated profiles, indexed by the source from which the profiles originated, never
* {@code null}.
* @return a non-null, possibly empty list of active profiles for this project
* @since 4.0.0
* @see Profile
*/
@Nonnull
Map<String, List<String>> getActivatedProfileIdsBySource();
List<Profile> getActiveProfiles();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.RepositoryUtils;
Expand All @@ -39,6 +38,7 @@
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.model.DependencyManagement;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.Profile;
import org.apache.maven.impl.MappedCollection;
import org.apache.maven.impl.MappedList;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -167,8 +167,12 @@ public Optional<Project> getParent() {
}

@Override
public Map<String, List<String>> getActivatedProfileIdsBySource() {
return project.getInjectedProfileIds();
@Nonnull
public List<Profile> getActiveProfiles() {
List<org.apache.maven.model.Profile> activeProfiles = project.getActiveProfiles();
return activeProfiles != null
? new MappedList<>(activeProfiles, org.apache.maven.model.Profile::getDelegate)
: List.of();
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,26 @@
import java.nio.file.StandardCopyOption;
import java.util.List;

import org.apache.maven.api.model.InputLocation;
import org.apache.maven.api.model.InputSource;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.impl.InternalSession;
import org.apache.maven.internal.impl.DefaultProject;
import org.apache.maven.internal.impl.InternalMavenSession;
import org.apache.maven.model.Profile;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mockito;

import static org.apache.maven.project.ProjectBuildingResultWithProblemMessageMatcher.projectBuildingResultWithProblemMessage;
import static org.codehaus.plexus.testing.PlexusExtension.getTestFile;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -378,6 +383,19 @@ void testActivatedDefaultProfileBySource() throws Exception {
assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("profile1"::equals));
assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("profile2"::equals));
assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().anyMatch("active-by-default"::equals));

InternalMavenSession session = Mockito.mock(InternalMavenSession.class);
List<org.apache.maven.api.model.Profile> activeProfiles =
new DefaultProject(session, project).getActiveProfiles();
assertEquals(1, activeProfiles.size());
org.apache.maven.api.model.Profile profile = activeProfiles.get(0);
assertEquals("active-by-default", profile.getId());
InputLocation location = profile.getLocation("");
assertNotNull(location);
assertThat(location.getLineNumber(), greaterThan(0));
assertThat(location.getColumnNumber(), greaterThan(0));
assertNotNull(location.getSource());
assertThat(location.getSource().getLocation(), containsString("pom-with-profiles/pom.xml"));
}

@Test
Expand All @@ -388,6 +406,10 @@ void testActivatedExternalProfileBySource() throws Exception {
request.setLocalRepository(getLocalRepository());

final Profile externalProfile = new Profile();
externalProfile.setLocation(
"",
new org.apache.maven.model.InputLocation(
1, 1, new org.apache.maven.model.InputSource(new InputSource(null, "settings.xml", null))));
externalProfile.setId("external-profile");
request.addProfile(externalProfile);
request.setActiveProfileIds(List.of(externalProfile.getId()));
Expand All @@ -399,6 +421,27 @@ void testActivatedExternalProfileBySource() throws Exception {
assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("profile1"::equals));
assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("profile2"::equals));
assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().anyMatch("active-by-default"::equals));

InternalMavenSession session = Mockito.mock(InternalMavenSession.class);
List<org.apache.maven.api.model.Profile> activeProfiles =
new DefaultProject(session, project).getActiveProfiles();
assertEquals(2, activeProfiles.size());
org.apache.maven.api.model.Profile profile = activeProfiles.get(0);
assertEquals("active-by-default", profile.getId());
InputLocation location = profile.getLocation("");
assertNotNull(location);
assertThat(location.getLineNumber(), greaterThan(0));
assertThat(location.getColumnNumber(), greaterThan(0));
assertNotNull(location.getSource());
assertThat(location.getSource().getLocation(), containsString("pom-with-profiles/pom.xml"));
profile = activeProfiles.get(1);
assertEquals("external-profile", profile.getId());
location = profile.getLocation("");
assertNotNull(location);
assertThat(location.getLineNumber(), greaterThan(0));
assertThat(location.getColumnNumber(), greaterThan(0));
assertNotNull(location.getSource());
assertThat(location.getSource().getLocation(), containsString("settings.xml"));
}

@Test
Expand Down

0 comments on commit d7c1fc4

Please sign in to comment.