[MNG-4840] Prerequisites is not working on m3

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@1055174 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2011-01-04 20:32:41 +00:00
parent be26167029
commit 40fb1880ae
14 changed files with 582 additions and 46 deletions

View File

@ -32,6 +32,7 @@ import org.apache.maven.model.DependencyManagement;
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 DefaultArtifactDescriptorReader
implements ArtifactDescriptorReader, Service
{
@SuppressWarnings( "unused" )
@Requirement
private Logger logger = NullLogger.INSTANCE;
@ -195,6 +197,12 @@ public class DefaultArtifactDescriptorReader
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++ )

View File

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

View File

@ -24,9 +24,11 @@ import org.apache.maven.artifact.versioning.ArtifactVersion;
/**
* 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();

View File

@ -27,10 +27,12 @@ import org.apache.maven.execution.MavenSession;
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;
@ -64,6 +66,9 @@ public class MojoExecutor
@Requirement
private BuildPluginManager pluginManager;
@Requirement
private MavenPluginManager mavenPluginManager;
@Requirement
private LifecycleDependencyResolver lifeCycleDependencyResolver;
@ -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 =

View File

@ -72,7 +72,7 @@ public class DefaultPluginDescriptorCache
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 DefaultPluginDescriptorCache
clone.setName( original.getName() );
clone.setDescription( original.getDescription() );
clone.setRequiredMavenVersion( original.getRequiredMavenVersion() );
clone.setPluginArtifact( ArtifactUtils.copyArtifactSafe( original.getPluginArtifact() ) );

View File

@ -67,6 +67,14 @@ public interface MavenPluginManager
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.

View File

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

View File

@ -55,6 +55,7 @@ import org.apache.maven.plugin.PluginConfigurationException;
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.descriptor.Parameter;
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

View File

@ -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.DependencyFilter;
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;
@ -86,6 +92,36 @@ public class DefaultPluginDependenciesResolver
{
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 );

View File

@ -22,14 +22,19 @@ package org.apache.maven.plugin.version.internal;
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.RepositoryListener;
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 DefaultPluginVersionResolver
@Requirement
private MetadataReader metadataReader;
@Requirement
private MavenPluginManager pluginManager;
public PluginVersionResult resolve( PluginVersionRequest request )
throws PluginVersionResolutionException
{
@ -118,8 +129,6 @@ public class DefaultPluginVersionResolver
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 DefaultPluginVersionResolver
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 DefaultPluginVersionResolver
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 DefaultPluginVersionResolver
ArtifactRepository latestRepository;
Map<String, ArtifactRepository> versions = new LinkedHashMap<String, ArtifactRepository>();
}
}

View File

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

View File

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

View File

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

View File

@ -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;