Author: bentmann
Date: Sat Mar  7 14:45:42 2009
New Revision: 751279

URL: http://svn.apache.org/viewvc?rev=751279&view=rev
Log:
[MNG-3043] Allow 'mvn test' to work with test-jar dependencies in a reactor

Added:
    
maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/artifact/ActiveProjectOutputArtifact.java
   (with props)
Modified:
    
maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/MavenProject.java

Modified: 
maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/MavenProject.java
URL: 
http://svn.apache.org/viewvc/maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/MavenProject.java?rev=751279&r1=751278&r2=751279&view=diff
==============================================================================
--- 
maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/MavenProject.java
 (original)
+++ 
maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/MavenProject.java
 Sat Mar  7 14:45:42 2009
@@ -63,6 +63,7 @@
 import org.apache.maven.model.Scm;
 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
 import org.apache.maven.project.artifact.ActiveProjectArtifact;
+import org.apache.maven.project.artifact.ActiveProjectOutputArtifact;
 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
 import org.apache.maven.project.artifact.MavenMetadataSource;
 import org.codehaus.plexus.logging.Logger;
@@ -1773,22 +1774,19 @@
             MavenProject ref = (MavenProject) getProjectReferences().get( 
refId );
             if ( ref != null )
             {
+                // first, try the main artifact
                 if ( ref.getArtifact() != null
                     && ref.getArtifact().getDependencyConflictId().equals( 
pluginArtifact.getDependencyConflictId() ) )
                 {
                     // if the project artifact doesn't exist, don't use it. We 
haven't built that far.
                     if ( ref.getArtifact().getFile() != null && 
ref.getArtifact().getFile().exists() )
                     {
-                        // FIXME: Why aren't we using project.getArtifact() 
for the second parameter here??
                         Artifact resultArtifact = new ActiveProjectArtifact( 
ref, pluginArtifact );
                         return resultArtifact;
                     }
-                    else
-                    {
-                        logMissingSiblingProjectArtifact( pluginArtifact );
-                    }
                 }
 
+                // next, check the attached artifacts
                 Artifact attached = findMatchingArtifact( 
ref.getAttachedArtifacts(), pluginArtifact );
                 if ( attached != null )
                 {
@@ -1798,10 +1796,18 @@
                         resultArtifact.setScope( pluginArtifact.getScope() );
                         return resultArtifact;
                     }
-                    else
-                    {
-                        logMissingSiblingProjectArtifact( pluginArtifact );
-                    }
+                }
+
+                // finally, try to satisfy from project main/test output 
directory
+                Artifact output = ActiveProjectOutputArtifact.newInstance( 
ref, pluginArtifact );
+                if ( output != null && output.getFile() != null )
+                {
+                    logMissingSiblingProjectArtifact( pluginArtifact, false );
+                    return output;
+                }
+                else
+                {
+                    logMissingSiblingProjectArtifact( pluginArtifact, true );
                 }
             }
         }
@@ -1873,19 +1879,30 @@
         return buffer.toString();
     }
 
-    private void logMissingSiblingProjectArtifact( Artifact artifact )
+    private void logMissingSiblingProjectArtifact( Artifact artifact, boolean 
repository )
     {
         if ( logger == null )
         {
             return;
         }
         
-        StringBuffer message = new StringBuffer();
-        message.append( "A dependency of the current project (or of one the 
plugins used in its build) was found in the reactor, " );
-        message.append( "\nbut had not been built at the time it was 
requested. It will be resolved from the repository instead." );
+        StringBuffer message = new StringBuffer( 1024 );
+        message.append( "A dependency of the current project (or of one the 
plugins used in its build)" );
+        message.append( "\nwas found in the reactor, but had not been built at 
the time it was requested." );
+        if ( repository )
+        {
+            message.append( "\nIt will be resolved from the repository 
instead. Hence, the build result can" );
+            message.append( "\nbe inaccurate due to the repository containing 
an outdated JAR." );
+        }
+        else
+        {
+            message.append( "\nIt will be resolved from the project output 
directory instead. Hence, the" );
+            message.append( "\nbuild result can be inaccurate due to 
differences from the final JAR." );
+        }
         message.append( "\n\nCurrent Project: " ).append( getName() );
         message.append( "\nRequested Dependency: " ).append( artifact.getId() 
);
-        message.append( "\n\nNOTE: You may need to run this build to the 
'compile' lifecycle phase, or farther, in order to build the dependency 
artifact." );
+        message.append( "\n\nNOTE: You may need to run this build to the 
lifecycle phase 'compile' or even" );
+        message.append( "\n'package', in order to properly resolve the 
dependency artifact." );
         message.append( "\n" );
         
         logger.warn( message.toString() );

Added: 
maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/artifact/ActiveProjectOutputArtifact.java
URL: 
http://svn.apache.org/viewvc/maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/artifact/ActiveProjectOutputArtifact.java?rev=751279&view=auto
==============================================================================
--- 
maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/artifact/ActiveProjectOutputArtifact.java
 (added)
+++ 
maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/artifact/ActiveProjectOutputArtifact.java
 Sat Mar  7 14:45:42 2009
@@ -0,0 +1,463 @@
+package org.apache.maven.project.artifact;
+
+/*
+ * 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.artifact.Artifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.metadata.ArtifactMetadata;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
+import org.apache.maven.artifact.versioning.ArtifactVersion;
+import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
+import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.project.MavenProject;
+
+import java.io.File;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Provides a view of a dependency artifact whose file is resolved from the 
main/test output directory of an active
+ * project. Which project output directory is actually used as the artifact 
file depends on the type and/or classifier
+ * of the artifact. The artifact's file will be <code>null</code> if the 
corresponding project output directory does not
+ * exist.
+ * 
+ * @author Benjamin Bentmann
+ */
+public class ActiveProjectOutputArtifact
+    implements Artifact
+{
+
+    /**
+     * The dependency artifact to resolve from the project main/test output 
directory, never <code>null</code>.
+     */
+    private final Artifact artifact;
+
+    /**
+     * The project used to resolve the artifact file from, never 
<code>null</code>.
+     */
+    private final MavenProject project;
+
+    /**
+     * Creates a new project output artifact.
+     * 
+     * @param project The project used to resolve the artifact, must not be 
<code>null</code>.
+     * @param artifact The artifact whose file should be resolved from the 
project main/test output directory, must not
+     *            be <code>null</code>.
+     * @return The project output artifact or <code>null</code> if not 
appropriate.
+     */
+    public static Artifact newInstance( MavenProject project, Artifact 
artifact )
+    {
+        if ( artifact.getArtifactHandler().isAddedToClasspath() )
+        {
+            return new ActiveProjectOutputArtifact( project, artifact );
+        }
+        return null;
+    }
+
+    private ActiveProjectOutputArtifact( MavenProject project, Artifact 
artifact )
+    {
+        if ( artifact == null )
+        {
+            throw new IllegalArgumentException( "artifact missing" );
+        }
+        this.artifact = artifact;
+        if ( project == null )
+        {
+            throw new IllegalArgumentException( "project missing" );
+        }
+        this.project = project;
+    }
+
+    /** {...@inheritdoc} */
+    public File getFile()
+    {
+        String path;
+        if ( isTestArtifact( artifact ) )
+        {
+            path = project.getBuild().getTestOutputDirectory();
+        }
+        else
+        {
+            path = project.getBuild().getOutputDirectory();
+        }
+        File file = new File( path );
+        return file.isDirectory() ? file : null;
+    }
+
+    /**
+     * Determines whether the specified artifact refers to test classes.
+     * 
+     * @param artifact The artifact to check, must not be <code>null</code>.
+     * @return <code>true</code> if the artifact refers to test classes, 
<code>false</code> otherwise.
+     */
+    private static boolean isTestArtifact( Artifact artifact )
+    {
+        if ( "test-jar".equals( artifact.getType() ) )
+        {
+            return true;
+        }
+        else if ( "jar".equals( artifact.getType() ) && "tests".equals( 
artifact.getClassifier() ) )
+        {
+            return true;
+        }
+        return false;
+    }
+
+    /** {...@inheritdoc} */
+    public void setFile( File destination )
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    /** {...@inheritdoc} */
+    public String getGroupId()
+    {
+        return artifact.getGroupId();
+    }
+
+    /** {...@inheritdoc} */
+    public void setGroupId( String groupId )
+    {
+        artifact.setGroupId( groupId );
+    }
+
+    /** {...@inheritdoc} */
+    public String getArtifactId()
+    {
+        return artifact.getArtifactId();
+    }
+
+    /** {...@inheritdoc} */
+    public void setArtifactId( String artifactId )
+    {
+        artifact.setArtifactId( artifactId );
+    }
+
+    /** {...@inheritdoc} */
+    public String getVersion()
+    {
+        return artifact.getVersion();
+    }
+
+    /** {...@inheritdoc} */
+    public void setVersion( String version )
+    {
+        artifact.setVersion( version );
+    }
+
+    /** {...@inheritdoc} */
+    public String getScope()
+    {
+        return artifact.getScope();
+    }
+
+    /** {...@inheritdoc} */
+    public void setScope( String scope )
+    {
+        artifact.setScope( scope );
+    }
+
+    /** {...@inheritdoc} */
+    public String getType()
+    {
+        return artifact.getType();
+    }
+
+    /** {...@inheritdoc} */
+    public String getClassifier()
+    {
+        return artifact.getClassifier();
+    }
+
+    /** {...@inheritdoc} */
+    public boolean hasClassifier()
+    {
+        return artifact.hasClassifier();
+    }
+
+    /** {...@inheritdoc} */
+    public String getBaseVersion()
+    {
+        return artifact.getBaseVersion();
+    }
+
+    /** {...@inheritdoc} */
+    public void setBaseVersion( String baseVersion )
+    {
+        artifact.setBaseVersion( baseVersion );
+    }
+
+    /** {...@inheritdoc} */
+    public String getId()
+    {
+        return artifact.getId();
+    }
+
+    /** {...@inheritdoc} */
+    public String getDependencyConflictId()
+    {
+        return artifact.getDependencyConflictId();
+    }
+
+    /** {...@inheritdoc} */
+    public void addMetadata( ArtifactMetadata metadata )
+    {
+        artifact.addMetadata( metadata );
+    }
+
+    /** {...@inheritdoc} */
+    public Collection getMetadataList()
+    {
+        return artifact.getMetadataList();
+    }
+
+    /** {...@inheritdoc} */
+    public ArtifactRepository getRepository()
+    {
+        return artifact.getRepository();
+    }
+
+    /** {...@inheritdoc} */
+    public void setRepository( ArtifactRepository remoteRepository )
+    {
+        artifact.setRepository( remoteRepository );
+    }
+
+    /** {...@inheritdoc} */
+    public void updateVersion( String version, ArtifactRepository 
localRepository )
+    {
+        artifact.updateVersion( version, localRepository );
+    }
+
+    /** {...@inheritdoc} */
+    public String getDownloadUrl()
+    {
+        return artifact.getDownloadUrl();
+    }
+
+    /** {...@inheritdoc} */
+    public void setDownloadUrl( String downloadUrl )
+    {
+        artifact.setDownloadUrl( downloadUrl );
+    }
+
+    /** {...@inheritdoc} */
+    public ArtifactFilter getDependencyFilter()
+    {
+        return artifact.getDependencyFilter();
+    }
+
+    /** {...@inheritdoc} */
+    public void setDependencyFilter( ArtifactFilter artifactFilter )
+    {
+        artifact.setDependencyFilter( artifactFilter );
+    }
+
+    /** {...@inheritdoc} */
+    public ArtifactHandler getArtifactHandler()
+    {
+        return artifact.getArtifactHandler();
+    }
+
+    /** {...@inheritdoc} */
+    public List getDependencyTrail()
+    {
+        return artifact.getDependencyTrail();
+    }
+
+    /** {...@inheritdoc} */
+    public void setDependencyTrail( List dependencyTrail )
+    {
+        artifact.setDependencyTrail( dependencyTrail );
+    }
+
+    /** {...@inheritdoc} */
+    public VersionRange getVersionRange()
+    {
+        return artifact.getVersionRange();
+    }
+
+    /** {...@inheritdoc} */
+    public void setVersionRange( VersionRange newRange )
+    {
+        artifact.setVersionRange( newRange );
+    }
+
+    /** {...@inheritdoc} */
+    public void selectVersion( String version )
+    {
+        artifact.selectVersion( version );
+    }
+
+    /** {...@inheritdoc} */
+    public boolean isSnapshot()
+    {
+        return artifact.isSnapshot();
+    }
+
+    /** {...@inheritdoc} */
+    public int compareTo( Object o )
+    {
+        return artifact.compareTo( o );
+    }
+
+    /** {...@inheritdoc} */
+    public boolean isResolved()
+    {
+        return true;
+    }
+
+    /** {...@inheritdoc} */
+    public void setResolved( boolean resolved )
+    {
+        // noop
+    }
+
+    /** {...@inheritdoc} */
+    public void setResolvedVersion( String version )
+    {
+        artifact.setResolvedVersion( version );
+    }
+
+    /** {...@inheritdoc} */
+    public void setArtifactHandler( ArtifactHandler handler )
+    {
+        artifact.setArtifactHandler( handler );
+    }
+
+    /** {...@inheritdoc} */
+    public String toString()
+    {
+        return artifact + " <- " + project;
+    }
+
+    /** {...@inheritdoc} */
+    public boolean isRelease()
+    {
+        return artifact.isRelease();
+    }
+
+    /** {...@inheritdoc} */
+    public void setRelease( boolean release )
+    {
+        artifact.setRelease( release );
+    }
+
+    /** {...@inheritdoc} */
+    public List getAvailableVersions()
+    {
+        return artifact.getAvailableVersions();
+    }
+
+    /** {...@inheritdoc} */
+    public void setAvailableVersions( List versions )
+    {
+        artifact.setAvailableVersions( versions );
+    }
+
+    /** {...@inheritdoc} */
+    public boolean isOptional()
+    {
+        return artifact.isOptional();
+    }
+
+    /** {...@inheritdoc} */
+    public void setOptional( boolean optional )
+    {
+        artifact.setOptional( optional );
+    }
+
+    /** {...@inheritdoc} */
+    public ArtifactVersion getSelectedVersion()
+        throws OverConstrainedVersionException
+    {
+        return artifact.getSelectedVersion();
+    }
+
+    /** {...@inheritdoc} */
+    public boolean isSelectedVersionKnown()
+        throws OverConstrainedVersionException
+    {
+        return artifact.isSelectedVersionKnown();
+    }
+
+    /** {...@inheritdoc} */
+    public int hashCode()
+    {
+        int result = 17;
+
+        result = 37 * result + getGroupId().hashCode();
+        result = 37 * result + getArtifactId().hashCode();
+        result = 37 * result + getType().hashCode();
+        if ( getVersion() != null )
+        {
+            result = 37 * result + getVersion().hashCode();
+        }
+        result = 37 * result + ( getClassifier() != null ? 
getClassifier().hashCode() : 0 );
+
+        return result;
+    }
+
+    /** {...@inheritdoc} */
+    public boolean equals( Object o )
+    {
+        if ( o == this )
+        {
+            return true;
+        }
+
+        if ( !( o instanceof Artifact ) )
+        {
+            return false;
+        }
+
+        Artifact a = (Artifact) o;
+
+        if ( !a.getGroupId().equals( getGroupId() ) )
+        {
+            return false;
+        }
+        else if ( !a.getArtifactId().equals( getArtifactId() ) )
+        {
+            return false;
+        }
+        else if ( !a.getVersion().equals( getVersion() ) )
+        {
+            return false;
+        }
+        else if ( !a.getType().equals( getType() ) )
+        {
+            return false;
+        }
+        else if ( a.getClassifier() == null ? getClassifier() != null : 
!a.getClassifier().equals( getClassifier() ) )
+        {
+            return false;
+        }
+
+        return true;
+    }
+
+    public ArtifactMetadata getMetadata( Class metadataClass )
+    {
+        return artifact.getMetadata( metadataClass );
+    }
+
+}

Propchange: 
maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/artifact/ActiveProjectOutputArtifact.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/components/branches/MNG-3043/maven-project/src/main/java/org/apache/maven/project/artifact/ActiveProjectOutputArtifact.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision


Reply via email to