From c6b56c0e4edbe21b7900e49f6b80d135dcc17c3e Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Mon, 24 Oct 2005 02:14:13 +0000 Subject: [PATCH] PR: MNG-1001 Submitted by: Johnny R. Ruiz III add VCS section based on scm URL git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@327911 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/maven/plugin/idea/IdeaMojo.java | 149 +++++++++++++-- .../resources/templates/default/workspace.xml | 178 ++++++++++++++---- .../maven/plugin/idea/IdeaMojoTest.java | 67 +++++++ 3 files changed, 339 insertions(+), 55 deletions(-) create mode 100644 maven-plugins/maven-idea-plugin/src/test/java/org/apache/maven/plugin/idea/IdeaMojoTest.java diff --git a/maven-plugins/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaMojo.java b/maven-plugins/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaMojo.java index 90544234c9..6a6e502ca4 100644 --- a/maven-plugins/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaMojo.java +++ b/maven-plugins/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaMojo.java @@ -113,23 +113,45 @@ public class IdeaMojo throws MojoExecutionException { File workspaceFile = new File( project.getBasedir(), project.getArtifactId() + ".iws" ); - if ( !workspaceFile.exists() ) - { - FileWriter w = null; - try - { - w = new FileWriter( workspaceFile ); - IOUtil.copy( getClass().getResourceAsStream( "/templates/default/workspace.xml" ), w ); - } - catch ( IOException e ) - { - throw new MojoExecutionException( "Unable to create workspace file", e ); - } - finally - { - IOUtil.close( w ); - } + FileWriter writer = null; + + Reader reader = null; + + Xpp3Dom module = null; + + try + { + if ( workspaceFile.exists() ) + { + reader = new FileReader( workspaceFile ); + } + else + { + reader = new InputStreamReader( getClass().getResourceAsStream( "/templates/default/workspace.xml" ) ); + } + module = Xpp3DomBuilder.build( reader ); + + setProjectScmType( module ); + + writer = new FileWriter( workspaceFile ); + + Xpp3DomWriter.write( writer, module ); + } + catch ( XmlPullParserException e ) + { + throw new MojoExecutionException( "Error parsing existing IWS file: " + workspaceFile.getAbsolutePath(), + e ); + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Unable to create workspace file", e ); + } + finally + { + IOUtil.close(reader); + + IOUtil.close( writer ); } } @@ -609,6 +631,29 @@ Can't run this anyway as Xpp3Dom is in both classloaders... return setting; } + /** + * Returns a an Xpp3Dom element with (child) tag name and (name) attribute name. + * + * @param component Xpp3Dom element + * @param name Setting attribute to find + * @return option Xpp3Dom element + */ + private Xpp3Dom findElementName( Xpp3Dom component, String child, String name ) + { + Xpp3Dom[] elements = component.getChildren( child ); + for ( int i = 0; i < elements.length; i++ ) + { + if ( name.equals( elements[i].getAttribute( "name" ) ) ) + { + return elements[i]; + } + } + + Xpp3Dom element = createElement( component, child ); + element.setAttribute( "name", name ); + return element; + } + /** * Creates an Xpp3Dom element. * @@ -640,4 +685,76 @@ Can't run this anyway as Xpp3Dom is in both classloaders... } return element; } + + /** + * Sets the SCM type of the project + * + */ + private void setProjectScmType( Xpp3Dom content ) + { + String scmType = null; + + scmType = getScmType(); + + if ( scmType != null ) + { + Xpp3Dom component = findComponent( content, "VcsManagerConfiguration" ); + + Xpp3Dom element = findElementName( component, "option" , "ACTIVE_VCS_NAME" ); + + element.setAttribute( "value", scmType ); + } + } + + /** + * used to retrieve the SCM Type + * + * @return the Scm Type string used to connect to the SCM + */ + protected String getScmType() + { + String scmType = null; + + if ( project.getScm() == null ) + { + return null; + } + scmType = getScmType( project.getScm().getConnection() ); + + if ( scmType != null ) + { + return scmType; + } + scmType = getScmType( project.getScm().getDeveloperConnection() ); + + return scmType; + } + + protected String getScmType( String connection ) + { + String scmType = null; + + if ( connection != null ) + { + if ( connection.length() > 0 ) + { + int startIndex = connection.indexOf( ":" ); + + int endIndex = connection.indexOf( ":", startIndex + 1); + + if ( startIndex < endIndex ) + { + scmType = connection.substring( startIndex + 1, endIndex); + + return scmType; + } + } + } + return null; + } + + public void setProject( MavenProject project ) + { + this.project = project; + } } diff --git a/maven-plugins/maven-idea-plugin/src/main/resources/templates/default/workspace.xml b/maven-plugins/maven-idea-plugin/src/main/resources/templates/default/workspace.xml index 6d26e88b12..f1465fda55 100755 --- a/maven-plugins/maven-idea-plugin/src/main/resources/templates/default/workspace.xml +++ b/maven-plugins/maven-idea-plugin/src/main/resources/templates/default/workspace.xml @@ -199,50 +199,49 @@