Skip to content

Commit

Permalink
[MNG-4840] Prerequisites is not working on m3
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@1055174 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
bentmann committed Jan 4, 2011
1 parent be26167 commit 40fb188
Show file tree
Hide file tree
Showing 14 changed files with 582 additions and 46 deletions.
Expand Up @@ -32,6 +32,7 @@
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;
Expand Down Expand Up @@ -88,6 +89,7 @@ public class DefaultArtifactDescriptorReader
implements ArtifactDescriptorReader, Service
{

@SuppressWarnings( "unused" )
@Requirement
private Logger logger = NullLogger.INSTANCE;

Expand Down Expand Up @@ -195,6 +197,12 @@ public ArtifactDescriptorResult readArtifactDescriptor( RepositorySystemSession

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++ )
Expand Down
Expand Up @@ -22,17 +22,14 @@
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$
*/
Expand All @@ -41,9 +38,9 @@
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;

Expand All @@ -55,33 +52,14 @@ public ArtifactVersion getApplicationVersion()
public void initialize()
throws InitializationException
{
InputStream resourceAsStream = null;
try
{
Properties properties = new Properties();
resourceAsStream = getClass().getClassLoader().getResourceAsStream( MAVEN_PROPERTIES );

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" );
}
String mavenVersion = rtInfo.getMavenVersion();

applicationVersion = new DefaultArtifactVersion( property );
}
catch ( IOException e )
{
throw new InitializationException( "Unable to read properties file from maven-core", e );
}
finally
if ( StringUtils.isEmpty( mavenVersion ) )
{
IOUtil.close( resourceAsStream );
throw new InitializationException( "Unable to read Maven version from maven-core" );
}

applicationVersion = new DefaultArtifactVersion( mavenVersion );
}

}
Expand Up @@ -24,9 +24,11 @@
/**
* 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();
Expand Down
Expand Up @@ -27,10 +27,12 @@
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;
Expand Down Expand Up @@ -64,6 +66,9 @@ public class MojoExecutor
@Requirement
private BuildPluginManager pluginManager;

@Requirement
private MavenPluginManager mavenPluginManager;

@Requirement
private LifecycleDependencyResolver lifeCycleDependencyResolver;

Expand Down Expand Up @@ -156,6 +161,15 @@ private void execute( MavenSession session, MojoExecution mojoExecution, Project
{
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 =
Expand Down
Expand Up @@ -72,7 +72,7 @@ public void put( Key cacheKey, PluginDescriptor pluginDescriptor )
descriptors.put( cacheKey, clone( pluginDescriptor ) );
}

private static PluginDescriptor clone( PluginDescriptor original )
protected static PluginDescriptor clone( PluginDescriptor original )
{
PluginDescriptor clone = null;

Expand All @@ -88,6 +88,7 @@ private static PluginDescriptor clone( PluginDescriptor original )

clone.setName( original.getName() );
clone.setDescription( original.getDescription() );
clone.setRequiredMavenVersion( original.getRequiredMavenVersion() );

clone.setPluginArtifact( ArtifactUtils.copyArtifactSafe( original.getPluginArtifact() ) );

Expand Down
Expand Up @@ -67,6 +67,14 @@ MojoDescriptor getMojoDescriptor( Plugin plugin, String goal, List<RemoteReposit
throws MojoNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
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.
Expand Down
@@ -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 );
}

}
Expand Up @@ -55,6 +55,7 @@
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;
Expand All @@ -64,6 +65,7 @@
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;
Expand Down Expand Up @@ -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 )
Expand All @@ -134,11 +139,15 @@ public synchronized PluginDescriptor getPluginDescriptor( Plugin plugin, List<Re

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 );
}

Expand Down Expand Up @@ -261,6 +270,27 @@ public MojoDescriptor getMojoDescriptor( Plugin plugin, String goal, List<Remote
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
Expand Down
Expand Up @@ -19,7 +19,9 @@
* 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;
Expand All @@ -40,9 +42,13 @@
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;
Expand Down Expand Up @@ -86,6 +92,36 @@ public Artifact resolve( Plugin plugin, List<RemoteRepository> repositories, Rep
{
Artifact pluginArtifact = toArtifact( plugin, session );

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 );
Expand Down

0 comments on commit 40fb188

Please sign in to comment.