Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8588] Add activated profiles to Project interface #2119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 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,6 +20,7 @@

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

import org.apache.maven.api.annotations.Experimental;
Expand Down Expand Up @@ -237,4 +238,17 @@ default String getId() {
*/
@Nonnull
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}.
*
* @return The identifiers of all activated profiles, indexed by the source from which the profiles originated, never
* {@code null}.
*/
@Nonnull
Map<String, List<String>> getActivatedProfileIdsBySource();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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 Down Expand Up @@ -165,6 +166,11 @@ public Optional<Project> getParent() {
return Optional.ofNullable(session.getProject(parent));
}

@Override
public Map<String, List<String>> getActivatedProfileIdsBySource() {
return project.getInjectedProfileIds();
}

@Nonnull
private DependencyCoordinates toDependency(org.apache.maven.api.model.Dependency dependency) {
return new DependencyCoordinates() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.impl.InternalSession;
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;
Expand Down Expand Up @@ -345,6 +346,92 @@ void rereadPom_mng7063() throws Exception {
assertThat(project.getName(), is("PROJECT NAME"));
}

@Test
void testActivatedProfileBySource() throws Exception {
File testPom = getTestFile("src/test/resources/projects/pom-with-profiles/pom.xml");

ProjectBuildingRequest request = newBuildingRequest();
request.setLocalRepository(getLocalRepository());
request.setActiveProfileIds(List.of("profile1"));

MavenProject project = projectBuilder.build(testPom, request).getProject();

assertTrue(project.getInjectedProfileIds().keySet().containsAll(List.of("external", project.getId())));
assertTrue(project.getInjectedProfileIds().get("external").isEmpty());
assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().anyMatch("profile1"::equals));
assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("profile2"::equals));
assertTrue(
project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("active-by-default"::equals));
}

@Test
void testActivatedDefaultProfileBySource() throws Exception {
File testPom = getTestFile("src/test/resources/projects/pom-with-profiles/pom.xml");

ProjectBuildingRequest request = newBuildingRequest();
request.setLocalRepository(getLocalRepository());

MavenProject project = projectBuilder.build(testPom, request).getProject();

assertTrue(project.getInjectedProfileIds().keySet().containsAll(List.of("external", project.getId())));
assertTrue(project.getInjectedProfileIds().get("external").isEmpty());
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));
}

@Test
void testActivatedExternalProfileBySource() throws Exception {
File testPom = getTestFile("src/test/resources/projects/pom-with-profiles/pom.xml");

ProjectBuildingRequest request = newBuildingRequest();
request.setLocalRepository(getLocalRepository());

final Profile externalProfile = new Profile();
externalProfile.setId("external-profile");
request.addProfile(externalProfile);
request.setActiveProfileIds(List.of(externalProfile.getId()));

MavenProject project = projectBuilder.build(testPom, request).getProject();

assertTrue(project.getInjectedProfileIds().keySet().containsAll(List.of("external", project.getId())));
assertTrue(project.getInjectedProfileIds().get("external").stream().anyMatch("external-profile"::equals));
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));
}

@Test
void testActivatedProfileIsResolved() throws Exception {
File testPom = getTestFile("src/test/resources/projects/pom-with-profiles/pom.xml");

ProjectBuildingRequest request = newBuildingRequest();
request.setLocalRepository(getLocalRepository());
request.setActiveProfileIds(List.of("profile1"));

MavenProject project = projectBuilder.build(testPom, request).getProject();

assertEquals(1, project.getActiveProfiles().size());
assertTrue(project.getActiveProfiles().stream().anyMatch(p -> "profile1".equals(p.getId())));
assertTrue(project.getActiveProfiles().stream().noneMatch(p -> "profile2".equals(p.getId())));
assertTrue(project.getActiveProfiles().stream().noneMatch(p -> "active-by-default".equals(p.getId())));
}

@Test
void testActivatedProfileByDefaultIsResolved() throws Exception {
File testPom = getTestFile("src/test/resources/projects/pom-with-profiles/pom.xml");

ProjectBuildingRequest request = newBuildingRequest();
request.setLocalRepository(getLocalRepository());

MavenProject project = projectBuilder.build(testPom, request).getProject();

assertEquals(1, project.getActiveProfiles().size());
assertTrue(project.getActiveProfiles().stream().noneMatch(p -> "profile1".equals(p.getId())));
assertTrue(project.getActiveProfiles().stream().noneMatch(p -> "profile2".equals(p.getId())));
assertTrue(project.getActiveProfiles().stream().anyMatch(p -> "active-by-default".equals(p.getId())));
}

/**
* Tests whether external version range parent references are build correctly.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="ISO-8859-1"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>maven</groupId>
<artifactId>maven-core</artifactId>
<version>2.0-SNAPSHOT</version>

<name>Maven</name>

<profiles>
<profile>
<id>active-by-default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>profile1</id>
</profile>
<profile>
<id>profile2</id>
</profile>
</profiles>
</project>