Skip to content

Commit

Permalink
[MNG-8053] Profile activation by packaging in the POM (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Feb 15, 2024
1 parent 7050958 commit 0c529f8
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 14 deletions.
6 changes: 6 additions & 0 deletions api/maven-api-model/src/main/mdo/maven.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
8 changes: 8 additions & 0 deletions api/maven-api-settings/src/main/mdo/settings.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -208,6 +210,8 @@ public static org.apache.maven.api.model.Profile convertFromSettingsProfile(Prof
.build());
}

activation.packaging(settingsActivation.getPackaging());

profile.activation(activation.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 0c529f8

Please sign in to comment.