View Javadoc

1   package org.apache.maven.plugin.idea;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.dom4j.Element;
24  import org.dom4j.Document;
25  import org.dom4j.DocumentException;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  /**
31   * Creates the workspace file (*.iws) for IntelliJ IDEA.
32   *
33   * @author Edwin Punzalan
34   * @goal workspace
35   * @execute phase="generate-sources"
36   */
37  public class IdeaWorkspaceMojo
38      extends AbstractIdeaMojo
39  {
40      /**
41       * Create IDEA workspace (.iws) file.
42       *
43       * @throws org.apache.maven.plugin.MojoExecutionException
44       *
45       */
46      public void execute()
47          throws MojoExecutionException
48      {
49          try
50          {
51              doDependencyResolution( executedProject, localRepo );
52          }
53          catch ( Exception e )
54          {
55              throw new MojoExecutionException( "Unable to build project dependencies.", e );
56          }
57  
58          rewriteWorkspace();
59      }
60  
61      public void rewriteWorkspace()
62          throws MojoExecutionException
63      {
64          File workspaceFile = new File( executedProject.getBasedir(), executedProject.getArtifactId() + ".iws" );
65  
66          try
67          {
68              Document document = readXmlDocument( workspaceFile, "workspace.xml" );
69  
70              Element module = document.getRootElement();
71  
72              setProjectScmType( module );
73  
74              writeXmlDocument( workspaceFile, document );
75          }
76          catch ( DocumentException e )
77          {
78              throw new MojoExecutionException( "Error parsing existing IWS file: " + workspaceFile.getAbsolutePath(),
79                                                e );
80          }
81          catch ( IOException e )
82          {
83              throw new MojoExecutionException( "Unable to create workspace file", e );
84          }
85      }
86  
87      /**
88       * Sets the SCM type of the project
89       */
90      private void setProjectScmType( Element content )
91      {
92          String scmType = getScmType();
93  
94          if ( scmType != null )
95          {
96              Element component = findComponent( content, "VcsManagerConfiguration" );
97  
98              Element element = findElement( component, "option", "ACTIVE_VCS_NAME" );
99  
100             element.addAttribute( "value", scmType );
101         }
102     }
103 
104     /**
105      * used to retrieve the SCM Type
106      *
107      * @return the Scm Type string used to connect to the SCM
108      */
109     protected String getScmType()
110     {
111         String scmType;
112 
113         if ( executedProject.getScm() == null )
114         {
115             return null;
116         }
117         scmType = getScmType( executedProject.getScm().getConnection() );
118 
119         if ( scmType != null )
120         {
121             return scmType;
122         }
123         scmType = getScmType( executedProject.getScm().getDeveloperConnection() );
124 
125         return scmType;
126     }
127 
128     protected String getScmType( String connection )
129     {
130         String scmType;
131 
132         if ( connection != null )
133         {
134             if ( connection.length() > 0 )
135             {
136                 int startIndex = connection.indexOf( ":" );
137 
138                 int endIndex = connection.indexOf( "|", startIndex + 1 );
139 
140                 if ( endIndex == -1 )
141                 {
142                     endIndex = connection.indexOf( ":", startIndex + 1 );
143                 }
144 
145                 if ( startIndex < endIndex )
146                 {
147                     scmType = connection.substring( startIndex + 1, endIndex );
148 
149                     scmType = translateScmType( scmType );
150 
151                     return scmType;
152                 }
153             }
154         }
155         return null;
156     }
157 
158     /**
159      * Translate the SCM type from the SCM connection URL to the format used by
160      * IDEA as the value for ACTIVE_VCS_NAME.
161      */
162     protected String translateScmType( String scmType )
163     {
164         if ( "cvs".equals( scmType ) )
165         {
166             return "CVS";
167         }
168         else if ( "perforce".equals( scmType ) )
169         {
170             return "Perforce";
171         }
172         else if ( "starteam".equals( scmType ) )
173         {
174             return "StarTeam";
175         }
176         else if ( "vss".equals( scmType ) )
177         {
178             return "SourceSafe";
179         }
180         else
181         {
182             return scmType;
183         }
184     }
185 }