extract the subversion address from the repo connection settings (anonymous and dev) to use it with the svn executable.

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@384947 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
aheritier 2006-03-10 23:13:28 +00:00
parent 49e6ddb737
commit 7b212ef60f
2 changed files with 311 additions and 318 deletions

View File

@ -21,386 +21,377 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.maven.scm.manager.NoSuchScmProviderException;
import org.apache.maven.scm.manager.ScmManager;
import org.apache.maven.scm.provider.clearcase.ClearCaseScmProvider; import org.apache.maven.scm.provider.clearcase.ClearCaseScmProvider;
import org.apache.maven.scm.provider.clearcase.repository.ClearCaseScmProviderRepository; import org.apache.maven.scm.provider.clearcase.repository.ClearCaseScmProviderRepository;
import org.apache.maven.scm.provider.perforce.PerforceScmProvider; import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository; import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
import org.apache.maven.scm.provider.starteam.StarteamScmProvider; import org.apache.maven.scm.provider.starteam.StarteamScmProvider;
import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository; import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
import org.apache.maven.scm.repository.ScmRepository;
import org.apache.maven.scm.repository.ScmRepositoryException; import org.apache.maven.scm.repository.ScmRepositoryException;
import org.apache.maven.util.EnhancedStringTokenizer; import org.apache.maven.util.EnhancedStringTokenizer;
/** /**
* Utility class to manage SCM informations. * Utility class to manage SCM informations. NOTE: This is very CVS specific,
* NOTE: This is very CVS specific, but I would like to try additional SCM * but I would like to try additional SCM package like subversion ASAP.
* package like subversion ASAP. *
*
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @author <a href="mailto:aheritier@apache.org">Arnaud Heritier</a> * @author <a href="mailto:aheritier@apache.org">Arnaud Heritier</a>
* @version $Id$ * @version $Id$
*/ */
public final class ScmUtil public final class ScmUtil {
{ /**
/** * Get the SCM type.
* Get the SCM type. *
* * @param scmConnection
* @param scmConnection the scm connection to analyse. * the scm connection to analyse.
* @return the scm type : cvs, svn , ... * @return the scm type : cvs, svn , ...
*/ */
public String getScmType( final String scmConnection ) public String getScmType(final String scmConnection) {
{ if (isValid(scmConnection)) {
if ( isValid( scmConnection ) ) return splitSCMConnection(scmConnection)[1];
{ }
return splitSCMConnection( scmConnection )[1];
}
return null; return null;
} }
/** /**
* Get cvs connection string. * Get cvs connection string. Used in
* Used in xdocs/src/plugin-resources/templates/scm/cvs.xml. * xdocs/src/plugin-resources/templates/scm/cvs.xml. If username == "",
* If username == "", assumes anonymous (pserver) connection. In this case, * assumes anonymous (pserver) connection. In this case, inserts a separator
* inserts a separator (':' or '|') between the username and '@' to indicate * (':' or '|') between the username and '@' to indicate that there is a
* that there is a password and that it is empty. * password and that it is empty. If username != "" it replaces username in
* If username != "" it replaces username in conn. * conn.
* *
* @param conn six token connection string * @param conn
* @param username username override if non-empty. * six token connection string
* @return CVS root. * @param username
*/ * username override if non-empty.
public String getCvsConnection( String conn, String username ) * @return CVS root.
{ */
String[] tokens = splitSCMConnection( conn ); public String getCvsConnection(String conn, String username) {
String[] tokens = splitSCMConnection(conn);
if ( !tokens[1].equals( "cvs" ) ) if (!tokens[1].equals("cvs")) {
{ return "";
return ""; }
}
String separator = getSCMConnectionSeparator( conn ); String separator = getSCMConnectionSeparator(conn);
if ( tokens[3].indexOf( '@' ) >= 0 ) if (tokens[3].indexOf('@') >= 0) {
{ if (username.length() == 0) {
if ( username.length() == 0 ) username = tokens[3].substring(0, tokens[3].indexOf('@'))
{ + separator;
username = }
tokens[3].substring( 0, tokens[3].indexOf( '@' ) )
+ separator;
}
tokens[3] = tokens[3] = username + "@"
username + "@" + tokens[3].substring(tokens[3].indexOf('@') + 1);
+ tokens[3].substring( tokens[3].indexOf( '@' ) + 1 ); }
}
String result = String result = tokens[0] + ":" + tokens[1] + separator + tokens[2]
tokens[0] + ":" + tokens[1] + separator + tokens[2] + separator + tokens[3] + separator + tokens[4] + separator
+ separator + tokens[3] + separator + tokens[4] + separator + tokens[5];
+ tokens[5];
return result; return result;
} }
/** /**
* Get cvs module. Used in * Get cvs module. Used in xdocs/src/plugin-resources/templates/scm/cvs.xml.
* xdocs/src/plugin-resources/templates/scm/cvs.xml. *
* * @param conn
* @param conn six token connection string * six token connection string
* @return CVS module. * @return CVS module.
*/ */
public String getCvsModule( String conn ) public String getCvsModule(String conn) {
{ if (isValid(conn)) {
if ( isValid( conn ) ) String[] tokens = splitSCMConnection(conn);
{
String[] tokens = splitSCMConnection( conn );
if ( !tokens[1].equals( "cvs" ) ) if (!tokens[1].equals("cvs")) {
{ return "";
return ""; }
}
return tokens[5]; return tokens[5];
} }
return null; return null;
} }
/** /**
* Create the documentation to provide an developer access with a <code>Perforce</code> SCM. * Get svn connection string. Used in
* For example, generate the following command line: * xdocs/src/plugin-resources/templates/scm/svn.xml. It removes the first
* <p>p4 -H hostname -p port -u username -P password path</p> * two elements (scm:svn:)
* <p>p4 -H hostname -p port -u username -P password path submit -c changement</p> *
* * @param conn
* @param perforceRepo * the repository connection
* @see <a href="http://www.perforce.com/perforce/doc.051/manuals/cmdref/index.html">http://www.perforce.com/perforce/doc.051/manuals/cmdref/index.html</> * @return CVS root.
*/ */
public String developerAccessPerforce( String devConnection ) public String getSvnConnection(String conn) {
{ if (conn != null && conn.length() > 8
StringBuffer command = new StringBuffer(); && "svn".equals(conn.substring(4, 7)))
char delim = getSCMConnectionSeparator( devConnection ).charAt( 0 ); return conn.substring(8);
else {
System.err
.println("Your subversion connection does not seem to be valid: "
+ conn);
return "";
}
}
PerforceScmProvider perforceProvider= new PerforceScmProvider(); /**
PerforceScmProviderRepository perforceRepo; * Create the documentation to provide an developer access with a
* <code>Perforce</code> SCM. For example, generate the following command
* line:
* <p>
* p4 -H hostname -p port -u username -P password path
* </p>
* <p>
* p4 -H hostname -p port -u username -P password path submit -c changement
* </p>
*
* @param devConnection
* @see <a
* href="http://www.perforce.com/perforce/doc.051/manuals/cmdref/index.html">http://www.perforce.com/perforce/doc.051/manuals/cmdref/index.html</>
*/
public String developerAccessPerforce(String devConnection) {
StringBuffer command = new StringBuffer();
char delim = getSCMConnectionSeparator(devConnection).charAt(0);
String scmSpecificUrl = devConnection.substring( 13 ); PerforceScmProvider perforceProvider = new PerforceScmProvider();
PerforceScmProviderRepository perforceRepo;
try String scmSpecificUrl = devConnection.substring(13);
{
perforceRepo = (PerforceScmProviderRepository)
perforceProvider.makeProviderScmRepository( scmSpecificUrl, delim );
}
catch (ScmRepositoryException e)
{
System.err.println( "Your developerConnection does not seem to be valid: " + e.getMessage() );
return "";
}
try {
perforceRepo = (PerforceScmProviderRepository) perforceProvider
.makeProviderScmRepository(scmSpecificUrl, delim);
} catch (ScmRepositoryException e) {
System.err
.println("Your developerConnection does not seem to be valid: "
+ e.getMessage());
return "";
}
command.append( "p4" ); command.append("p4");
if ( !StringUtils.isEmpty( perforceRepo.getHost() ) ) if (!StringUtils.isEmpty(perforceRepo.getHost())) {
{ command.append(" -H ").append(perforceRepo.getHost());
command.append( " -H " ).append( perforceRepo.getHost() ); }
} if (perforceRepo.getPort() > 0) {
if ( perforceRepo.getPort() > 0 ) command.append(" -p " + perforceRepo.getPort());
{ }
command.append( " -p " + perforceRepo.getPort() ); command.append(" -u username");
} command.append(" -P password");
command.append( " -u username" ); command.append(" ");
command.append( " -P password" ); command.append(perforceRepo.getPath());
command.append( " " ); command.append("\n");
command.append( perforceRepo.getPath() ); command.append("p4 submit -c \"A comment\"");
command.append( "\n" );
command.append( "p4 submit -c \"A comment\"" );
return command.toString(); return command.toString();
} }
// Starteam // Starteam
/** /**
* Create the documentation to provide an developer access with a <code>Starteam</code> SCM. * Create the documentation to provide an developer access with a
* For example, generate the following command line: * <code>Starteam</code> SCM. For example, generate the following command
* <p>stcmd co -x -nologo -stop -p myusername:mypassword@myhost:1234/projecturl -is</p> * line:
* <p>stcmd ci -x -nologo -stop -p myusername:mypassword@myhost:1234/projecturl -f NCI -is</p> * <p>
* * stcmd co -x -nologo -stop -p myusername:mypassword@myhost:1234/projecturl
* @param starteamRepo * -is
*/ * </p>
public String developerAccessStarteam( String devConnection ) * <p>
{ * stcmd ci -x -nologo -stop -p myusername:mypassword@myhost:1234/projecturl
* -f NCI -is
* </p>
*
* @param devConnection
*/
public String developerAccessStarteam(String devConnection) {
StringBuffer command = new StringBuffer(); StringBuffer command = new StringBuffer();
char delim = getSCMConnectionSeparator( devConnection ).charAt( 0 ); char delim = getSCMConnectionSeparator(devConnection).charAt(0);
StarteamScmProvider starteamProvider= new StarteamScmProvider(); StarteamScmProvider starteamProvider = new StarteamScmProvider();
StarteamScmProviderRepository starteamRepo; StarteamScmProviderRepository starteamRepo;
String scmSpecificUrl = devConnection.substring( 13 ); String scmSpecificUrl = devConnection.substring(13);
try try {
{ starteamRepo = (StarteamScmProviderRepository) starteamProvider
starteamRepo = (StarteamScmProviderRepository) .makeProviderScmRepository(scmSpecificUrl, delim);
starteamProvider.makeProviderScmRepository( scmSpecificUrl, delim ); } catch (ScmRepositoryException e) {
} System.err
catch (ScmRepositoryException e) .println("Your developerConnection does not seem to be valid: "
{ + e.getMessage());
System.err.println( "Your developerConnection does not seem to be valid: " + e.getMessage() ); return "";
return ""; }
}
// Safety: remove the username/password if present // Safety: remove the username/password if present
String fullUrl = StringUtils.replace( starteamRepo.getFullUrl(), starteamRepo.getUser(), "username" ); String fullUrl = StringUtils.replace(starteamRepo.getFullUrl(),
fullUrl = StringUtils.replace( fullUrl, starteamRepo.getPassword(), "password" ); starteamRepo.getUser(), "username");
fullUrl = StringUtils.replace(fullUrl, starteamRepo.getPassword(),
"password");
command.append( "stcmd co -x -nologo -stop -p " ); command.append("stcmd co -x -nologo -stop -p ");
command.append( fullUrl ); command.append(fullUrl);
command.append( " -is" ); command.append(" -is");
command.append( "\n" ); command.append("\n");
command.append( "stcmd ci -x -nologo -stop -p " ); command.append("stcmd ci -x -nologo -stop -p ");
command.append( fullUrl ); command.append(fullUrl);
command.append( " -f NCI -is" ); command.append(" -f NCI -is");
return command.toString(); return command.toString();
} }
/**
* Create the documentation to provide an developer access with a
* <code>Clearcase</code> SCM. For example, generate the following command
* line:
* <p>
* cleartool checkout module
* </p>
*
* @param devConnection
*/
public String developerAccessClearCase(String devConnection) {
/** StringBuffer command = new StringBuffer();
* Create the documentation to provide an developer access with a <code>Clearcase</code> SCM. char delim = getSCMConnectionSeparator(devConnection).charAt(0);
* For example, generate the following command line:
* <p>cleartool checkout module</p>
*
* @param clearCaseRepo
*/
public String developerAccessClearCase( String devConnection )
{
StringBuffer command = new StringBuffer(); ClearCaseScmProvider clearCaseProvider = new ClearCaseScmProvider();
char delim = getSCMConnectionSeparator( devConnection ).charAt( 0 ); ClearCaseScmProviderRepository clearCaseRepo;
ClearCaseScmProvider clearCaseProvider= new ClearCaseScmProvider(); String scmSpecificUrl = devConnection.substring(14);
ClearCaseScmProviderRepository clearCaseRepo;
String scmSpecificUrl = devConnection.substring( 14 ); try {
clearCaseRepo = (ClearCaseScmProviderRepository) clearCaseProvider
.makeProviderScmRepository(scmSpecificUrl, delim);
} catch (ScmRepositoryException e) {
System.err
.println("Your developerConnection does not seem to be valid: "
+ e.getMessage());
return "";
}
try command.append("cleartool checkout ").append(
{ clearCaseRepo.getViewName("id"));
clearCaseRepo = (ClearCaseScmProviderRepository)
clearCaseProvider.makeProviderScmRepository( scmSpecificUrl, delim );
}
catch (ScmRepositoryException e)
{
System.err.println( "Your developerConnection does not seem to be valid: " + e.getMessage() );
return "";
}
command.append( "cleartool checkout " ).append( clearCaseRepo.getViewName( "id" ) ); return command.toString();
}
return command.toString(); /**
} * Splits an SCM string into parts.
*
* @param connection
* @return A string array of SCM parts
*/
public String[] splitSCMConnection(String connection) {
if (connection == null) {
throw new NullPointerException("repository connection is null");
}
if (connection.length() < 5) {
throw new IllegalArgumentException(
"repository connection is too short");
}
/** if (!connection.startsWith("scm:")) {
* Splits an SCM string into parts. throw new IllegalArgumentException(
* "repository connection must start with scm:");
* @param connection }
* @return A string array of SCM parts
*/
public String[] splitSCMConnection( String connection )
{
if ( connection == null )
{
throw new NullPointerException( "repository connection is null" );
}
if ( connection.length() < 5 ) String delimiter = getSCMConnectionSeparator(connection);
{
throw new IllegalArgumentException(
"repository connection is too short" );
}
if ( !connection.startsWith( "scm:" ) ) // If the tokenizer is going to work correctly then the character
{ // following "scm" must be the same as the delimiter, which is not
throw new IllegalArgumentException( // always the case. Therefor we give it a modified connection.
"repository connection must start with scm:" ); String modifiedConnection = "scm" + delimiter + connection.substring(4);
}
String delimiter = getSCMConnectionSeparator( connection ); EnhancedStringTokenizer tok = new EnhancedStringTokenizer(
modifiedConnection, delimiter);
// If the tokenizer is going to work correctly then the character String[] tokens = tokenizerToArray(tok);
// following "scm" must be the same as the delimiter, which is not
// always the case. Therefor we give it a modified connection.
String modifiedConnection =
"scm" + delimiter + connection.substring( 4 );
EnhancedStringTokenizer tok = // for a valid repository, it should be scm:<provider> at least
new EnhancedStringTokenizer( modifiedConnection, delimiter ); if ((tokens.length >= 1) && tokens[1].equals("cvs")) {
if ((tokens.length >= 2) && tokens[2].equals("local")) {
if (tokens.length == 6) {
if ((tokens[3].length() > 0) && !tokens[3].equals("local")) {
throw new IllegalArgumentException(
"cvs local repository connection string must specify 5 tokens, or an empty 3rd token if 6");
}
} else if (tokens.length == 5) {
String[] newTokens = new String[6];
String[] tokens = tokenizerToArray( tok ); newTokens[0] = tokens[0];
newTokens[1] = tokens[1];
newTokens[2] = tokens[2];
newTokens[3] = "";
newTokens[4] = tokens[3];
newTokens[5] = tokens[4];
tokens = newTokens;
} else {
throw new IllegalArgumentException(
"cvs local repository connection string doesn't contain five tokens");
}
}
// for a valid repository, it should be scm:<provider> at least if (tokens.length != 6) {
if ( ( tokens.length >= 1 ) && tokens[1].equals( "cvs" ) ) throw new IllegalArgumentException(
{ "cvs repository connection string doesn't contain six tokens");
if ( ( tokens.length >= 2 ) && tokens[2].equals( "local" ) ) }
{ }
if ( tokens.length == 6 )
{
if ( ( tokens[3].length() > 0 )
&& !tokens[3].equals( "local" ) )
{
throw new IllegalArgumentException(
"cvs local repository connection string must specify 5 tokens, or an empty 3rd token if 6" );
}
}
else if ( tokens.length == 5 )
{
String[] newTokens = new String[6];
newTokens[0] = tokens[0]; return tokens;
newTokens[1] = tokens[1]; }
newTokens[2] = tokens[2];
newTokens[3] = "";
newTokens[4] = tokens[3];
newTokens[5] = tokens[4];
tokens = newTokens;
}
else
{
throw new IllegalArgumentException(
"cvs local repository connection string doesn't contain five tokens" );
}
}
if ( tokens.length != 6 ) /**
{ * Get the separator used in an SCM string
throw new IllegalArgumentException( *
"cvs repository connection string doesn't contain six tokens" ); * @param connection
} * @return String that can be either ":" or "|"
} */
public String getSCMConnectionSeparator(String connection) {
if (connection == null) {
throw new NullPointerException("repository connection is null");
}
return tokens; if (connection.indexOf("|") != -1) {
} return "|";
} else {
return ":";
}
}
/** /**
* Get the separator used in an SCM string * Converts a tokenizer to an array of strings FIXME: This should be in a
* @param connection * string util class.
* @return String that can be either ":" or "|" *
*/ * @param tok
public String getSCMConnectionSeparator( String connection ) * @return String[]
{ */
if ( connection == null ) public String[] tokenizerToArray(EnhancedStringTokenizer tok) {
{ List l = new ArrayList();
throw new NullPointerException( "repository connection is null" );
}
if ( connection.indexOf( "|" ) != -1 ) while (tok.hasMoreTokens()) {
{ l.add(tok.nextToken());
return "|"; }
}
else
{
return ":";
}
}
/** return (String[]) l.toArray(new String[l.size()]);
* Converts a tokenizer to an array of strings FIXME: This should be in a }
* string util class.
*
* @param tok
* @return String[]
*/
public String[] tokenizerToArray( EnhancedStringTokenizer tok )
{
List l = new ArrayList();
while ( tok.hasMoreTokens() ) /**
{ * Simple check for a value in the POM. Due to the Jelly swizzling fields
l.add( tok.nextToken() ); * that aren't set come out as empty strings. This will not be required when
} * the new lazy evaluation mechanism is put in place.
*
* @param value
* POM value to test.
* @return Is the value valid.
*/
protected boolean isValid(String value) {
if ((value != null) && !value.trim().equals("")) {
return true;
}
return (String[]) l.toArray( new String[l.size()] ); return false;
} }
/**
* Simple check for a value in the POM. Due to the Jelly swizzling fields that aren't set come
* out as empty strings. This will not be required when the new lazy evaluation mechanism is put
* in place.
*
* @param value POM value to test.
* @return Is the value valid.
*/
protected boolean isValid( String value )
{
if ( ( value != null ) && !value.trim().equals( "" ) )
{
return true;
}
return false;
}
} }

View File

@ -12,6 +12,7 @@
#if ($repository.connection && $repository.connection != '') #if ($repository.connection && $repository.connection != '')
#set ($connscm = $scmUtil.getScmType($repository.connection)) #set ($connscm = $scmUtil.getScmType($repository.connection))
#set ($svnconn = $scmUtil.getSvnConnection($repository.connection))
#set ($conn = $repository.connection) #set ($conn = $repository.connection)
@ -40,13 +41,14 @@
bundle="plugin-resources.templates.templates" /> bundle="plugin-resources.templates.templates" />
</p> </p>
<source>svn checkout $conn $project.artifactId</source> <source>svn checkout $svnconn $project.artifactId</source>
</subsection> </subsection>
#end #end
#if ($repository.developerConnection && $repository.developerConnection != '') #if ($repository.developerConnection && $repository.developerConnection != '')
#set ($connscm = $scmUtil.getScmType($repository.developerConnection)) #set ($connscm = $scmUtil.getScmType($repository.developerConnection))
#set ($svnconn = $scmUtil.getSvnConnection($repository.developerConnection))
#set ($conn = $repository.developerConnection) #set ($conn = $repository.developerConnection)
@ -77,7 +79,7 @@
bundle="plugin-resources.templates.templates" /> bundle="plugin-resources.templates.templates" />
</p> </p>
<source>svn checkout $conn $project.artifactId</source> <source>svn checkout $svnconn $project.artifactId</source>
</subsection> </subsection>