maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bentm...@apache.org
Subject svn commit: r1055174 - in /maven/maven-3/trunk: maven-aether-provider/src/main/java/org/apache/maven/repository/internal/ maven-compat/src/main/java/org/apache/maven/execution/ maven-core/src/main/java/org/apache/maven/lifecycle/internal/ maven-core/sr...
Date Tue, 04 Jan 2011 20:32:42 GMT
Author: bentmann
Date: Tue Jan  4 20:32:41 2011
New Revision: 1055174

URL: http://svn.apache.org/viewvc?rev=1055174&view=rev
Log:
[MNG-4840] Prerequisites is not working on m3

Added:
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/PluginIncompatibleException.java
  (with props)
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/   (with props)
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java
  (with props)
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/   (with
props)
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java
  (with props)
    maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/   (with props)
    maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/   (with
props)
    maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java
  (with props)
Modified:
    maven/maven-3/trunk/maven-aether-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java
    maven/maven-3/trunk/maven-compat/src/main/java/org/apache/maven/execution/DefaultRuntimeInformation.java
    maven/maven-3/trunk/maven-compat/src/main/java/org/apache/maven/execution/RuntimeInformation.java
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginDescriptorCache.java
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/MavenPluginManager.java
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultPluginDependenciesResolver.java
    maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java
    maven/maven-3/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java

Modified: maven/maven-3/trunk/maven-aether-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-aether-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java?rev=1055174&r1=1055173&r2=1055174&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-aether-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java
(original)
+++ maven/maven-3/trunk/maven-aether-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java
Tue Jan  4 20:32:41 2011
@@ -32,6 +32,7 @@ import org.apache.maven.model.Dependency
 import org.apache.maven.model.DistributionManagement;
 import org.apache.maven.model.License;
 import org.apache.maven.model.Model;
+import org.apache.maven.model.Prerequisites;
 import org.apache.maven.model.Relocation;
 import org.apache.maven.model.Repository;
 import org.apache.maven.model.building.DefaultModelBuilderFactory;
@@ -88,6 +89,7 @@ public class DefaultArtifactDescriptorRe
     implements ArtifactDescriptorReader, Service
 {
 
+    @SuppressWarnings( "unused" )
     @Requirement
     private Logger logger = NullLogger.INSTANCE;
 
@@ -195,6 +197,12 @@ public class DefaultArtifactDescriptorRe
 
             Map<String, Object> properties = new LinkedHashMap<String, Object>();
 
+            Prerequisites prerequisites = model.getPrerequisites();
+            if ( prerequisites != null )
+            {
+                properties.put( "prerequisites.maven", prerequisites.getMaven() );
+            }
+
             List<License> licenses = model.getLicenses();
             properties.put( "license.count", Integer.valueOf( licenses.size() ) );
             for ( int i = 0; i < licenses.size(); i++ )

Modified: maven/maven-3/trunk/maven-compat/src/main/java/org/apache/maven/execution/DefaultRuntimeInformation.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-compat/src/main/java/org/apache/maven/execution/DefaultRuntimeInformation.java?rev=1055174&r1=1055173&r2=1055174&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-compat/src/main/java/org/apache/maven/execution/DefaultRuntimeInformation.java
(original)
+++ maven/maven-3/trunk/maven-compat/src/main/java/org/apache/maven/execution/DefaultRuntimeInformation.java
Tue Jan  4 20:32:41 2011
@@ -22,17 +22,14 @@ package org.apache.maven.execution;
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
-import org.codehaus.plexus.util.IOUtil;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Properties;
+import org.codehaus.plexus.util.StringUtils;
 
 /**
  * Describes runtime information about the application.
- *
+ * 
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  * @version $Id$
  */
@@ -41,9 +38,9 @@ import java.util.Properties;
 public class DefaultRuntimeInformation
     implements RuntimeInformation, Initializable
 {
-    private static final String MAVEN_GROUPID = "org.apache.maven";
 
-    private static final String MAVEN_PROPERTIES = "META-INF/maven/" + MAVEN_GROUPID + "/maven-core/pom.properties";
+    @Requirement
+    private org.apache.maven.rtinfo.RuntimeInformation rtInfo;
 
     private ArtifactVersion applicationVersion;
 
@@ -55,33 +52,14 @@ public class DefaultRuntimeInformation
     public void initialize()
         throws InitializationException
     {
-        InputStream resourceAsStream = null;
-        try
-        {
-            Properties properties = new Properties();
-            resourceAsStream = getClass().getClassLoader().getResourceAsStream( MAVEN_PROPERTIES
);
+        String mavenVersion = rtInfo.getMavenVersion();
 
-            if ( resourceAsStream == null )
-            {
-                throw new IllegalStateException( "Unable to find Maven properties in classpath:
" + MAVEN_PROPERTIES );
-            }
-            properties.load( resourceAsStream );
-
-            String property = properties.getProperty( "version" );
-            if ( property == null )
-            {
-                throw new InitializationException( "maven-core properties did not include
the version" );
-            }
-
-            applicationVersion = new DefaultArtifactVersion( property );
-        }
-        catch ( IOException e )
+        if ( StringUtils.isEmpty( mavenVersion ) )
         {
-            throw new InitializationException( "Unable to read properties file from maven-core",
e );
-        }
-        finally
-        {
-            IOUtil.close( resourceAsStream );
+            throw new InitializationException( "Unable to read Maven version from maven-core"
);
         }
+
+        applicationVersion = new DefaultArtifactVersion( mavenVersion );
     }
+
 }

Modified: maven/maven-3/trunk/maven-compat/src/main/java/org/apache/maven/execution/RuntimeInformation.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-compat/src/main/java/org/apache/maven/execution/RuntimeInformation.java?rev=1055174&r1=1055173&r2=1055174&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-compat/src/main/java/org/apache/maven/execution/RuntimeInformation.java
(original)
+++ maven/maven-3/trunk/maven-compat/src/main/java/org/apache/maven/execution/RuntimeInformation.java
Tue Jan  4 20:32:41 2011
@@ -24,9 +24,11 @@ import org.apache.maven.artifact.version
 /**
  * Describes runtime information about the application.
  *
+ * @deprecated Use {@link org.apache.maven.rtinfo.RuntimeInformation} instead.
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  * @version $Id$
  */
+@Deprecated
 public interface RuntimeInformation
 {
     ArtifactVersion getApplicationVersion();

Modified: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java?rev=1055174&r1=1055173&r2=1055174&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
(original)
+++ maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
Tue Jan  4 20:32:41 2011
@@ -27,10 +27,12 @@ import org.apache.maven.execution.MavenS
 import org.apache.maven.lifecycle.LifecycleExecutionException;
 import org.apache.maven.lifecycle.MissingProjectException;
 import org.apache.maven.plugin.BuildPluginManager;
+import org.apache.maven.plugin.MavenPluginManager;
 import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugin.PluginConfigurationException;
+import org.apache.maven.plugin.PluginIncompatibleException;
 import org.apache.maven.plugin.PluginManagerException;
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 import org.apache.maven.project.MavenProject;
@@ -65,6 +67,9 @@ public class MojoExecutor
     private BuildPluginManager pluginManager;
 
     @Requirement
+    private MavenPluginManager mavenPluginManager;
+
+    @Requirement
     private LifecycleDependencyResolver lifeCycleDependencyResolver;
 
     @Requirement
@@ -156,6 +161,15 @@ public class MojoExecutor
     {
         MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
 
+        try
+        {
+            mavenPluginManager.checkRequiredMavenVersion( mojoDescriptor.getPluginDescriptor()
);
+        }
+        catch ( PluginIncompatibleException e )
+        {
+            throw new LifecycleExecutionException( mojoExecution, session.getCurrentProject(),
e );
+        }
+
         if ( mojoDescriptor.isProjectRequired() && !session.isUsingPOMsFromFilesystem()
)
         {
             Throwable cause =

Modified: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginDescriptorCache.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginDescriptorCache.java?rev=1055174&r1=1055173&r2=1055174&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginDescriptorCache.java
(original)
+++ maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginDescriptorCache.java
Tue Jan  4 20:32:41 2011
@@ -72,7 +72,7 @@ public class DefaultPluginDescriptorCach
         descriptors.put( cacheKey, clone( pluginDescriptor ) );
     }
 
-    private static PluginDescriptor clone( PluginDescriptor original )
+    protected static PluginDescriptor clone( PluginDescriptor original )
     {
         PluginDescriptor clone = null;
 
@@ -88,6 +88,7 @@ public class DefaultPluginDescriptorCach
 
             clone.setName( original.getName() );
             clone.setDescription( original.getDescription() );
+            clone.setRequiredMavenVersion( original.getRequiredMavenVersion() );
 
             clone.setPluginArtifact( ArtifactUtils.copyArtifactSafe( original.getPluginArtifact()
) );
 

Modified: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/MavenPluginManager.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/MavenPluginManager.java?rev=1055174&r1=1055173&r2=1055174&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/MavenPluginManager.java
(original)
+++ maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/MavenPluginManager.java
Tue Jan  4 20:32:41 2011
@@ -68,6 +68,14 @@ public interface MavenPluginManager
         InvalidPluginDescriptorException;
 
     /**
+     * Verifies the specified plugin is compatible with the current Maven runtime.
+     * 
+     * @param pluginDescriptor The descriptor of the plugin to check, must not be {@code
null}.
+     */
+    void checkRequiredMavenVersion( PluginDescriptor pluginDescriptor )
+        throws PluginIncompatibleException;
+
+    /**
      * Sets up the class realm for the specified plugin. Both the class realm and the plugin
artifacts that constitute
      * it will be stored in the plugin descriptor.
      * 

Added: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/PluginIncompatibleException.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/PluginIncompatibleException.java?rev=1055174&view=auto
==============================================================================
--- maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/PluginIncompatibleException.java
(added)
+++ maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/PluginIncompatibleException.java
Tue Jan  4 20:32:41 2011
@@ -0,0 +1,36 @@
+package org.apache.maven.plugin;
+
+/*
+ * 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.
+ */
+
+import org.apache.maven.model.Plugin;
+
+/**
+ * Signals a plugin which is not compatible with the current Maven runtime.
+ */
+public class PluginIncompatibleException
+    extends PluginManagerException
+{
+
+    public PluginIncompatibleException( Plugin plugin, String message )
+    {
+        super( plugin, message, (Throwable) null );
+    }
+
+}

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/PluginIncompatibleException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/PluginIncompatibleException.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java?rev=1055174&r1=1055173&r2=1055174&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
(original)
+++ maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
Tue Jan  4 20:32:41 2011
@@ -55,6 +55,7 @@ import org.apache.maven.plugin.PluginCon
 import org.apache.maven.plugin.PluginContainerException;
 import org.apache.maven.plugin.PluginDescriptorCache;
 import org.apache.maven.plugin.PluginDescriptorParsingException;
+import org.apache.maven.plugin.PluginIncompatibleException;
 import org.apache.maven.plugin.PluginParameterException;
 import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
 import org.apache.maven.plugin.PluginRealmCache;
@@ -64,6 +65,7 @@ import org.apache.maven.plugin.descripto
 import org.apache.maven.plugin.descriptor.PluginDescriptor;
 import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.rtinfo.RuntimeInformation;
 import org.codehaus.plexus.PlexusContainer;
 import org.codehaus.plexus.classworlds.realm.ClassRealm;
 import org.codehaus.plexus.component.annotations.Component;
@@ -123,6 +125,9 @@ public class DefaultMavenPluginManager
     @Requirement
     private PluginDependenciesResolver pluginDependenciesResolver;
 
+    @Requirement
+    private RuntimeInformation runtimeInformation;
+
     private PluginDescriptorBuilder builder = new PluginDescriptorBuilder();
 
     public synchronized PluginDescriptor getPluginDescriptor( Plugin plugin, List<RemoteRepository>
repositories, RepositorySystemSession session )
@@ -134,11 +139,15 @@ public class DefaultMavenPluginManager
 
         if ( pluginDescriptor == null )
         {
-            Artifact pluginArtifact =
-                RepositoryUtils.toArtifact( pluginDependenciesResolver.resolve( plugin, repositories,
session ) );
+            org.sonatype.aether.artifact.Artifact artifact =
+                pluginDependenciesResolver.resolve( plugin, repositories, session );
+
+            Artifact pluginArtifact = RepositoryUtils.toArtifact( artifact );
 
             pluginDescriptor = extractPluginDescriptor( pluginArtifact, plugin );
 
+            pluginDescriptor.setRequiredMavenVersion( artifact.getProperty( "requiredMavenVersion",
null ) );
+
             pluginDescriptorCache.put( cacheKey, pluginDescriptor );
         }
 
@@ -261,6 +270,27 @@ public class DefaultMavenPluginManager
         return mojoDescriptor;
     }
 
+    public void checkRequiredMavenVersion( PluginDescriptor pluginDescriptor )
+        throws PluginIncompatibleException
+    {
+        String requiredMavenVersion = pluginDescriptor.getRequiredMavenVersion();
+        if ( StringUtils.isNotBlank( requiredMavenVersion ) )
+        {
+            try
+            {
+                if ( !runtimeInformation.isMavenVersion( requiredMavenVersion ) )
+                {
+                    throw new PluginIncompatibleException( pluginDescriptor.getPlugin(),
"The plugin "
+                        + pluginDescriptor.getId() + " requires Maven version " + requiredMavenVersion
);
+                }
+            }
+            catch ( RuntimeException e )
+            {
+                logger.warn( "Could not verify plugin's Maven prerequisite: " + e.getMessage()
);
+            }
+        }
+    }
+
     public synchronized void setupPluginRealm( PluginDescriptor pluginDescriptor, MavenSession
session,
                                                ClassLoader parent, List<String> imports,
DependencyFilter filter )
         throws PluginResolutionException, PluginContainerException

Modified: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultPluginDependenciesResolver.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultPluginDependenciesResolver.java?rev=1055174&r1=1055173&r2=1055174&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultPluginDependenciesResolver.java
(original)
+++ maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultPluginDependenciesResolver.java
Tue Jan  4 20:32:41 2011
@@ -19,7 +19,9 @@ package org.apache.maven.plugin.internal
  * under the License.
  */
 
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.maven.ArtifactFilterManager;
 import org.apache.maven.RepositoryUtils;
@@ -40,9 +42,13 @@ import org.sonatype.aether.graph.Depende
 import org.sonatype.aether.graph.DependencyNode;
 import org.sonatype.aether.graph.DependencyVisitor;
 import org.sonatype.aether.repository.RemoteRepository;
+import org.sonatype.aether.resolution.ArtifactDescriptorException;
+import org.sonatype.aether.resolution.ArtifactDescriptorRequest;
+import org.sonatype.aether.resolution.ArtifactDescriptorResult;
 import org.sonatype.aether.resolution.ArtifactRequest;
 import org.sonatype.aether.resolution.ArtifactResolutionException;
 import org.sonatype.aether.util.DefaultRepositorySystemSession;
+import org.sonatype.aether.util.FilterRepositorySystemSession;
 import org.sonatype.aether.util.artifact.DefaultArtifact;
 import org.sonatype.aether.util.artifact.JavaScopes;
 import org.sonatype.aether.util.filter.AndDependencyFilter;
@@ -88,6 +94,36 @@ public class DefaultPluginDependenciesRe
 
         try
         {
+            RepositorySystemSession pluginSession = new FilterRepositorySystemSession( session
)
+            {
+                @Override
+                public boolean isIgnoreMissingArtifactDescriptor()
+                {
+                    return false;
+                }
+            };
+
+            ArtifactDescriptorRequest request =
+                new ArtifactDescriptorRequest( pluginArtifact, repositories, REPOSITORY_CONTEXT
);
+            ArtifactDescriptorResult result = repoSystem.readArtifactDescriptor( pluginSession,
request );
+
+            pluginArtifact = result.getArtifact();
+
+            String requiredMavenVersion = (String) result.getProperties().get( "prerequisites.maven"
);
+            if ( requiredMavenVersion != null )
+            {
+                Map<String, String> props = new LinkedHashMap<String, String>(
pluginArtifact.getProperties() );
+                props.put( "requiredMavenVersion", requiredMavenVersion );
+                pluginArtifact = pluginArtifact.setProperties( props );
+            }
+        }
+        catch ( ArtifactDescriptorException e )
+        {
+            throw new PluginResolutionException( plugin, e );
+        }
+
+        try
+        {
             ArtifactRequest request = new ArtifactRequest( pluginArtifact, repositories,
REPOSITORY_CONTEXT );
             pluginArtifact = repoSystem.resolveArtifact( session, request ).getArtifact();
         }

Modified: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java?rev=1055174&r1=1055173&r2=1055174&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java
(original)
+++ maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java
Tue Jan  4 20:32:41 2011
@@ -22,14 +22,19 @@ package org.apache.maven.plugin.version.
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.TreeSet;
 
 import org.apache.maven.artifact.repository.metadata.Metadata;
 import org.apache.maven.artifact.repository.metadata.Versioning;
 import org.apache.maven.artifact.repository.metadata.io.MetadataReader;
 import org.apache.maven.model.Build;
 import org.apache.maven.model.Plugin;
+import org.apache.maven.plugin.MavenPluginManager;
+import org.apache.maven.plugin.PluginResolutionException;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
 import org.apache.maven.plugin.version.PluginVersionRequest;
 import org.apache.maven.plugin.version.PluginVersionResolutionException;
 import org.apache.maven.plugin.version.PluginVersionResolver;
@@ -43,12 +48,15 @@ import org.sonatype.aether.RepositoryLis
 import org.sonatype.aether.RepositorySystem;
 import org.sonatype.aether.RepositorySystemSession;
 import org.sonatype.aether.repository.ArtifactRepository;
-import org.sonatype.aether.repository.LocalRepository;
 import org.sonatype.aether.repository.RemoteRepository;
 import org.sonatype.aether.resolution.MetadataRequest;
 import org.sonatype.aether.resolution.MetadataResult;
 import org.sonatype.aether.util.listener.DefaultRepositoryEvent;
 import org.sonatype.aether.util.metadata.DefaultMetadata;
+import org.sonatype.aether.util.version.GenericVersionScheme;
+import org.sonatype.aether.version.InvalidVersionSpecificationException;
+import org.sonatype.aether.version.Version;
+import org.sonatype.aether.version.VersionScheme;
 
 /**
  * Resolves a version for a plugin.
@@ -72,6 +80,9 @@ public class DefaultPluginVersionResolve
     @Requirement
     private MetadataReader metadataReader;
 
+    @Requirement
+    private MavenPluginManager pluginManager;
+
     public PluginVersionResult resolve( PluginVersionRequest request )
         throws PluginVersionResolutionException
     {
@@ -118,8 +129,6 @@ public class DefaultPluginVersionResolve
 
         List<MetadataResult> results = repositorySystem.resolveMetadata( request.getRepositorySession(),
requests );
 
-        LocalRepository localRepo = request.getRepositorySession().getLocalRepository();
-
         Versions versions = new Versions();
 
         for ( MetadataResult res : results )
@@ -133,24 +142,137 @@ public class DefaultPluginVersionResolve
             mergeMetadata( request.getRepositorySession(), versions, res.getMetadata(), repository
);
         }
 
+        selectVersion( result, request, versions );
+
+        return result;
+    }
+
+    private void selectVersion( DefaultPluginVersionResult result, PluginVersionRequest request,
Versions versions )
+        throws PluginVersionResolutionException
+    {
+        String version = null;
+        ArtifactRepository repo = null;
+
         if ( StringUtils.isNotEmpty( versions.releaseVersion ) )
         {
-            result.setVersion( versions.releaseVersion );
-            result.setRepository( ( versions.releaseRepository == null ) ? localRepo : versions.releaseRepository
);
+            version = versions.releaseVersion;
+            repo = versions.releaseRepository;
         }
         else if ( StringUtils.isNotEmpty( versions.latestVersion ) )
         {
-            result.setVersion( versions.latestVersion );
-            result.setRepository( ( versions.latestRepository == null ) ? localRepo : versions.latestRepository
);
+            version = versions.latestVersion;
+            repo = versions.latestRepository;
+        }
+        if ( version != null && !isCompatible( request, version ) )
+        {
+            versions.versions.remove( version );
+            version = null;
+        }
+
+        if ( version == null )
+        {
+            VersionScheme versionScheme = new GenericVersionScheme();
+
+            TreeSet<Version> releases = new TreeSet<Version>( Collections.reverseOrder()
);
+            TreeSet<Version> snapshots = new TreeSet<Version>( Collections.reverseOrder()
);
+
+            for ( String ver : versions.versions.keySet() )
+            {
+                try
+                {
+                    Version v = versionScheme.parseVersion( ver );
+
+                    if ( ver.endsWith( "-SNAPSHOT" ) )
+                    {
+                        snapshots.add( v );
+                    }
+                    else
+                    {
+                        releases.add( v );
+                    }
+                }
+                catch ( InvalidVersionSpecificationException e )
+                {
+                    continue;
+                }
+            }
+
+            for ( Version v : releases )
+            {
+                String ver = v.toString();
+                if ( isCompatible( request, ver ) )
+                {
+                    version = ver;
+                    repo = versions.versions.get( version );
+                    break;
+                }
+            }
+
+            if ( version == null )
+            {
+                for ( Version v : snapshots )
+                {
+                    String ver = v.toString();
+                    if ( isCompatible( request, ver ) )
+                    {
+                        version = ver;
+                        repo = versions.versions.get( version );
+                        break;
+                    }
+                }
+            }
+        }
+
+        if ( version != null )
+        {
+            result.setVersion( version );
+            result.setRepository( repo );
         }
         else
         {
-            throw new PluginVersionResolutionException( request.getGroupId(), request.getArtifactId(),
localRepo,
+            throw new PluginVersionResolutionException( request.getGroupId(), request.getArtifactId(),
+                                                        request.getRepositorySession().getLocalRepository(),
                                                         request.getRepositories(),
                                                         "Plugin not found in any plugin repository"
);
         }
+    }
 
-        return result;
+    private boolean isCompatible( PluginVersionRequest request, String version )
+    {
+        Plugin plugin = new Plugin();
+        plugin.setGroupId( request.getGroupId() );
+        plugin.setArtifactId( request.getArtifactId() );
+        plugin.setVersion( version );
+
+        PluginDescriptor pluginDescriptor;
+
+        try
+        {
+            pluginDescriptor =
+                pluginManager.getPluginDescriptor( plugin, request.getRepositories(), request.getRepositorySession()
);
+        }
+        catch ( PluginResolutionException e )
+        {
+            logger.debug( "Ignoring unresolvable plugin version " + version, e );
+            return false;
+        }
+        catch ( Exception e )
+        {
+            // ignore for now and delay failure to higher level processing
+            return true;
+        }
+
+        try
+        {
+            pluginManager.checkRequiredMavenVersion( pluginDescriptor );
+        }
+        catch ( Exception e )
+        {
+            logger.debug( "Ignoring incompatible plugin version " + version + ": " + e.getMessage()
);
+            return false;
+        }
+
+        return true;
     }
 
     private void mergeMetadata( RepositorySystemSession session, Versions versions,
@@ -209,6 +331,14 @@ public class DefaultPluginVersionResolve
                 versions.latestTimestamp = timestamp;
                 versions.latestRepository = repository;
             }
+
+            for ( String version : versioning.getVersions() )
+            {
+                if ( !versions.versions.containsKey( version ) )
+                {
+                    versions.versions.put( version, repository );
+                }
+            }
         }
     }
 
@@ -266,6 +396,8 @@ public class DefaultPluginVersionResolve
 
         ArtifactRepository latestRepository;
 
+        Map<String, ArtifactRepository> versions = new LinkedHashMap<String, ArtifactRepository>();
+
     }
 
 }

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/
------------------------------------------------------------------------------
    bugtraq:label = Enter issue ID:

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/
------------------------------------------------------------------------------
    bugtraq:message = Issue id: %BUGID%

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/
------------------------------------------------------------------------------
    bugtraq:number = false

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/
------------------------------------------------------------------------------
    bugtraq:url = http://jira.codehaus.org/browse/%BUGID%

Added: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java?rev=1055174&view=auto
==============================================================================
--- maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java
(added)
+++ maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java
Tue Jan  4 20:32:41 2011
@@ -0,0 +1,49 @@
+package org.apache.maven.rtinfo;
+
+/*
+ * 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.
+ */
+
+/**
+ * Provides information about the current Maven runtime.
+ * 
+ * @since 3.0.2
+ */
+public interface RuntimeInformation
+{
+
+    /**
+     * Retrieves the current Maven version, for example "3.0.2".
+     * 
+     * @return The current Maven version or an empty string if unknown, never {@code null}.
+     */
+    String getMavenVersion();
+
+    /**
+     * Checks whether the current Maven runtime matches the specified version range. A version
range can either use the
+     * usual mathematical syntax "[2.0.10,2.1.0),[3.0,)" or use a single version "2.2.1".
The latter is a short form for
+     * "[2.2.1,)", i.e. denotes the minimum version required.
+     * 
+     * @param versionRange The version range to match the current Maven runtime against,
must not be {@code null}.
+     * @return {@code true} if the current Maven runtime matches the specified version range,
{@code false} otherwise.
+     * @throws IllegalArgumentException If the specified version range is {@code null}, empty
or otherwise not a valid
+     *             version specification.
+     */
+    boolean isMavenVersion( String versionRange );
+
+}

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/
------------------------------------------------------------------------------
    bugtraq:label = Enter issue ID:

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/
------------------------------------------------------------------------------
    bugtraq:message = Issue id: %BUGID%

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/
------------------------------------------------------------------------------
    bugtraq:number = false

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/
------------------------------------------------------------------------------
    bugtraq:url = http://jira.codehaus.org/browse/%BUGID%

Added: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java?rev=1055174&view=auto
==============================================================================
--- maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java
(added)
+++ maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java
Tue Jan  4 20:32:41 2011
@@ -0,0 +1,149 @@
+package org.apache.maven.rtinfo.internal;
+
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import org.apache.maven.rtinfo.RuntimeInformation;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
+import org.codehaus.plexus.logging.Logger;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
+import org.sonatype.aether.util.version.GenericVersionScheme;
+import org.sonatype.aether.version.InvalidVersionSpecificationException;
+import org.sonatype.aether.version.Version;
+import org.sonatype.aether.version.VersionConstraint;
+import org.sonatype.aether.version.VersionScheme;
+
+/**
+ * Provides information about the current Maven runtime.
+ */
+@Component( role = RuntimeInformation.class )
+public class DefaultRuntimeInformation
+    implements RuntimeInformation
+{
+
+    @Requirement
+    private Logger logger;
+
+    private String mavenVersion;
+
+    public String getMavenVersion()
+    {
+        if ( mavenVersion == null )
+        {
+            Properties props = new Properties();
+
+            String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
+
+            InputStream is = DefaultRuntimeInformation.class.getResourceAsStream( "/" + resource
);
+            if ( is != null )
+            {
+                try
+                {
+                    props.load( is );
+                }
+                catch ( IOException e )
+                {
+                    String msg = "Could not parse " + resource + ", Maven runtime information
not available";
+                    if ( logger.isDebugEnabled() )
+                    {
+                        logger.warn( msg, e );
+                    }
+                    else
+                    {
+                        logger.warn( msg );
+                    }
+                }
+                finally
+                {
+                    IOUtil.close( is );
+                }
+            }
+            else
+            {
+                logger.warn( "Could not locate " + resource + " on classpath, Maven runtime
information not available" );
+            }
+
+            String version = props.getProperty( "version", "" ).trim();
+
+            if ( !version.startsWith( "${" ) )
+            {
+                mavenVersion = version;
+            }
+            else
+            {
+                mavenVersion = "";
+            }
+        }
+
+        return mavenVersion;
+    }
+
+    public boolean isMavenVersion( String versionRange )
+    {
+        VersionScheme versionScheme = new GenericVersionScheme();
+
+        if ( versionRange == null )
+        {
+            throw new IllegalArgumentException( "Version range must not be null" );
+        }
+        if ( StringUtils.isBlank( versionRange ) )
+        {
+            throw new IllegalArgumentException( "Version range must not be empty" );
+        }
+
+        VersionConstraint constraint;
+        try
+        {
+            constraint = versionScheme.parseVersionConstraint( versionRange );
+        }
+        catch ( InvalidVersionSpecificationException e )
+        {
+            throw new IllegalArgumentException( e.getMessage(), e );
+        }
+
+        Version current;
+        try
+        {
+            String mavenVersion = getMavenVersion();
+            if ( mavenVersion.length() <= 0 )
+            {
+                throw new IllegalStateException( "Could not determine current Maven version"
);
+            }
+
+            current = versionScheme.parseVersion( mavenVersion );
+        }
+        catch ( InvalidVersionSpecificationException e )
+        {
+            throw new IllegalStateException( "Could not parse current Maven version: " +
e.getMessage(), e );
+        }
+
+        if ( constraint.getRanges().isEmpty() )
+        {
+            return constraint.getVersion().compareTo( current ) <= 0;
+        }
+        return constraint.containsVersion( current );
+    }
+
+}

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/
------------------------------------------------------------------------------
    bugtraq:label = Enter issue ID:

Propchange: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/
------------------------------------------------------------------------------
    bugtraq:message = Issue id: %BUGID%

Propchange: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/
------------------------------------------------------------------------------
    bugtraq:number = false

Propchange: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/
------------------------------------------------------------------------------
    bugtraq:url = http://jira.codehaus.org/browse/%BUGID%

Propchange: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/
------------------------------------------------------------------------------
    bugtraq:label = Enter issue ID:

Propchange: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/
------------------------------------------------------------------------------
    bugtraq:message = Issue id: %BUGID%

Propchange: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/
------------------------------------------------------------------------------
    bugtraq:number = false

Propchange: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/
------------------------------------------------------------------------------
    bugtraq:url = http://jira.codehaus.org/browse/%BUGID%

Added: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java?rev=1055174&view=auto
==============================================================================
--- maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java
(added)
+++ maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java
Tue Jan  4 20:32:41 2011
@@ -0,0 +1,81 @@
+package org.apache.maven.rtinfo.internal;
+
+/*
+ * 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.
+ */
+
+import org.apache.maven.rtinfo.RuntimeInformation;
+import org.codehaus.plexus.PlexusTestCase;
+
+public class DefaultRuntimeInformationTest
+    extends PlexusTestCase
+{
+
+    public void testGetMavenVersion()
+        throws Exception
+    {
+        RuntimeInformation rtInfo = lookup( RuntimeInformation.class );
+
+        String mavenVersion = rtInfo.getMavenVersion();
+        assertNotNull( mavenVersion );
+        assertTrue( mavenVersion.length() > 0 );
+    }
+
+    public void testIsMavenVersion()
+        throws Exception
+    {
+        RuntimeInformation rtInfo = lookup( RuntimeInformation.class );
+
+        assertTrue( rtInfo.isMavenVersion( "2.0" ) );
+        assertFalse( rtInfo.isMavenVersion( "9.9" ) );
+
+        assertTrue( rtInfo.isMavenVersion( "[2.0.11,2.1.0),[3.0,)" ) );
+        assertFalse( rtInfo.isMavenVersion( "[9.0,)" ) );
+
+        try
+        {
+            rtInfo.isMavenVersion( "[3.0," );
+            fail( "Bad version range wasn't rejected" );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            assertTrue( true );
+        }
+
+        try
+        {
+            rtInfo.isMavenVersion( "" );
+            fail( "Bad version range wasn't rejected" );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            assertTrue( true );
+        }
+
+        try
+        {
+            rtInfo.isMavenVersion( null );
+            fail( "Bad version range wasn't rejected" );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            assertTrue( true );
+        }
+    }
+
+}

Propchange: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/maven-3/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java?rev=1055174&r1=1055173&r2=1055174&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java
(original)
+++ maven/maven-3/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java
Tue Jan  4 20:32:41 2011
@@ -79,6 +79,8 @@ public class PluginDescriptor
 
     private String description;
 
+    private String requiredMavenVersion;
+
     private Plugin plugin;
 
     private Artifact pluginArtifact;
@@ -89,7 +91,7 @@ public class PluginDescriptor
     //
     // ----------------------------------------------------------------------
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings( { "unchecked", "rawtypes" } )
     public List<MojoDescriptor> getMojos()
     {
         return (List) getComponents();
@@ -332,6 +334,16 @@ public class PluginDescriptor
         return description;
     }
 
+    public void setRequiredMavenVersion( String requiredMavenVersion )
+    {
+        this.requiredMavenVersion = requiredMavenVersion;
+    }
+
+    public String getRequiredMavenVersion()
+    {
+        return requiredMavenVersion;
+    }
+
     public void setPlugin( Plugin plugin )
     {
         this.plugin = plugin;



Mime
View raw message