PR: MNG-415

implement provided scope


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@190930 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-06-16 15:02:07 +00:00
parent 4aeca8e8a9
commit ac2d8a7e83
17 changed files with 190 additions and 70 deletions

View File

@ -39,6 +39,8 @@ public interface Artifact
String SCOPE_RUNTIME = "runtime";
String SCOPE_PROVIDED = "provided";
String getGroupId();
String getArtifactId();

View File

@ -49,7 +49,7 @@ public class DefaultArtifactFactory
{
desiredScope = scope;
}
else if ( Artifact.SCOPE_TEST.equals( scope ) )
else if ( Artifact.SCOPE_TEST.equals( scope ) || Artifact.SCOPE_PROVIDED.equals( scope ) )
{
return null;
}
@ -66,6 +66,11 @@ public class DefaultArtifactFactory
desiredScope = Artifact.SCOPE_TEST;
}
if ( Artifact.SCOPE_PROVIDED.equals( inheritedScope ) )
{
desiredScope = Artifact.SCOPE_PROVIDED;
}
DefaultArtifact artifact = new DefaultArtifact( groupId, artifactId, version, desiredScope, type, classifier );
return artifact;

View File

@ -34,28 +34,34 @@ public class ScopeArtifactFilter
private final boolean testScope;
private final boolean providedScope;
public ScopeArtifactFilter( String scope )
{
if ( DefaultArtifact.SCOPE_COMPILE.equals( scope ) )
{
providedScope = true;
compileScope = true;
runtimeScope = false;
testScope = false;
}
else if ( DefaultArtifact.SCOPE_RUNTIME.equals( scope ) )
{
providedScope = false;
compileScope = true;
runtimeScope = true;
testScope = false;
}
else if ( DefaultArtifact.SCOPE_TEST.equals( scope ) )
{
providedScope = false;
compileScope = true;
runtimeScope = true;
testScope = true;
}
else
{
providedScope = false;
compileScope = false;
runtimeScope = false;
testScope = false;
@ -76,9 +82,13 @@ public class ScopeArtifactFilter
{
return testScope;
}
else if ( DefaultArtifact.SCOPE_PROVIDED.equals( artifact.getScope() ) )
{
return providedScope;
}
else
{
// TODO: should this be true?
// TODO: should this be true? Does it even happen?
return false;
}
}

View File

@ -383,6 +383,13 @@ public class Verifier
private void verifyExpectedResult( String line )
throws VerificationException
{
boolean wanted = true;
if ( line.startsWith( "!" ) )
{
line = line.substring( 1 );
wanted = false;
}
if ( line.indexOf( "!/" ) > 0 )
{
String urlString = "jar:file:" + basedir + "/" + line;
@ -395,14 +402,24 @@ public class Verifier
if ( is == null )
{
throw new VerificationException( "Expected JAR resource was not found: " + line );
if ( wanted )
{
throw new VerificationException( "Expected JAR resource was not found: " + line );
}
}
else
{
if ( !wanted )
{
throw new VerificationException( "Unwanted JAR resource was found: " + line );
}
}
is.close();
}
catch ( Exception e )
{
throw new VerificationException( "Expected JAR resource was not found: " + line );
throw new VerificationException( "Error looking for JAR resource", e );
}
}
else
@ -416,7 +433,17 @@ public class Verifier
if ( !expectedFile.exists() )
{
throw new VerificationException( "Expected file was not found: " + expectedFile.getPath() );
if ( wanted )
{
throw new VerificationException( "Expected file was not found: " + expectedFile.getPath() );
}
}
else
{
if ( !wanted )
{
throw new VerificationException( "Unwanted file was found: " + expectedFile.getPath() );
}
}
}
}

View File

@ -1,6 +1,9 @@
target/classes/org/apache/maven/it0016/Person.class
target/maven-core-it0016-1.0/index.html
target/maven-core-it0016-1.0/WEB-INF/classes/org/apache/maven/it0016/Person.class
target/maven-core-it0016-1.0/WEB-INF/lib/commons-logging-1.0.3.jar
!target/maven-core-it0016-1.0/WEB-INF/lib/servletapi-2.4-20040521.jar
target/maven-core-it0016-1.0.war
target/maven-core-it0016-1.0.war!/index.html
target/maven-core-it0016-1.0.war!/WEB-INF/classes/org/apache/maven/it0016/Person.class
target/maven-core-it0016-1.0.war!/WEB-INF/lib/commons-logging-1.0.3.jar

View File

@ -4,6 +4,20 @@
<artifactId>maven-core-it0016</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>servletapi</groupId>
<artifactId>servletapi</artifactId>
<version>2.4-20040521</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -104,12 +104,6 @@ public class MBoot
private boolean online = true;
private static final String SCOPE_TEST = "test";
private static final String SCOPE_COMPILE = "compile";
private static final String SCOPE_RUNTIME = "runtime";
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
@ -444,7 +438,7 @@ public class MBoot
{
FileUtils.copyFileToDirectory( source, core );
}
else if ( !d.getScope().equals( SCOPE_TEST ) )
else if ( !d.getScope().equals( Dependency.SCOPE_TEST ) )
{
// only compile and runtime
FileUtils.copyFileToDirectory( source, lib );
@ -588,12 +582,12 @@ public class MBoot
if ( new File( generatedSources ).exists() )
{
compile( reader.getDependencies(), sources, classes, null, generatedSources, SCOPE_COMPILE,
compile( reader.getDependencies(), sources, classes, null, generatedSources, Dependency.SCOPE_COMPILE,
localRepository );
}
else
{
compile( reader.getDependencies(), sources, classes, null, null, SCOPE_COMPILE, localRepository );
compile( reader.getDependencies(), sources, classes, null, null, Dependency.SCOPE_COMPILE, localRepository );
}
// ----------------------------------------------------------------------
@ -624,7 +618,7 @@ public class MBoot
Collection testDependencies = new ArrayList( reader.getDependencies() );
compile( testDependencies, testSources, testClasses, classes, null, SCOPE_TEST, localRepository );
compile( testDependencies, testSources, testClasses, classes, null, Dependency.SCOPE_TEST, localRepository );
// ----------------------------------------------------------------------
// Test resources
@ -825,7 +819,7 @@ public class MBoot
List depList = new ArrayList( reader.getDependencies() );
depList.addAll( surefireDependencies );
List classpath = classpath( depList, null, SCOPE_TEST, localRepository );
List classpath = classpath( depList, null, Dependency.SCOPE_TEST, localRepository );
classpath.add( classes );
classpath.add( testClasses );
@ -851,21 +845,21 @@ public class MBoot
String element = localRepository.getArtifactFile( d ).getAbsolutePath();
if ( SCOPE_COMPILE.equals( scope ) )
if ( Dependency.SCOPE_COMPILE.equals( scope ) )
{
if ( d.getScope().equals( SCOPE_COMPILE ) )
if ( d.getScope().equals( Dependency.SCOPE_COMPILE ) )
{
classpath.add( element );
}
}
else if ( SCOPE_RUNTIME.equals( scope ) )
else if ( Dependency.SCOPE_RUNTIME.equals( scope ) )
{
if ( d.getScope().equals( SCOPE_COMPILE ) || d.getScope().equals( SCOPE_RUNTIME ) )
if ( d.getScope().equals( Dependency.SCOPE_COMPILE ) || d.getScope().equals( Dependency.SCOPE_RUNTIME ) )
{
classpath.add( element );
}
}
else if ( SCOPE_TEST.equals( scope ) )
else if ( Dependency.SCOPE_TEST.equals( scope ) )
{
classpath.add( element );
}

View File

@ -37,10 +37,16 @@ public class Dependency
private String type = "jar";
private String scope = "compile";
private String scope = SCOPE_COMPILE;
private String resolvedVersion;
public static final String SCOPE_TEST = "test";
public static final String SCOPE_COMPILE = "compile";
public static final String SCOPE_RUNTIME = "runtime";
public Dependency()
{
}

View File

@ -357,9 +357,9 @@ public class ModelReader
Dependency d = (Dependency) i.next();
// Do we care about runtime here?
if ( "test".equals( inheritedScope ) )
if ( Dependency.SCOPE_TEST.equals( inheritedScope ) )
{
d.setScope( "test" );
d.setScope( Dependency.SCOPE_TEST );
}
if ( !hasDependency( d, target ) )
@ -376,7 +376,7 @@ public class ModelReader
{
// We only care about pushing in compile scope dependencies I think
// if not, we'll need to be able to get the original and pick the appropriate scope
if ( d.getScope().equals( "compile" ) )
if ( d.getScope().equals( Dependency.SCOPE_COMPILE ) )
{
dependencies.remove( conflictId );
}

View File

@ -15,5 +15,10 @@
<artifactId>maven-archiver</artifactId>
<version>2.0-alpha-2</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
</model>

View File

@ -170,7 +170,7 @@ public class WarMojo
// TODO: scope handler
// Include runtime and compile time libraries
if ( "jar".equals( artifact.getType() ) && !Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
if ( "jar".equals( artifact.getType() ) && !Artifact.SCOPE_TEST.equals( artifact.getScope() ) && !Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ) )
{
FileUtils.copyFileToDirectory( artifact.getFile(), libDirectory );
}

View File

@ -249,7 +249,7 @@ public class MavenProject
if ( isAddedToClasspath( a ) )
{
// TODO: let the scope handler deal with this
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) )
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) )
{
File file = a.getFile();
if ( file == null )
@ -279,7 +279,7 @@ public class MavenProject
Artifact a = (Artifact) i.next();
// TODO: let the scope handler deal with this
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) )
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) )
{
Dependency dependency = new Dependency();

View File

@ -52,16 +52,15 @@ public class ProjectClasspathTest
assertNotNull( "Test project can't be null!", project );
checkArtifactIdScope( project, "provided", "provided" );
checkArtifactIdScope( project, "test", "test" );
checkArtifactIdScope( project, "compile", "compile" );
checkArtifactIdScope( project, "runtime", "runtime" );
checkArtifactIdScope( project, "default", "compile" );
checkInheritedArtifactIdScope( project, "compile", "compile" );
checkInheritedArtifactIdScope( project, "runtime", "runtime" );
checkInheritedArtifactIdScope( project, "default", "compile" );
// check all transitive deps of a test dependency are test, except test which is skipped
// check all transitive deps of a test dependency are test, except test and provided which is skipped
artifact = getArtifact( project, "maven-test-test", "scope-provided" );
assertNull( "Check no provided dependencies are transitive", artifact );
artifact = getArtifact( project, "maven-test-test", "scope-test" );
assertNull( "Check no test dependencies are transitive", artifact );
artifact = getArtifact( project, "maven-test-test", "scope-compile" );
@ -71,28 +70,38 @@ public class ProjectClasspathTest
artifact = getArtifact( project, "maven-test-test", "scope-runtime" );
assertEquals( "Check scope", "test", artifact.getScope() );
// check all transitive deps of a provided dependency are provided scope, except for test
checkGroupIdScope( project, "provided", "maven-test-provided" );
artifact = getArtifact( project, "maven-test-provided", "scope-runtime" );
assertEquals( "Check scope", "provided", artifact.getScope() );
// check all transitive deps of a runtime dependency are runtime scope, except for test
checkGroupIdScope( project, "runtime", "runtime" );
checkGroupIdScope( project, "runtime", "maven-test-runtime" );
artifact = getArtifact( project, "maven-test-runtime", "scope-runtime" );
assertEquals( "Check scope", "runtime", artifact.getScope() );
// check all transitive deps of a compile dependency are compile scope, except for runtime and test
checkGroupIdScope( project, "compile", "compile" );
checkGroupIdScope( project, "compile", "maven-test-compile" );
artifact = getArtifact( project, "maven-test-compile", "scope-runtime" );
assertEquals( "Check scope", "runtime", artifact.getScope() );
// check all transitive deps of a default dependency are compile scope, except for runtime and test
checkGroupIdScope( project, "default", "compile" );
checkGroupIdScope( project, "compile", "maven-test-default" );
artifact = getArtifact( project, "maven-test-default", "scope-runtime" );
assertEquals( "Check scope", "runtime", artifact.getScope() );
}
private void checkGroupIdScope( MavenProject project, String scope, String scopeValue )
private void checkGroupIdScope( MavenProject project, String scopeValue, String groupId )
{
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" );
assertNull( "Check test dependency is not transitive", artifact );
artifact = getArtifact( project, groupId, "scope-provided" );
assertNull( "Check provided dependency is not transitive", artifact );
artifact = getArtifact( project, groupId, "scope-default" );
assertEquals( "Check scope", scopeValue, artifact.getScope() );
artifact = getArtifact( project, groupId, "scope-runtime" );
assertEquals( "Check scope", "runtime", artifact.getScope() );
}
private void checkArtifactIdScope( MavenProject project, String scope, String scopeValue )
@ -102,13 +111,6 @@ public class ProjectClasspathTest
assertEquals( "Check scope", scopeValue, artifact.getScope() );
}
private void checkInheritedArtifactIdScope( MavenProject project, String scope, String scopeValue )
{
String artifactId = "scope-" + scope;
Artifact artifact = getArtifact( project, "maven-inherited", artifactId );
assertEquals( "Check scope", scopeValue, artifact.getScope() );
}
private Artifact getArtifact( MavenProject project, String groupId, String artifactId )
{
for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); )

View File

@ -35,6 +35,13 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>maven-test</groupId>
<artifactId>scope-provided</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>maven-inherited</groupId>
<artifactId>scope-default</artifactId>
@ -45,7 +52,7 @@
<groupId>maven-inherited</groupId>
<artifactId>scope-compile</artifactId>
<version>1.0</version>
<scope>runtime</scope>
<scope>compile</scope>
</dependency>
<dependency>
@ -54,6 +61,20 @@
<version>1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>maven-inherited</groupId>
<artifactId>scope-test</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>maven-inherited</groupId>
<artifactId>scope-provided</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</model>

View File

@ -32,27 +32,6 @@
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>maven-inherited</groupId>
<artifactId>scope-compile</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>maven-inherited</groupId>
<artifactId>scope-default</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>maven-inherited</groupId>
<artifactId>scope-runtime</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</model>

View File

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

View File

@ -52,13 +52,17 @@
various build tasks.
</p>
<p>
There are 3 scopes available:
There are 4 scopes available:
</p>
<ul>
<li>
<b>compile</b> - this is the default scope, used if none is specified. Compile dependencies are available
in all classpaths.
</li>
<li>
<b>provided</b> - this is much like compile, but indicates you expect the JDK or a container to provide it.
It is only available on the compilation classpath, and is not transitive.
</li>
<li>
<b>runtime</b> - this scope indicates that the dependency is not required for compilation, but is for execution.
It is in the runtime and test classpaths, but not the compile classpath.
@ -78,24 +82,35 @@
<tr>
<th></th>
<th>compile</th>
<th>provided</th>
<th>runtime</th>
<th>test</th>
</tr>
<tr>
<th>compile</th>
<td>compile (*)</td>
<td>-</td>
<td>runtime</td>
<td>-</td>
</tr>
<tr>
<th>provided</th>
<td>provided</td>
<td>provided</td>
<td>provided</td>
<td>-</td>
</tr>
<tr>
<th>runtime</th>
<td>runtime</td>
<td>-</td>
<td>runtime</td>
<td>-</td>
</tr>
<tr>
<th>test</th>
<td>test</td>
<td>-</td>
<td>test</td>
<td>-</td>
</tr>