[MNG-2006] Changed the model inheritance assembler to allow the project builder to pass in a child path adjustment String, which is the module path of the child in the parent POM. This can be used to realign the URLs of the child POM in addition to simply appending the child path to the parent.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@379247 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2006-02-20 21:52:03 +00:00
parent 1724b0e94d
commit 602899a9f4
5 changed files with 160 additions and 30 deletions

View File

@ -686,6 +686,8 @@ private MavenProject buildInternal( String pomLocation,
project.setOriginalModel( originalModel );
// we don't have to force the collision exception for superModel here, it's already been done in getSuperModel()
MavenProject previousProject = superProject;
Model previous = superProject.getModel();
for ( Iterator i = lineage.iterator(); i.hasNext(); )
@ -693,10 +695,22 @@ private MavenProject buildInternal( String pomLocation,
MavenProject currentProject = (MavenProject) i.next();
Model current = currentProject.getModel();
String pathAdjustment = null;
try
{
pathAdjustment = previousProject.getModulePathAdjustment( currentProject );
}
catch ( IOException e )
{
getLogger().debug( "Cannot determine whether " + currentProject.getId() + " is a module of " + previousProject.getId() + ". Reason: " + e.getMessage(), e );
}
modelInheritanceAssembler.assembleModelInheritance( current, previous );
modelInheritanceAssembler.assembleModelInheritance( current, previous, pathAdjustment );
previous = current;
previousProject = currentProject;
}
// only add the super repository if it wasn't overridden by a profile or project

View File

@ -135,6 +135,8 @@ public class MavenProject
private Build buildOverlay;
private boolean executionRoot;
private Map moduleFiles;
public MavenProject( Model model )
{
@ -188,6 +190,44 @@ public MavenProject( MavenProject project )
this.artifact = ArtifactUtils.copyArtifact( project.artifact );
}
public String getModulePathAdjustment( MavenProject moduleProject ) throws IOException
{
File module = moduleProject.getFile();
if ( module == null )
{
return null;
}
module = module.getCanonicalFile();
if ( moduleFiles == null )
{
moduleFiles = new HashMap();
List modules = getModules();
File myFile = getFile();
if ( myFile != null )
{
File myDir = myFile.getCanonicalFile().getParentFile();
if ( modules != null )
{
for ( Iterator it = modules.iterator(); it.hasNext(); )
{
String modulePath = (String) it.next();
File moduleFile = new File( myDir, modulePath ).getCanonicalFile();
moduleFiles.put( moduleFile, modulePath );
}
}
}
}
return (String) moduleFiles.get( module );
}
// ----------------------------------------------------------------------
// Accessors

View File

@ -16,6 +16,15 @@
* limitations under the License.
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.TreeMap;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
@ -29,14 +38,6 @@
import org.apache.maven.project.ModelUtils;
import org.codehaus.plexus.util.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
* @version $Id: DefaultModelInheritanceAssembler.java,v 1.4 2004/08/23 20:24:54
@ -48,15 +49,20 @@ public class DefaultModelInheritanceAssembler
{
public void copyModel( Model dest, Model source )
{
assembleModelInheritance( dest, source, false );
assembleModelInheritance( dest, source, null, false );
}
public void assembleModelInheritance( Model child, Model parent, String childPathAdjustment )
{
assembleModelInheritance( child, parent, childPathAdjustment, true );
}
public void assembleModelInheritance( Model child, Model parent )
{
assembleModelInheritance( child, parent, true );
assembleModelInheritance( child, parent, null, true );
}
private void assembleModelInheritance( Model child, Model parent, boolean appendPaths )
private void assembleModelInheritance( Model child, Model parent, String childPathAdjustment, boolean appendPaths )
{
// cannot inherit from null parent.
if ( parent == null )
@ -93,7 +99,7 @@ private void assembleModelInheritance( Model child, Model parent, boolean append
{
if ( parent.getUrl() != null )
{
child.setUrl( appendPath( parent.getUrl(), child.getArtifactId(), appendPaths ) );
child.setUrl( appendPath( parent.getUrl(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
}
else
{
@ -105,7 +111,7 @@ private void assembleModelInheritance( Model child, Model parent, boolean append
// Distribution
// ----------------------------------------------------------------------
assembleDistributionInheritence( child, parent, appendPaths );
assembleDistributionInheritence( child, parent, childPathAdjustment, appendPaths );
// issueManagement
if ( child.getIssueManagement() == null )
@ -126,7 +132,7 @@ private void assembleModelInheritance( Model child, Model parent, boolean append
}
// Scm
assembleScmInheritance( child, parent, appendPaths );
assembleScmInheritance( child, parent, childPathAdjustment, appendPaths );
// ciManagement
if ( child.getCiManagement() == null )
@ -399,7 +405,7 @@ private void assembleBuildInheritance( Model child, Model parent )
}
}
private void assembleScmInheritance( Model child, Model parent, boolean appendPaths )
private void assembleScmInheritance( Model child, Model parent, String childPathAdjustment, boolean appendPaths )
{
if ( parent.getScm() != null )
{
@ -416,7 +422,7 @@ private void assembleScmInheritance( Model child, Model parent, boolean appendPa
if ( StringUtils.isEmpty( childScm.getConnection() ) && !StringUtils.isEmpty( parentScm.getConnection() ) )
{
childScm.setConnection( appendPath( parentScm.getConnection(), child.getArtifactId(), appendPaths ) );
childScm.setConnection( appendPath( parentScm.getConnection(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
}
if ( StringUtils.isEmpty( childScm.getDeveloperConnection() ) &&
@ -424,17 +430,17 @@ private void assembleScmInheritance( Model child, Model parent, boolean appendPa
{
childScm
.setDeveloperConnection(
appendPath( parentScm.getDeveloperConnection(), child.getArtifactId(), appendPaths ) );
appendPath( parentScm.getDeveloperConnection(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
}
if ( StringUtils.isEmpty( childScm.getUrl() ) && !StringUtils.isEmpty( parentScm.getUrl() ) )
{
childScm.setUrl( appendPath( parentScm.getUrl(), child.getArtifactId(), appendPaths ) );
childScm.setUrl( appendPath( parentScm.getUrl(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
}
}
}
private void assembleDistributionInheritence( Model child, Model parent, boolean appendPaths )
private void assembleDistributionInheritence( Model child, Model parent, String childPathAdjustment, boolean appendPaths )
{
if ( parent.getDistributionManagement() != null )
{
@ -465,7 +471,7 @@ private void assembleDistributionInheritence( Model child, Model parent, boolean
if ( site.getUrl() != null )
{
site.setUrl( appendPath( site.getUrl(), child.getArtifactId(), appendPaths ) );
site.setUrl( appendPath( site.getUrl(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
}
}
}
@ -508,23 +514,78 @@ private void assembleDistributionInheritence( Model child, Model parent, boolean
}
}
private String appendPath( String url, String path, boolean appendPaths )
protected String appendPath( String parentPath, String childPath, String pathAdjustment, boolean appendPaths )
{
List pathFragments = new ArrayList();
String rootPath = parentPath;
String protocol = null;
int protocolIdx = rootPath.indexOf( "://" );
if ( protocolIdx > -1 )
{
protocol = rootPath.substring( 0, protocolIdx + 3 );
rootPath = rootPath.substring( protocolIdx + 3 );
}
pathFragments.add( rootPath );
if ( appendPaths )
{
if ( url.endsWith( "/" ) )
if ( pathAdjustment != null )
{
return url + path;
}
else
{
return url + "/" + path;
pathFragments.add( pathAdjustment );
}
pathFragments.add( childPath );
}
else
StringBuffer cleanedPath = new StringBuffer();
if ( protocol != null )
{
return url;
cleanedPath.append( protocol );
}
if ( rootPath.startsWith( "/" ) )
{
cleanedPath.append( '/' );
}
String lastToken = null;
String currentToken = null;
for ( Iterator it = pathFragments.iterator(); it.hasNext(); )
{
String pathFragment = (String) it.next();
StringTokenizer tokens = new StringTokenizer( pathFragment, "/" );
while( tokens.hasMoreTokens() )
{
lastToken = currentToken;
currentToken = tokens.nextToken();
if ( "..".equals( currentToken ) )
{
// trim the previous path part off...
cleanedPath.setLength( cleanedPath.length() - ( lastToken.length() + 1 ) );
}
else if ( !".".equals( currentToken ) )
{
// don't worry about /./ self-references.
cleanedPath.append( currentToken ).append( '/' );
}
}
}
if ( !childPath.endsWith( "/" ) && appendPaths )
{
cleanedPath.setLength( cleanedPath.length() - 1 );
}
return cleanedPath.toString();
}
}

View File

@ -26,6 +26,8 @@ public interface ModelInheritanceAssembler
{
String ROLE = ModelInheritanceAssembler.class.getName();
void assembleModelInheritance( Model child, Model parent, String childPathAdjustment );
void assembleModelInheritance( Model child, Model parent );
void copyModel( Model dest, Model source );

View File

@ -43,6 +43,19 @@ public class DefaultModelInheritanceAssemblerTest
extends TestCase
{
private ModelInheritanceAssembler assembler = new DefaultModelInheritanceAssembler();
public void testShouldAppendPathWithChildPathAdjustment()
{
String parentPath = "http://maven.apache.org/shared/maven-shared-parent";
String childPath = "file-management";
String pathAdjustment = "..";
String result = ((DefaultModelInheritanceAssembler) assembler).appendPath( parentPath, childPath, pathAdjustment, true );
System.out.println( "Resulting path is: \'" + result + "\'" );
assertEquals( "Append with path adjustment failed.", "http://maven.apache.org/shared/file-management", result );
}
public void testShouldOverrideUnitTestExcludesOnly()
{