mirror of https://github.com/apache/maven.git
[MNG-8053] Profile activation by packaging in the POM (#1410)
This commit is contained in:
parent
70509586f8
commit
0c529f830a
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
||||
|
|
|
@ -65,19 +65,6 @@ public final class SettingsUtils {
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -125,6 +125,8 @@ public final class SettingsUtilsV4 {
|
|||
activation.file(file);
|
||||
}
|
||||
|
||||
activation.packaging(modelActivation.getPackaging());
|
||||
|
||||
profile.activation(activation.build());
|
||||
}
|
||||
|
||||
|
@ -208,6 +210,8 @@ public final class SettingsUtilsV4 {
|
|||
.build());
|
||||
}
|
||||
|
||||
activation.packaging(settingsActivation.getPackaging());
|
||||
|
||||
profile.activation(activation.build());
|
||||
}
|
||||
|
||||
|
|
|
@ -83,6 +83,7 @@ class SettingsUtilsTest {
|
|||
.file(af)
|
||||
.property(ap)
|
||||
.os(ao)
|
||||
.packaging("pom")
|
||||
.build();
|
||||
Map<String, String> props = new HashMap<>();
|
||||
int count = entropy.nextInt(10);
|
||||
|
@ -145,6 +146,7 @@ class SettingsUtilsTest {
|
|||
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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue