[MNG-2006] - Module SCM URL resolution -relative path. Brought scm path behavior inline with Maven 2

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@759354 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Britton Isbell 2009-03-27 21:13:45 +00:00
parent 60c1b6f7b6
commit bdf651f694
10 changed files with 297 additions and 37 deletions

View File

@ -19,8 +19,17 @@ package org.apache.maven.project.processor;
* under the License.
*/
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.maven.model.Model;
public abstract class BaseProcessor implements Processor
{
@ -30,6 +39,9 @@ public abstract class BaseProcessor implements Processor
Object child;
Collection<Processor> processors;
private List<Model> parentModels;
public BaseProcessor( Collection<Processor> processors )
{
@ -39,11 +51,20 @@ public abstract class BaseProcessor implements Processor
}
this.processors = processors;
parentModels = new ArrayList<Model>();
}
/**
* Ordered from least specialized to most specialized.
*/
public List<Model> getParentModels()
{
return parentModels;
}
public BaseProcessor()
{
this.processors = new ArrayList<Processor>();
this(new ArrayList<Processor>());
}
public void process( Object parent, Object child, Object target, boolean isChildMostSpecialized )
@ -55,7 +76,10 @@ public abstract class BaseProcessor implements Processor
this.parent = parent;
this.child = child;
if(parent instanceof Model)
{
parentModels.add( (Model) parent );
}
for ( Processor processor : processors )
{
processor.process( parent, child, target, isChildMostSpecialized );
@ -72,4 +96,81 @@ public abstract class BaseProcessor implements Processor
{
return parent;
}
protected String normalizeUri(String u, String artifactId, Model parent)
{
if(u == null)
{
return null;
}
try
{
String slashes = getSlashes(new URI(u).getRawSchemeSpecificPart());
URI uri = new URI(u + "/"
+ getModulePathAdjustment(parent, artifactId));
String normalized = uri.normalize().toString();
if("file".equals(uri.getScheme()))//UNC Paths
{
normalized = normalized.replaceFirst("/", slashes);
}
return normalized;
}
catch (URISyntaxException e) {
}
return null;
}
private static String getSlashes(String uri)
{
StringBuilder sb = new StringBuilder();
for(byte b : uri.getBytes())
{
if(b == 47)
{
sb.append("/");
}
else
{
break;
}
}
return sb.toString();
}
private String getModulePathAdjustment(Model moduleProject,
String artifactId) {
Map<String, String> moduleAdjustments = new HashMap<String, String>();
List<String> modules = moduleProject.getModules();
if (modules != null) {
for (Iterator<String> it = modules.iterator(); it.hasNext();) {
String modulePath = (String) it.next();
String moduleName = modulePath;
if (moduleName.endsWith("/") || moduleName.endsWith("\\")) {
moduleName = moduleName.substring(0,
moduleName.length() - 1);
}
int lastSlash = moduleName.lastIndexOf('/');
if (lastSlash < 0) {
lastSlash = moduleName.lastIndexOf('\\');
}
String adjustment = null;
if (lastSlash > -1) {
moduleName = moduleName.substring(lastSlash + 1);
adjustment = modulePath.substring(0, lastSlash);
}
moduleAdjustments.put(moduleName, adjustment);
}
}
String adjust = moduleAdjustments.get(artifactId);
return (adjust != null) ? adjust + "/" + artifactId : "/" + artifactId;
}
}

View File

@ -19,6 +19,9 @@ package org.apache.maven.project.processor;
* under the License.
*/
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Model;
@ -46,25 +49,25 @@ public class DistributionManagementProcessor
if ( c.getDistributionManagement() != null )
{
copy( c.getDistributionManagement(), t.getDistributionManagement(), isChildMostSpecialized,
c.getArtifactId() );
c.getArtifactId(), p );
if ( p != null && p.getDistributionManagement() != null )
{
copy( p.getDistributionManagement(), t.getDistributionManagement(), false, c.getArtifactId() );
copy( p.getDistributionManagement(), t.getDistributionManagement(), false, c.getArtifactId(), p );
}
}
else if ( p != null && p.getDistributionManagement() != null )
{
copy( p.getDistributionManagement(), t.getDistributionManagement(), false, c.getArtifactId() );
copy( p.getDistributionManagement(), t.getDistributionManagement(), false, c.getArtifactId(), p );
}
else if(t.getDistributionManagement() != null && t.getDistributionManagement().getSite() != null)
{
copySite( t.getDistributionManagement().getSite(), t.getDistributionManagement().getSite(), false, c.getArtifactId() );
copySite( t.getDistributionManagement().getSite(), t.getDistributionManagement().getSite(), false, c.getArtifactId(), p );
// copy( t.getDistributionManagement(), t.getDistributionManagement(), isChildMostSpecialized, c.getArtifactId() );
}
}
private static void copy( DistributionManagement source, DistributionManagement target, boolean isChild,
String artifactId )
private void copy( DistributionManagement source, DistributionManagement target, boolean isChild,
String artifactId, Model parent )
{
if ( target.getDownloadUrl() == null )
{
@ -102,7 +105,7 @@ public class DistributionManagementProcessor
if ( target.getSite() == null && source.getSite() != null )
{
target.setSite( new Site() );
copySite( source.getSite(), target.getSite(), isChild, artifactId );
copySite( source.getSite(), target.getSite(), isChild, artifactId, parent );
}
}
@ -131,7 +134,7 @@ public class DistributionManagementProcessor
target.setUniqueVersion( source.isUniqueVersion() );
}
private static void copySite( Site source, Site target, boolean isChild, String artifactId )
private void copySite( Site source, Site target, boolean isChild, String artifactId, Model parent )
{
if ( target.getId() == null )
{
@ -148,11 +151,10 @@ public class DistributionManagementProcessor
if ( isChild )
{
target.setUrl( source.getUrl() );
}
}
else
{
target.setUrl( source.getUrl() + (source.getUrl().endsWith("/") ? "" : "/") + artifactId );
{
target.setUrl(normalizeUri(source.getUrl(), artifactId, parent));
}
}
else

View File

@ -19,15 +19,22 @@ package org.apache.maven.project.processor;
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.shared.model.ModelProperty;
/*
* hold original pom
@ -123,8 +130,8 @@ public class ModelProcessor
t.setUrl(c.getUrl());
}
else if(p != null && p.getUrl() != null)
{
t.setUrl( p.getUrl() + t.getArtifactId() );
{
t.setUrl( normalizeUri(p.getUrl(), t.getArtifactId(), p) );
}
else if (t.getUrl() != null)
{
@ -154,4 +161,15 @@ public class ModelProcessor
t.getDependencyManagement().getDependencies().addAll( mngDeps );
}
}
private static List<String> getParentNames(List<Model> models)
{
List<String> names = new ArrayList<String>();
for(Model m : models)
{
names.add(m.getArtifactId());
}
Collections.reverse(names);
return names;
}
}

View File

@ -1,5 +1,9 @@
package org.apache.maven.project.processor;
import java.util.List;
import org.apache.maven.model.Model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@ -26,4 +30,7 @@ public interface Processor
Object getParent();
Object getChild();
List<Model> getParentModels();
}

View File

@ -109,7 +109,7 @@ public class ProcessorContext
DependencyManagement depMng = model.getDependencyManagement();
model.setDependencyManagement( depMng );
Model target = processModelsForInheritance(profileModels, processors, false);
Model target = processModelsForInheritance(profileModels, processors);
//TODO: Merge
target.getBuild().setPluginManagement( mng );
target.setDependencyManagement( depMng );
@ -202,7 +202,7 @@ public class ProcessorContext
new RepositoriesProcessor(), new DistributionManagementProcessor(),
new LicensesProcessor(), new ScmProcessor(), new PrerequisitesProcessor(),
new ContributorsProcessor(), new DevelopersProcessor(), new ProfilesProcessor() );
Model target = processModelsForInheritance( convertDomainModelsToMavenModels( domainModels ), processors, true );
Model target = processModelsForInheritance( convertDomainModelsToMavenModels( domainModels ), processors );
PomClassicDomainModel model = convertToDomainModel( target, false );
List<ModelProperty> props = new ArrayList<ModelProperty>( model.getModelProperties());
@ -226,7 +226,7 @@ public class ProcessorContext
return new PomClassicDomainModel( modelProperties );
}
private static Model processModelsForInheritance(List<Model> models, List<Processor> processors, boolean reverse)
private static Model processModelsForInheritance(List<Model> models, List<Processor> processors)
{
ModelProcessor modelProcessor = new ModelProcessor( processors );
Collections.reverse( models );

View File

@ -19,6 +19,9 @@ package org.apache.maven.project.processor;
* under the License.
*/
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.maven.model.Model;
import org.apache.maven.model.Scm;
@ -35,13 +38,13 @@ public class ScmProcessor extends BaseProcessor
t.setScm( new Scm() );
}
copyUrl( ((p != null) ? p.getScm() : null), c.getScm(), t.getScm(), c.getArtifactId());
copyConnection( ((p != null) ? p.getScm() : null), c.getScm(), t.getScm(), c.getArtifactId());
copyDeveloperConnection( ((p != null) ? p.getScm() : null), c.getScm(), t.getScm(), c.getArtifactId());
copyUrl( ((p != null) ? p.getScm() : null), c.getScm(), t.getScm(), c.getArtifactId(), p);
copyConnection( ((p != null) ? p.getScm() : null), c.getScm(), t.getScm(), c.getArtifactId(), p);
copyDeveloperConnection( ((p != null) ? p.getScm() : null), c.getScm(), t.getScm(), c.getArtifactId(), p);
copyTag( ( ( p != null ) ? p.getScm() : null ), c.getScm(), t.getScm() );
}
private static void copyUrl(Scm p, Scm c, Scm t, String artifactId )
private void copyUrl(Scm p, Scm c, Scm t, String artifactId, Model parent )
{
if(c != null && c.getUrl() != null)
{
@ -49,37 +52,37 @@ public class ScmProcessor extends BaseProcessor
}
else if(p != null && p.getUrl() != null)
{
t.setUrl( p.getUrl() + "/" + artifactId );
t.setUrl( normalizeUri(p.getUrl(), artifactId, parent));
}
else if(t.getUrl() != null) {
t.setUrl( t.getUrl() + "/" + artifactId );
}
}
private static void copyConnection(Scm p, Scm c, Scm t, String artifactId )
private void copyConnection(Scm p, Scm c, Scm t, String artifactId, Model parent )
{
if(c!= null && c.getConnection() != null)
{
t.setConnection(c.getConnection());
}
else if(p != null && p.getConnection() != null)
{
t.setConnection( p.getConnection() + "/" + artifactId );
{
t.setConnection( normalizeUri(p.getConnection(), artifactId, parent));
}
else if(t.getConnection() != null) {
t.setConnection( t.getConnection() + "/" + artifactId );
}
}
private static void copyDeveloperConnection(Scm p, Scm c, Scm t, String artifactId )
private void copyDeveloperConnection(Scm p, Scm c, Scm t, String artifactId, Model parent )
{
if(c!= null && c.getDeveloperConnection() != null)
{
t.setDeveloperConnection(c.getDeveloperConnection());
}
else if(p != null && p.getDeveloperConnection() != null)
{
t.setDeveloperConnection( p.getDeveloperConnection() + "/" + artifactId );
{
t.setDeveloperConnection( normalizeUri(p.getDeveloperConnection(), artifactId, parent) );
}
else if(t.getDeveloperConnection() != null){
t.setDeveloperConnection( t.getDeveloperConnection() + "/" + artifactId );

View File

@ -916,6 +916,19 @@ public class PomConstructionTest
PomTestWrapper pom = this.buildPom( "unc-path/sub" );
assertEquals("file:////host/site/test-child", pom.getValue( "distributionManagement/site/url" ));
}
/** MNG-2006 */
public void testUrlAppend()
throws Exception
{
PomTestWrapper pom = this.buildPom( "url-append/child" );
System.out.println(pom.getDomainModel().asString());
assertEquals("http://project.url/child", pom.getValue( "url" ));
assertEquals("http://viewvc.project.url/child", pom.getValue( "scm/url" ));
assertEquals("http://scm.project.url/child", pom.getValue( "scm/connection" ));
assertEquals("https://scm.project.url/child", pom.getValue( "scm/developerConnection" ));
assertEquals("http://site.project.url/child", pom.getValue( "distributionManagement/site/url" ));
}
public void testPluginConfigurationUsingAttributesWithoutPluginManagement()
throws Exception

View File

@ -69,12 +69,12 @@ public class ProjectInheritanceTest
System.out.println( "Child SCM developer connection is: "
+ project1.getScm().getDeveloperConnection() );
assertEquals( project1.getScm().getUrl(), project0.getScm().getUrl() + "/p1" );
assertEquals( project1.getScm().getUrl(), project0.getScm().getUrl() + "/modules/p1" );
assertEquals( project1.getScm().getConnection(), project0.getScm().getConnection()
+ "/p1" );
+ "/modules/p1" );
assertEquals( project1.getScm().getDeveloperConnection(), project0.getScm()
.getDeveloperConnection()
+ "/p1" );
+ "/modules/p1" );
}
public void testScmInfoCalculatedCorrectlyOnChildOnlyRead()
@ -93,10 +93,9 @@ public class ProjectInheritanceTest
System.out.println( "Child SCM developer connection is: "
+ project1.getScm().getDeveloperConnection() );
assertEquals( project1.getScm().getUrl(), "http://host/viewer?path=/p0/p1" );
assertEquals( project1.getScm().getConnection(), "scm:svn:http://host/p0/p1" );
assertEquals( project1.getScm().getDeveloperConnection(),
"scm:svn:https://host/p0/p1" );
assertEquals( "http://host/viewer?path=/p0/modules/p1", project1.getScm().getUrl() );
assertEquals( "scm:svn:http://host/p0/modules/p1", project1.getScm().getConnection() );
assertEquals( "scm:svn:https://host/p0/modules/p1", project1.getScm().getDeveloperConnection() );
}
// public void testScmInfoCalculatedCorrectlyOnChildReadFromLocalRepository()

View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven.its.mng2006</groupId>
<artifactId>parent</artifactId>
<version>0.1</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<artifactId>child</artifactId>
<name>Child Project</name>
<description>
Test that inheritance of those URLs which automatically append the child's artifact id take the child's
relative location to the parent into account.
</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.its.plugins</groupId>
<artifactId>maven-it-plugin-expression</artifactId>
<version>2.1-SNAPSHOT</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>eval</goal>
</goals>
<configuration>
<outputFile>target/pom.properties</outputFile>
<expressions>
<expression>project/url</expression>
<expression>project/scm</expression>
<expression>project/distributionManagement/site</expression>
</expressions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.mng2006</groupId>
<artifactId>parent</artifactId>
<version>0.1</version>
<packaging>pom</packaging>
<name>Maven Integration Test :: MNG-2006</name>
<description>
Test that inheritance of those URLs which automatically append the child's artifact id take the child's
relative location to the parent into account.
</description>
<modules>
<module>../child</module>
</modules>
<url>http://project.url/parent</url>
<scm>
<url>http://viewvc.project.url/parent</url>
<connection>http://scm.project.url/parent</connection>
<developerConnection>https://scm.project.url/parent</developerConnection>
</scm>
<distributionManagement>
<site>
<url>http://site.project.url/parent</url>
<id>parent.site</id>
</site>
</distributionManagement>
</project>