Resolving: MNG-835. Using activateByDefault within activation in the profile.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@280476 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-09-13 03:23:16 +00:00
parent 459737fa5f
commit c9b6d83c57
11 changed files with 274 additions and 5 deletions

View File

@ -2610,6 +2610,12 @@
the automatic inclusion of the parent build profile.
]]></description>
<fields>
<field>
<name>activeByDefault</name>
<version>4.0.0</version>
<type>boolean</type>
<description>Flag specifying whether this profile is active as a default.</description>
</field>
<field>
<name>jdk</name>
<version>4.0.0</version>

View File

@ -112,6 +112,12 @@
the automatic inclusion of the parent build profile.
]]></description>
<fields>
<field>
<name>activeByDefault</name>
<version>1.0.0</version>
<type>boolean</type>
<description>Flag specifying whether this profile is active as a default.</description>
</field>
<field>
<name>jdk</name>
<version>1.0.0</version>

View File

@ -45,6 +45,8 @@ public class ProfilesConversionUtils
{
Activation activation = new Activation();
activation.setActiveByDefault( profileActivation.isActiveByDefault() );
activation.setJdk( profileActivation.getJdk() );
org.apache.maven.profiles.ActivationProperty profileProp = profileActivation.getProperty();

View File

@ -1,5 +1,6 @@
package org.apache.maven.profiles;
import org.apache.maven.model.Activation;
import org.apache.maven.model.Profile;
import org.apache.maven.profiles.activation.ProfileActivationException;
import org.apache.maven.profiles.activation.ProfileActivator;
@ -38,6 +39,7 @@ public class DefaultProfileManager implements ProfileManager
private Set activatedIds = new HashSet();
private Set deactivatedIds = new HashSet();
private Set defaultIds = new HashSet();
private Map profilesById = new HashMap();
@ -86,6 +88,13 @@ public class DefaultProfileManager implements ProfileManager
}
profilesById.put( profile.getId(), profile );
Activation activation = profile.getActivation();
if ( activation != null && activation.isActiveByDefault() )
{
activateAsDefault( profileId );
}
}
/* (non-Javadoc)
@ -158,6 +167,18 @@ public class DefaultProfileManager implements ProfileManager
}
}
if ( active.isEmpty() )
{
for ( Iterator it = defaultIds.iterator(); it.hasNext(); )
{
String profileId = (String) it.next();
Profile profile = (Profile) profilesById.get( profileId );
active.add( profile );
}
}
return active;
}
@ -211,4 +232,9 @@ public class DefaultProfileManager implements ProfileManager
}
}
public void activateAsDefault( String profileId )
{
defaultIds.add( profileId );
}
}

View File

@ -20,6 +20,8 @@ public interface ProfileManager
void explicitlyDeactivate( List profileIds );
void activateAsDefault( String profileId );
List getActiveProfiles()
throws ProfileActivationException;

View File

@ -31,8 +31,25 @@ public class JdkPrefixProfileActivator
String jdk = activation.getJdk();
boolean reverse = false;
if ( jdk.startsWith( "!" ) )
{
reverse = true;
jdk = jdk.substring( 1 );
}
// null case is covered by canDetermineActivation(), so we can do a straight startsWith() here.
return JDK_VERSION.startsWith( jdk );
boolean result = JDK_VERSION.startsWith( jdk );
if ( reverse )
{
return !result;
}
else
{
return result;
}
}
protected boolean canDetectActivation( Profile profile )

View File

@ -37,17 +37,51 @@ public class SystemPropertyProfileActivator
if ( property != null )
{
String sysValue = System.getProperty( property.getName() );
String name = property.getName();
boolean reverseName = false;
if ( name.startsWith("!") )
{
reverseName = true;
name = name.substring( 1 );
}
String sysValue = System.getProperty( name );
String propValue = property.getValue();
if ( StringUtils.isNotEmpty( propValue ) )
{
boolean reverseValue = false;
if ( propValue.startsWith( "!" ) )
{
reverseValue = true;
propValue = propValue.substring( 1 );
}
// we have a value, so it has to match the system value...
return propValue.equals( sysValue );
boolean result = propValue.equals( sysValue );
if ( reverseValue )
{
return !result;
}
else
{
return result;
}
}
else
{
return StringUtils.isNotEmpty( sysValue );
boolean result = StringUtils.isNotEmpty( sysValue );
if ( reverseName )
{
return !result;
}
else
{
return result;
}
}
}

View File

@ -861,11 +861,20 @@ public class DefaultMavenProjectBuilder
if ( root != null )
{
List active = root.getActiveProfiles();
if( active != null && !active.isEmpty() )
{
profileManager.explicitlyActivate( root.getActiveProfiles() );
}
for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); )
{
org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next();
profileManager.addProfile( ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile ) );
Profile converted = ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile );
profileManager.addProfile( converted );
}
}
}

View File

@ -0,0 +1,159 @@
package org.apache.maven.profiles;
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationProperty;
import org.apache.maven.model.Profile;
import org.apache.maven.profiles.activation.ProfileActivationException;
import org.codehaus.plexus.PlexusTestCase;
import java.util.List;
public class DefaultProfileManagerTest
extends PlexusTestCase
{
public void testShouldActivateDefaultProfile() throws ProfileActivationException
{
Profile notActivated = new Profile();
notActivated.setId("notActivated");
Activation nonActivation = new Activation();
nonActivation.setJdk("19.2");
notActivated.setActivation( nonActivation );
Profile defaultActivated = new Profile();
defaultActivated.setId("defaultActivated");
Activation defaultActivation = new Activation();
defaultActivation.setActiveByDefault(true);
defaultActivated.setActivation( defaultActivation );
ProfileManager profileManager = new DefaultProfileManager(getContainer());
profileManager.addProfile(notActivated);
profileManager.addProfile(defaultActivated);
List active = profileManager.getActiveProfiles();
assertNotNull( active );
assertEquals( 1, active.size() );
assertEquals("defaultActivated", ((Profile)active.get(0)).getId());
}
public void testShouldNotActivateDefaultProfile() throws ProfileActivationException
{
Profile syspropActivated = new Profile();
syspropActivated.setId("syspropActivated");
Activation syspropActivation = new Activation();
ActivationProperty syspropProperty = new ActivationProperty();
syspropProperty.setName("java.version");
syspropActivation.setProperty(syspropProperty);
syspropActivated.setActivation( syspropActivation );
Profile defaultActivated = new Profile();
defaultActivated.setId("defaultActivated");
Activation defaultActivation = new Activation();
defaultActivation.setActiveByDefault(true);
defaultActivated.setActivation( defaultActivation );
ProfileManager profileManager = new DefaultProfileManager(getContainer());
profileManager.addProfile(syspropActivated);
profileManager.addProfile(defaultActivated);
List active = profileManager.getActiveProfiles();
assertNotNull( active );
assertEquals( 1, active.size() );
assertEquals("syspropActivated", ((Profile)active.get(0)).getId());
}
public void testShouldNotActivateReversalOfPresentSystemProperty() throws ProfileActivationException
{
Profile syspropActivated = new Profile();
syspropActivated.setId("syspropActivated");
Activation syspropActivation = new Activation();
ActivationProperty syspropProperty = new ActivationProperty();
syspropProperty.setName("!java.version");
syspropActivation.setProperty(syspropProperty);
syspropActivated.setActivation( syspropActivation );
ProfileManager profileManager = new DefaultProfileManager(getContainer());
profileManager.addProfile(syspropActivated);
List active = profileManager.getActiveProfiles();
assertNotNull( active );
assertEquals( 0, active.size() );
}
public void testShouldOverrideAndActivateInactiveProfile() throws ProfileActivationException
{
Profile syspropActivated = new Profile();
syspropActivated.setId("syspropActivated");
Activation syspropActivation = new Activation();
ActivationProperty syspropProperty = new ActivationProperty();
syspropProperty.setName("!java.version");
syspropActivation.setProperty(syspropProperty);
syspropActivated.setActivation( syspropActivation );
ProfileManager profileManager = new DefaultProfileManager(getContainer());
profileManager.addProfile(syspropActivated);
profileManager.explicitlyActivate("syspropActivated");
List active = profileManager.getActiveProfiles();
assertNotNull( active );
assertEquals( 1, active.size() );
assertEquals( "syspropActivated", ((Profile)active.get(0)).getId());
}
public void testShouldOverrideAndDeactivateActiveProfile() throws ProfileActivationException
{
Profile syspropActivated = new Profile();
syspropActivated.setId("syspropActivated");
Activation syspropActivation = new Activation();
ActivationProperty syspropProperty = new ActivationProperty();
syspropProperty.setName("java.version");
syspropActivation.setProperty(syspropProperty);
syspropActivated.setActivation( syspropActivation );
ProfileManager profileManager = new DefaultProfileManager(getContainer());
profileManager.addProfile(syspropActivated);
profileManager.explicitlyDeactivate("syspropActivated");
List active = profileManager.getActiveProfiles();
assertNotNull( active );
assertEquals( 0, active.size() );
}
}

View File

@ -559,6 +559,12 @@
the automatic inclusion of the parent build profile.
]]></description>
<fields>
<field>
<name>activeByDefault</name>
<version>1.0.0</version>
<type>boolean</type>
<description>Flag specifying whether this profile is active as a default.</description>
</field>
<field>
<name>jdk</name>
<version>1.0.0</version>

View File

@ -147,6 +147,8 @@ public final class SettingsUtils
{
org.apache.maven.model.Activation activation = new org.apache.maven.model.Activation();
activation.setActiveByDefault( settingsActivation.isActiveByDefault() );
activation.setJdk( settingsActivation.getJdk() );
ActivationProperty settingsProp = settingsActivation.getProperty();