You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jd...@apache.org on 2007/01/25 20:02:17 UTC

svn commit: r499911 - in /maven/components/trunk: maven-core/src/main/java/org/apache/maven/ maven-project/src/main/java/org/apache/maven/profiles/activation/ maven-project/src/main/resources/META-INF/plexus/

Author: jdcasey
Date: Thu Jan 25 11:02:16 2007
New Revision: 499911

URL: http://svn.apache.org/viewvc?view=rev&rev=499911
Log:
Adding custom profile activator support, using the <custom/> tag in the <activation/> section of a profile.

Added:
    maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivatorAdvice.java   (with props)
Modified:
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
    maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivator.java
    maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivator.java
    maven/components/trunk/maven-project/src/main/resources/META-INF/plexus/components.xml

Modified: maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java?view=diff&rev=499911&r1=499910&r2=499911
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java (original)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java Thu Jan 25 11:02:16 2007
@@ -17,6 +17,7 @@
  */
 
 
+import org.apache.maven.artifact.ArtifactUtils;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
@@ -41,6 +42,7 @@
 import org.apache.maven.monitor.event.MavenEvents;
 import org.apache.maven.profiles.DefaultProfileManager;
 import org.apache.maven.profiles.ProfileManager;
+import org.apache.maven.profiles.activation.CustomActivatorAdvice;
 import org.apache.maven.profiles.activation.ProfileActivationException;
 import org.apache.maven.project.DuplicateProjectException;
 import org.apache.maven.project.MavenProject;
@@ -381,83 +383,101 @@
                                             ProfileManager globalProfileManager )
         throws MavenExecutionException
     {
-        MavenProject superProject;
-        try
-        {
-            superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository(),
-                                                                                    globalProfileManager );
-        }
-        catch ( ProjectBuildingException e )
-        {
-            throw new MavenExecutionException( "Error building super-POM for retrieving the default remote repository list: " + e.getMessage(), e );
-        }
-        
-        List originalRemoteRepositories = superProject.getRemoteArtifactRepositories();
-        Map cache = new HashMap();
+        // setup the CustomActivatorAdvice to fail quietly while we discover extensions...then, we'll
+        // reset it.
+        CustomActivatorAdvice activatorAdvice = CustomActivatorAdvice.getCustomActivatorAdvice( buildContextManager );
+        activatorAdvice.setFailQuietly( true );
+        activatorAdvice.store( buildContextManager );
         
-        for ( Iterator it = files.iterator(); it.hasNext(); )
+        try
         {
-            File pom = (File) it.next();
-            
-            ModelLineage lineage;
+            MavenProject superProject;
             try
             {
-                lineage = modelLineageBuilder.buildModelLineage( pom, request.getLocalRepository(), originalRemoteRepositories, globalProfileManager, cache );
+                superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository(),
+                                                                                        globalProfileManager );
             }
             catch ( ProjectBuildingException e )
             {
-                throw new MavenExecutionException( "Error building model lineage in order to pre-scan for extensions: " + e.getMessage(), e );
+                throw new MavenExecutionException( "Error building super-POM for retrieving the default remote repository list: " + e.getMessage(), e );
             }
             
-            for ( ModelLineageIterator lineageIterator = lineage.lineageIterator(); lineageIterator.hasNext(); )
+            List originalRemoteRepositories = superProject.getRemoteArtifactRepositories();
+            Map cache = new HashMap();
+            
+            for ( Iterator it = files.iterator(); it.hasNext(); )
             {
-                Model model = (Model) lineageIterator.next();
+                File pom = (File) it.next();
                 
-                Build build = model.getBuild();
+                ModelLineage lineage;
+                try
+                {
+                    getLogger().debug( "Building model-lineage for: " + pom + " to pre-scan for extensions." );
+                    
+                    lineage = modelLineageBuilder.buildModelLineage( pom, request.getLocalRepository(), originalRemoteRepositories, globalProfileManager, cache );
+                }
+                catch ( ProjectBuildingException e )
+                {
+                    throw new MavenExecutionException( "Error building model lineage in order to pre-scan for extensions: " + e.getMessage(), e );
+                }
                 
-                if ( build != null )
+                for ( ModelLineageIterator lineageIterator = lineage.lineageIterator(); lineageIterator.hasNext(); )
                 {
-                    List extensions = build.getExtensions();
+                    Model model = (Model) lineageIterator.next();
+                    
+                    Build build = model.getBuild();
                     
-                    if ( extensions != null && !extensions.isEmpty() )
+                    if ( build != null )
                     {
-                        List remoteRepositories = lineageIterator.getArtifactRepositories();
+                        List extensions = build.getExtensions();
                         
-                        // thankfully, we don't have to deal with dependencyManagement here, yet.
-                        // TODO Revisit if/when extensions are made to use the info in dependencyManagement
-                        for ( Iterator extensionIterator = extensions.iterator(); extensionIterator.hasNext(); )
+                        if ( extensions != null && !extensions.isEmpty() )
                         {
-                            Extension extension = (Extension) extensionIterator.next();
+                            List remoteRepositories = lineageIterator.getArtifactRepositories();
                             
-                            try
-                            {
-                                extensionManager.addExtension( extension, model, remoteRepositories, request.getLocalRepository() );
-                            }
-                            catch ( ArtifactResolutionException e )
-                            {
-                                throw new MavenExecutionException( "Cannot resolve pre-scanned extension artifact: "
-                                    + extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
-                                                                   e );
-                            }
-                            catch ( ArtifactNotFoundException e )
-                            {
-                                throw new MavenExecutionException( "Cannot find pre-scanned extension artifact: "
-                                                                   + extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
-                                                                                                  e );
-                            }
-                            catch ( PlexusContainerException e )
+                            // thankfully, we don't have to deal with dependencyManagement here, yet.
+                            // TODO Revisit if/when extensions are made to use the info in dependencyManagement
+                            for ( Iterator extensionIterator = extensions.iterator(); extensionIterator.hasNext(); )
                             {
-                                throw new MavenExecutionException( "Failed to add pre-scanned extension: "
-                                                                   + extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
-                                                                                                  e );
+                                Extension extension = (Extension) extensionIterator.next();
+                                
+                                getLogger().debug( "Adding extension: " + ArtifactUtils.versionlessKey( extension.getGroupId(), extension.getArtifactId() ) + " from model: " + model.getId() );
+                                
+                                try
+                                {
+                                    extensionManager.addExtension( extension, model, remoteRepositories, request.getLocalRepository() );
+                                }
+                                catch ( ArtifactResolutionException e )
+                                {
+                                    throw new MavenExecutionException( "Cannot resolve pre-scanned extension artifact: "
+                                        + extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
+                                                                       e );
+                                }
+                                catch ( ArtifactNotFoundException e )
+                                {
+                                    throw new MavenExecutionException( "Cannot find pre-scanned extension artifact: "
+                                                                       + extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
+                                                                                                      e );
+                                }
+                                catch ( PlexusContainerException e )
+                                {
+                                    throw new MavenExecutionException( "Failed to add pre-scanned extension: "
+                                                                       + extension.getGroupId() + ":" + extension.getArtifactId() + ": " + e.getMessage(),
+                                                                                                      e );
+                                }
                             }
                         }
                     }
                 }
             }
+            
+            extensionManager.registerWagons();
+        }
+        finally
+        {
+            activatorAdvice.reset();
+            activatorAdvice.store( buildContextManager );
         }
-        
-        extensionManager.registerWagons();
     }
 
     private void logReactorSummaryLine( String name,

Modified: maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivator.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivator.java?view=diff&rev=499911&r1=499910&r2=499911
==============================================================================
--- maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivator.java (original)
+++ maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivator.java Thu Jan 25 11:02:16 2007
@@ -1,5 +1,6 @@
 package org.apache.maven.profiles.activation;
 
+import org.apache.maven.context.BuildContextManager;
 import org.apache.maven.model.Activation;
 import org.apache.maven.model.ActivationCustom;
 import org.apache.maven.model.Profile;
@@ -49,7 +50,10 @@
 
     private Logger logger;
 
+    private BuildContextManager buildContextManager;
+
     public boolean canDetermineActivation( Profile profile )
+        throws ProfileActivationException
     {
         Activation activation = profile.getActivation();
 
@@ -60,8 +64,11 @@
             if ( custom != null )
             {
                 ProfileActivator activator = loadProfileActivator( custom );
-
-                return activator.canDetermineActivation( profile );
+                
+                if ( activator != null )
+                {
+                    return activator.canDetermineActivation( profile );
+                }
             }
         }
 
@@ -69,10 +76,14 @@
     }
 
     private ProfileActivator loadProfileActivator( ActivationCustom custom )
+        throws ProfileActivationException
     {
+        CustomActivatorAdvice advice = CustomActivatorAdvice.getCustomActivatorAdvice( buildContextManager );
+
         String type = custom.getType();
 
-        ProfileActivator activator;
+        ProfileActivator activator = null;
+
         try
         {
             activator = (ProfileActivator) container.lookup( ProfileActivator.ROLE, type );
@@ -81,8 +92,11 @@
         {
             getLogger().debug( "Failed to lookup ProfileActivator \'" + type + "\'", e );
 
-            throw new IllegalArgumentException( "Cannot find ProfileActivator with role-hint: " + type
-                + ". \nPerhaps you're missing a build extension? \nSee debug output for more information." );
+            if ( !advice.failQuietly() )
+            {
+                throw new ProfileActivationException( "Cannot find ProfileActivator with role-hint: " + type
+                    + ". \nPerhaps you're missing a build extension?", e );
+            }
         }
 
         PlexusConfiguration configuration = new XmlPlexusConfiguration( (Xpp3Dom) custom.getConfiguration() );
@@ -97,14 +111,18 @@
         {
             getLogger().debug( "Failed to configure ProfileActivator \'" + type + "\'", e );
 
-            throw new IllegalArgumentException( "Failed to configure ProfileActivator with role-hint: " + type
-                + ". Turn on debug mode for more information." );
+            if ( !advice.failQuietly() )
+            {
+                throw new ProfileActivationException( "Failed to configure ProfileActivator with role-hint: " + type
+                    + ".", e );
+            }
         }
 
         return activator;
     }
 
     public boolean isActive( Profile profile )
+        throws ProfileActivationException
     {
         ActivationCustom custom = profile.getActivation().getCustom();
 

Added: maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivatorAdvice.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivatorAdvice.java?view=auto&rev=499911
==============================================================================
--- maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivatorAdvice.java (added)
+++ maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivatorAdvice.java Thu Jan 25 11:02:16 2007
@@ -0,0 +1,80 @@
+package org.apache.maven.profiles.activation;
+
+import org.apache.maven.context.BuildContext;
+import org.apache.maven.context.BuildContextManager;
+import org.apache.maven.context.ManagedBuildData;
+
+/**
+ * Advice for the custom profile activator, which tells how to handle cases where custom activators
+ * cannot be found or configured. This is used to suppress missing activators when pre-scanning for
+ * build extensions (which may contain the custom activator).
+ * 
+ * @author jdcasey
+ */
+public class CustomActivatorAdvice
+    implements ManagedBuildData
+{
+    
+    public static final String BUILD_CONTEXT_KEY = CustomActivatorAdvice.class.getName();
+    
+    private static final boolean DEFAULT_FAIL_QUIETLY = true;
+    
+    /**
+     * If set to false, this tells the CustomProfileActivator to fail quietly when the specified 
+     * custom profile activator cannot be found or configured correctly. Default behavior is to throw
+     * a new ProfileActivationException.
+     */
+    private boolean failQuietly = DEFAULT_FAIL_QUIETLY;
+    
+    public void reset()
+    {
+        failQuietly = DEFAULT_FAIL_QUIETLY;
+    }
+    
+    public void setFailQuietly( boolean ignoreMissingActivator )
+    {
+        this.failQuietly = ignoreMissingActivator;
+    }
+    
+    public boolean failQuietly()
+    {
+        return failQuietly;
+    }
+
+    public String getStorageKey()
+    {
+        return BUILD_CONTEXT_KEY;
+    }
+    
+    /**
+     * Read the custom profile activator advice from the build context. If missing or the build
+     * context has not been initialized, create a new instance of the advice and return that.
+     */
+    public static CustomActivatorAdvice getCustomActivatorAdvice( BuildContextManager buildContextManager )
+    {
+        BuildContext buildContext = buildContextManager.readBuildContext( false );
+        
+        CustomActivatorAdvice advice = null;
+        
+        if ( buildContext != null )
+        {
+            advice = (CustomActivatorAdvice) buildContext.get( BUILD_CONTEXT_KEY );
+        }
+        
+        if ( advice == null )
+        {
+            advice = new CustomActivatorAdvice();
+        }
+        
+        return advice;
+    }
+    
+    public void store( BuildContextManager buildContextManager )
+    {
+        BuildContext buildContext = buildContextManager.readBuildContext( true );
+        
+        buildContext.put( this );
+        
+        buildContextManager.storeBuildContext( buildContext );
+    }
+}

Propchange: maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivatorAdvice.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/CustomActivatorAdvice.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivator.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivator.java?view=diff&rev=499911&r1=499910&r2=499911
==============================================================================
--- maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivator.java (original)
+++ maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivator.java Thu Jan 25 11:02:16 2007
@@ -22,8 +22,8 @@
 {
     static final String ROLE = ProfileActivator.class.getName();
 
-    boolean canDetermineActivation( Profile profile );
+    boolean canDetermineActivation( Profile profile ) throws ProfileActivationException;
 
-    boolean isActive( Profile profile );
+    boolean isActive( Profile profile ) throws ProfileActivationException;
 
 }

Modified: maven/components/trunk/maven-project/src/main/resources/META-INF/plexus/components.xml
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/main/resources/META-INF/plexus/components.xml?view=diff&rev=499911&r1=499910&r2=499911
==============================================================================
--- maven/components/trunk/maven-project/src/main/resources/META-INF/plexus/components.xml (original)
+++ maven/components/trunk/maven-project/src/main/resources/META-INF/plexus/components.xml Thu Jan 25 11:02:16 2007
@@ -205,6 +205,12 @@
       <role>org.apache.maven.profiles.activation.ProfileActivator</role>
       <role-hint>custom</role-hint>
       <implementation>org.apache.maven.profiles.activation.CustomActivator</implementation>
+      <requirements>
+        <requirement>
+          <role>org.apache.maven.context.BuildContextManager</role>
+          <role-hint>default</role-hint>
+        </requirement>
+      </requirements>
     </component>
 <!--
      |