[MNG-8053] Profile activation by packaging in the POM (#1410)

This commit is contained in:
Guillaume Nodet 2024-02-15 09:42:00 +01:00 committed by GitHub
parent 70509586f8
commit 0c529f830a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 83 additions and 14 deletions

View File

@ -2771,6 +2771,12 @@
<type>ActivationFile</type>
</association>
</field>
<field>
<name>packaging</name>
<version>4.1.0+</version>
<type>String</type>
<description>Specifies that this profile will be activated based on the project's packaging.</description>
</field>
<!--
This could be included once we teach Maven to deal with multiple versions of the model
<field>

View File

@ -728,6 +728,14 @@
<type>ActivationFile</type>
</association>
</field>
<field>
<name>packaging</name>
<version>1.2.0+</version>
<type>String</type>
<description>
Specifies that this profile will be activated based on the project's packaging.
</description>
</field>
</fields>
</class>

View File

@ -65,19 +65,6 @@ public static Settings copySettings(Settings settings) {
return null;
}
Settings clone = new Settings();
clone.setActiveProfiles(settings.getActiveProfiles());
clone.setInteractiveMode(settings.isInteractiveMode());
clone.setLocalRepository(settings.getLocalRepository());
clone.setMirrors(settings.getMirrors());
clone.setOffline(settings.isOffline());
clone.setPluginGroups(settings.getPluginGroups());
clone.setProfiles(settings.getProfiles());
clone.setProxies(settings.getProxies());
clone.setServers(settings.getServers());
clone.setSourceLevel(settings.getSourceLevel());
clone.setUsePluginRegistry(settings.isUsePluginRegistry());
return clone;
return new Settings(settings.getDelegate());
}
}

View File

@ -125,6 +125,8 @@ public static Profile convertToSettingsProfile(org.apache.maven.api.model.Profil
activation.file(file);
}
activation.packaging(modelActivation.getPackaging());
profile.activation(activation.build());
}
@ -208,6 +210,8 @@ public static org.apache.maven.api.model.Profile convertFromSettingsProfile(Prof
.build());
}
activation.packaging(settingsActivation.getPackaging());
profile.activation(activation.build());
}

View File

@ -83,6 +83,7 @@ void testRoundTripProfiles() {
.file(af)
.property(ap)
.os(ao)
.packaging("pom")
.build();
Map<String, String> props = new HashMap<>();
int count = entropy.nextInt(10);
@ -145,6 +146,7 @@ void testRoundTripProfiles() {
assertEquals(
p.getActivation().getOs().getVersion(),
clone.getActivation().getOs().getVersion());
assertEquals(p.getActivation().getPackaging(), clone.getActivation().getPackaging());
assertEquals(p.getProperties(), clone.getProperties());
assertEquals(p.getRepositories().size(), clone.getRepositories().size());
// TODO deep compare the lists

View File

@ -0,0 +1,62 @@
/*
* 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.
*/
package org.apache.maven.model.profile.activation;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.Objects;
import java.util.Optional;
import org.apache.maven.api.model.Activation;
import org.apache.maven.api.model.Profile;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.profile.ProfileActivationContext;
/**
* Determines profile activation based on the project's packaging.
*/
@Named("packaging")
@Singleton
public class PackagingProfileActivator implements ProfileActivator {
@Override
public boolean isActive(
org.apache.maven.model.Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
return getActivationPackaging(profile).map(p -> isPackaging(context, p)).orElse(false);
}
@Override
public boolean presentInConfig(
org.apache.maven.model.Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
return getActivationPackaging(profile).isPresent();
}
private static boolean isPackaging(ProfileActivationContext context, String p) {
String packaging = context.getUserProperties().get(ProfileActivationContext.PROPERTY_NAME_PACKAGING);
return Objects.equals(p, packaging);
}
private static Optional<String> getActivationPackaging(org.apache.maven.model.Profile profile) {
return Optional.ofNullable(profile)
.map(org.apache.maven.model.Profile::getDelegate)
.map(Profile::getActivation)
.map(Activation::getPackaging);
}
}