tests and repairs for scope

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163486 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-03-08 05:34:52 +00:00
parent a8e6af5ff9
commit b31db60bff
15 changed files with 436 additions and 23 deletions

View File

@ -23,6 +23,7 @@ import org.apache.maven.artifact.repository.ArtifactRepository;
import org.codehaus.plexus.logging.AbstractLogEnabled;
/**
* @todo refactor away
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/

View File

@ -57,7 +57,6 @@ public class DefaultArtifact
/** @todo this should be replaced by type handler */
public DefaultArtifact( String groupId, String artifactId, String version, String type, String extension )
{
// TODO: default should be runtime, except in currently building POM where it is compile.
this( groupId, artifactId, version, SCOPE_COMPILE, type, extension );
}

View File

@ -11,6 +11,7 @@ import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.transform.ArtifactRequestTransformation;
import org.apache.maven.wagon.TransferFailedException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@ -19,6 +20,9 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @todo get rid of {@link AbstractArtifactComponent} and then create an AbstractArtifactResolver that does the transitive boilerplate
*/
public class DefaultArtifactResolver
extends AbstractArtifactComponent
implements ArtifactResolver
@ -175,11 +179,7 @@ public class DefaultArtifactResolver
ArtifactMetadataSource source )
throws ArtifactResolutionException
{
Set s = new HashSet();
s.add( artifact );
return resolveTransitively( s, remoteRepositories, localRepository, source );
return resolveTransitively( Collections.singleton( artifact ), remoteRepositories, localRepository, source );
}
@ -242,7 +242,6 @@ public class DefaultArtifactResolver
try
{
// TODO: need to convert scope compile -> runtime using scope handler
referencedDependencies = source.retrieve( newArtifact, localRepository, remoteRepositories );
}
catch ( ArtifactMetadataRetrievalException e )

View File

@ -79,12 +79,12 @@ public class MavenMetadataSource
if ( mavenProjectBuilder != null )
{
MavenProject project = mavenProjectBuilder.build( metadataArtifact.getFile(), localRepository );
artifacts = createArtifacts( project.getDependencies(), localRepository );
artifacts = createArtifacts( project.getDependencies(), artifact.getScope(), localRepository );
}
else
{
Model model = reader.read( new FileReader( metadataArtifact.getFile() ) );
artifacts = createArtifacts( model.getDependencies(), localRepository );
artifacts = createArtifacts( model.getDependencies(), artifact.getScope(), localRepository );
}
}
catch ( ArtifactResolutionException e )
@ -99,27 +99,40 @@ public class MavenMetadataSource
return artifacts;
}
public Set createArtifacts( List dependencies, ArtifactRepository localRepository )
protected Set createArtifacts( List dependencies, String scope, ArtifactRepository localRepository )
{
Set projectArtifacts = new HashSet();
for ( Iterator i = dependencies.iterator(); i.hasNext(); )
{
Dependency d = (Dependency) i.next();
Artifact artifact = createArtifact( d, localRepository );
Artifact artifact = createArtifact( d, scope, localRepository );
projectArtifacts.add( artifact );
}
return projectArtifacts;
}
public Artifact createArtifact( Dependency dependency, ArtifactRepository localRepository )
protected Artifact createArtifact( Dependency dependency, String scope, ArtifactRepository localRepository )
{
// TODO: duplicated with the ArtifactFactory, localRepository not used (should be used here to resolve path?
Artifact artifact = new DefaultArtifact( dependency.getGroupId(),
dependency.getArtifactId(),
dependency.getVersion(),
dependency.getScope(),
transitiveScope( dependency.getScope(), scope ),
dependency.getType(),
dependency.getType() );
return artifact;
}
private String transitiveScope( String desiredScope, String artifactScope )
{
// TODO: scope handler
if ( Artifact.SCOPE_TEST.equals( artifactScope ) || Artifact.SCOPE_TEST.equals( desiredScope ) )
{
return Artifact.SCOPE_TEST;
}
else
{
return Artifact.SCOPE_RUNTIME;
}
}
}

View File

@ -209,7 +209,8 @@ public class DefaultPluginManager
String version = "1.0-SNAPSHOT";
Artifact pluginArtifact = new DefaultArtifact( "maven", artifactId, version, "plugin", "jar" );
Artifact pluginArtifact = new DefaultArtifact( "maven", artifactId, version, DefaultArtifact.SCOPE_RUNTIME,
"plugin", "jar" );
addPlugin( pluginArtifact, session );
}

View File

@ -177,7 +177,7 @@ public class MavenProject
Artifact a = (Artifact) i.next();
// TODO: let the scope handler deal with this
if ( a.getScope() == null || Artifact.SCOPE_COMPILE.equals( a.getScope() ) )
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) )
{
list.add( a.getPath() );
}
@ -198,9 +198,8 @@ public class MavenProject
if ( isAddedToClasspath( a ) )
{
// TODO: let the scope handler deal with this
if ( a.getScope() == null || Artifact.SCOPE_TEST.equals( a.getScope() ) ||
Artifact.SCOPE_COMPILE.equals( a.getScope() ) ||
Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
if ( Artifact.SCOPE_TEST.equals( a.getScope() ) || Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals(
a.getScope() ) )
{
list.add( a.getPath() );
}
@ -222,8 +221,7 @@ public class MavenProject
if ( isAddedToClasspath( a ) )
{
// TODO: let the scope handler deal with this
if ( a.getScope() == null || Artifact.SCOPE_COMPILE.equals( a.getScope() ) ||
Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
{
list.add( a.getPath() );
}

View File

@ -0,0 +1,98 @@
package org.apache.maven.project;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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.artifact.Artifact;
import org.apache.maven.artifact.MavenMetadataSource;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerNotFoundException;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.resolver.DefaultArtifactResolver;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import java.io.InputStreamReader;
import java.util.Set;
public class ProjectClasspathArtifactResolver extends DefaultArtifactResolver
{
private static class Source extends MavenMetadataSource
{
public Source( ArtifactResolver artifactResolver )
{
super( artifactResolver );
}
public Set retrieve( Artifact artifact, ArtifactRepository localRepository, Set remoteRepositories )
throws ArtifactMetadataRetrievalException
{
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = null;
try
{
String scope = artifact.getArtifactId().substring( "scope-".length() );
String name = "/projects/scope/transitive-" + scope + "-dep.xml";
model = reader.read( new InputStreamReader( getClass().getResourceAsStream( name ) ) );
}
catch ( Exception e )
{
throw new ArtifactMetadataRetrievalException( e );
}
return createArtifacts( model.getDependencies(), artifact.getScope(), localRepository );
}
}
public Artifact resolve( Artifact artifact, Set remoteRepositories, ArtifactRepository localRepository )
throws ArtifactResolutionException
{
return artifact;
}
protected void setLocalRepositoryPath( Artifact artifact, ArtifactRepository localRepository )
throws ArtifactHandlerNotFoundException
{
}
public ArtifactResolutionResult resolveTransitively( Set artifacts, Set remoteRepositories,
ArtifactRepository localRepository,
ArtifactMetadataSource source, ArtifactFilter filter )
throws ArtifactResolutionException
{
return super.resolveTransitively( artifacts, remoteRepositories, localRepository, new Source( this ), filter );
}
public ArtifactResolutionResult resolveTransitively( Set artifacts, Set remoteRepositories,
ArtifactRepository localRepository,
ArtifactMetadataSource source )
throws ArtifactResolutionException
{
return super.resolveTransitively( artifacts, remoteRepositories, localRepository, new Source( this ) );
}
public ArtifactResolutionResult resolveTransitively( Artifact artifact, Set remoteRepositories,
ArtifactRepository localRepository,
ArtifactMetadataSource source )
throws ArtifactResolutionException
{
return super.resolveTransitively( artifact, remoteRepositories, localRepository, new Source( this ) );
}
}

View File

@ -0,0 +1,107 @@
package org.apache.maven.project;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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.MavenTestCase;
import org.apache.maven.artifact.Artifact;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Iterator;
/**
* @todo relocate to maven-artifact in entirety
*/
public class ProjectClasspathTest extends MavenTestCase
{
private String dir = "src/test/resources/projects/scope/";
public void testProjectClasspath()
throws Exception
{
File f = getTestFile( dir + "project-with-scoped-dependencies.xml" );
// XXX: Because this test fails, we resort to crude reflection hacks, see PLX-108 for the solution
// assertEquals( ProjectClasspathArtifactResolver.class.getName(), getContainer().lookup( ArtifactResolver.ROLE ) );
MavenProjectBuilder builder = (MavenProjectBuilder) getContainer().lookup( MavenProjectBuilder.ROLE );
Field declaredField = builder.getClass().getDeclaredField( "artifactResolver" );
boolean acc = declaredField.isAccessible();
declaredField.setAccessible( true );
declaredField.set( builder, new ProjectClasspathArtifactResolver() );
declaredField.setAccessible( acc );
// XXX: end hack
MavenProject project = getProject( f, true );
Artifact artifact;
assertNotNull( "Test project can't be null!", project );
checkArtifactIdScope( project, "test", "test" );
checkArtifactIdScope( project, "compile", "compile" );
checkArtifactIdScope( project, "runtime", "runtime" );
checkArtifactIdScope( project, "default", "compile" );
// check all transitive deps of a test dependency are test scope
checkGroupIdScope( project, "test", "test" );
// check all transitive deps of a runtime dependency are runtime scope, except for test
checkGroupIdScope( project, "runtime", "runtime" );
// check all transitive deps of a compile dependency are runtime scope, except for test
checkGroupIdScope( project, "compile", "runtime" );
// check all transitive deps of a default dependency are runtime scope, except for test
checkGroupIdScope( project, "default", "runtime" );
}
private void checkGroupIdScope( MavenProject project, String scope, String scopeValue )
{
Artifact artifact;
String groupId = "maven-test-" + scope;
artifact = getArtifact( project, groupId, "scope-compile" );
assertEquals( "Check scope", scopeValue, artifact.getScope() );
artifact = getArtifact( project, groupId, "scope-test" );
assertEquals( "Check scope", "test", artifact.getScope() );
artifact = getArtifact( project, groupId, "scope-default" );
assertEquals( "Check scope", scopeValue, artifact.getScope() );
artifact = getArtifact( project, groupId, "scope-runtime" );
assertEquals( "Check scope", scopeValue, artifact.getScope() );
}
private void checkArtifactIdScope( MavenProject project, String scope, String scopeValue )
{
String artifactId = "scope-" + scope;
Artifact artifact = getArtifact( project, "maven-test", artifactId );
assertEquals( "Check scope", scopeValue, artifact.getScope() );
}
private Artifact getArtifact( MavenProject project, String groupId, String artifactId )
{
for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); )
{
Artifact a = (Artifact) i.next();
if ( artifactId.equals( a.getArtifactId() ) && a.getGroupId().equals( groupId ) )
{
return a;
}
}
fail( "Dependency " + artifactId + " not found" );
return null;
}
}

View File

@ -0,0 +1,8 @@
<plexus>
<components>
<component>
<role>org.apache.maven.artifact.ArtifactResolver</role>
<implementation>org.apache.maven.project.ProjectClasspathArtifactResolver</implementation>
</component>
</components>
</plexus>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<groupId>maven</groupId>
<artifactId>maven-project-test</artifactId>
<name>Maven</name>
<version>1.0-beta-9</version>
<dependencies>
<dependency>
<groupId>maven-test</groupId>
<artifactId>scope-default</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>maven-test</groupId>
<artifactId>scope-test</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>maven-test</groupId>
<artifactId>scope-runtime</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>maven-test</groupId>
<artifactId>scope-compile</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<groupId>maven-test</groupId>
<artifactId>scope-compile</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>maven-test-compile</groupId>
<artifactId>scope-default</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>maven-test-compile</groupId>
<artifactId>scope-test</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>maven-test-compile</groupId>
<artifactId>scope-runtime</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>maven-test-compile</groupId>
<artifactId>scope-compile</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<groupId>maven-test</groupId>
<artifactId>scope-default</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>maven-test-default</groupId>
<artifactId>scope-default</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>maven-test-default</groupId>
<artifactId>scope-test</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>maven-test-default</groupId>
<artifactId>scope-runtime</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>maven-test-default</groupId>
<artifactId>scope-compile</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<groupId>maven-test</groupId>
<artifactId>scope-runtime</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>maven-test-runtime</groupId>
<artifactId>scope-default</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>maven-test-runtime</groupId>
<artifactId>scope-test</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>maven-test-runtime</groupId>
<artifactId>scope-runtime</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>maven-test-runtime</groupId>
<artifactId>scope-compile</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<groupId>maven-test</groupId>
<artifactId>scope-test</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>maven-test-test</groupId>
<artifactId>scope-default</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>maven-test-test</groupId>
<artifactId>scope-test</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>maven-test-test</groupId>
<artifactId>scope-runtime</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>maven-test-test</groupId>
<artifactId>scope-compile</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -807,9 +807,7 @@
<version>4.0.0</version>
<description>The scope of the dependency - build, compile, test, runtime</description>
<type>String</type>
<!-- TODO: set default value? see DefaultArtifact first
<defaultValue>runtime</defaultValue>
-->
<defaultValue>compile</defaultValue>
</field>
</fields>
<codeSegments>