View Javadoc

1   package org.apache.maven.plugins.scm;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.File;
22  
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmResult;
25  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
26  import org.apache.maven.scm.command.tag.TagScmResult;
27  import org.apache.maven.scm.command.update.UpdateScmResult;
28  import org.apache.maven.scm.log.DefaultLog;
29  import org.apache.maven.scm.manager.NoSuchScmProviderException;
30  import org.apache.maven.scm.manager.ScmManager;
31  import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
32  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
33  import org.apache.maven.scm.repository.ScmRepository;
34  import org.apache.maven.scm.repository.ScmRepositoryException;
35  import org.codehaus.plexus.embed.Embedder;
36  import org.codehaus.plexus.util.FileUtils;
37  
38  /**
39   * A bean for using the Maven SCM API because wrangling objects in Jelly is no fun.
40   *
41   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42   */
43  public class ScmBean
44  {
45      private String username;
46  
47      private String password;
48  
49      private String url;
50  
51      private String tag;
52  
53      private String workingDirectory;
54  
55      // note - this should not have a setter
56      private File checkoutDirectory;
57  
58      private String tagBase;
59  
60      protected Embedder getEmbedder()
61          throws Exception
62      {
63          ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
64          Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
65  
66          // TODO: allow this to be passed in as a parameter so Maven 1.1 can have one embedder only
67          Embedder embedder = new Embedder();
68          embedder.start();
69  
70          Thread.currentThread().setContextClassLoader( oldClassLoader );
71  
72          return embedder;
73      }
74  
75      public void checkout()
76          throws Exception
77      {
78          ScmManager scmManager = lookupScmManager();
79  
80          ScmRepository repository = getScmRepository( scmManager );
81  
82          if ( repository.getProvider().equals( "svn" ) )
83          {
84              if ( tag != null )
85              {
86                  checkoutDirectory = new File( checkoutDirectory, tag );
87              }
88          }
89  
90          checkoutDirectory = new File( workingDirectory );
91          if ( checkoutDirectory.exists() )
92          {
93              // TODO: sanity check that it is not . or .. or lower
94              FileUtils.deleteDirectory( checkoutDirectory );
95          }
96          checkoutDirectory.mkdirs();
97  
98          CheckOutScmResult result = scmManager.getProviderByRepository( repository )
99              .checkOut( repository, new ScmFileSet( checkoutDirectory.getAbsoluteFile() ), tag );
100 
101         checkResult( result );
102     }
103 
104     protected ScmRepository getScmRepository( ScmManager scmManager )
105         throws ScmRepositoryException, NoSuchScmProviderException
106     {
107         ScmRepository repository = scmManager.makeScmRepository( url );
108 
109         if ( repository.getProvider().equals( "svn" ) )
110         {
111             if ( url.endsWith("/") )
112             {
113                 repository = scmManager.makeScmRepository( url.substring(0, url.length() - 1 ));
114             }
115             SvnScmProviderRepository svnRepo = (SvnScmProviderRepository) repository.getProviderRepository();
116 
117             if ( username != null && username.length() > 0 )
118             {
119                 svnRepo.setUser( username );
120             }
121             if ( password != null && password.length() > 0 )
122             {
123                 svnRepo.setPassword( password );
124             }
125             if ( tagBase != null && tagBase.length() > 0 )
126             {
127                 svnRepo.setTagBase( tagBase );
128             }
129         }
130 
131         if ( repository.getProvider().equals( "starteam" ) )
132         {
133             StarteamScmProviderRepository starteamRepo = (StarteamScmProviderRepository) repository
134                 .getProviderRepository();
135 
136             if ( username != null && username.length() > 0 )
137             {
138                 starteamRepo.setUser( username );
139             }
140             if ( password != null && password.length() > 0 )
141             {
142                 starteamRepo.setPassword( password );
143             }
144         }
145 
146         return repository;
147     }
148 
149     protected ScmManager lookupScmManager()
150         throws Exception
151     {
152         Embedder embedder = getEmbedder();
153         ScmManager scmManager = (ScmManager) embedder.lookup( ScmManager.ROLE );
154         ScmRepository repository = getScmRepository( scmManager );
155         scmManager.getProviderByRepository( repository ).addListener( new DefaultLog() );
156         return scmManager;
157     }
158 
159     protected void checkResult( ScmResult result )
160         throws Exception
161     {
162         if ( !result.isSuccess() )
163         {
164             // TODO: improve error handling
165             System.err.println( "Provider message:" );
166             System.err.println( result.getProviderMessage() );
167             System.err.println( "Command output:" );
168             System.err.println( result.getCommandOutput() );
169             throw new Exception( "Error!" );
170         }
171     }
172 
173     public void update()
174         throws Exception
175     {
176         ScmManager scmManager = lookupScmManager();
177 
178         ScmRepository repository = getScmRepository( scmManager );
179 
180         checkoutDirectory = new File( workingDirectory );
181 
182         // TODO: want includes/excludes?
183         UpdateScmResult result = scmManager.getProviderByRepository( repository )
184             .update( repository, new ScmFileSet( new File( workingDirectory ) ), tag );
185 
186         checkResult( result );
187     }
188 
189     public void tag()
190         throws Exception
191     {
192         ScmManager scmManager = lookupScmManager();
193 
194         ScmRepository repository = getScmRepository( scmManager );
195 
196         // TODO: want includes/excludes?
197         TagScmResult result = scmManager.getProviderByRepository( repository )
198             .tag( repository, new ScmFileSet( new File( workingDirectory ) ), tag );
199 
200         checkResult( result );
201     }
202 
203     public void setUrl( String url )
204     {
205         this.url = url;
206     }
207 
208     public String getUrl()
209     {
210         return url;
211     }
212 
213     public void setTag( String tag )
214     {
215         this.tag = tag;
216     }
217 
218     public String getTag()
219     {
220         return tag;
221     }
222 
223     public void setWorkingDirectory( String workingDirectory )
224     {
225         this.workingDirectory = workingDirectory;
226     }
227 
228     public String getWorkingDirectory()
229     {
230         return workingDirectory;
231     }
232 
233     public File getCheckoutDirectory()
234     {
235         return checkoutDirectory;
236     }
237 
238     public String getTagBase()
239     {
240         return tagBase;
241     }
242 
243     public void setTagBase( String tagBase )
244     {
245         this.tagBase = tagBase;
246     }
247 
248     public String getUsername()
249     {
250         return username;
251     }
252 
253     public void setUsername( String username )
254     {
255         this.username = username;
256     }
257 
258     public String getPassword()
259     {
260         return password;
261     }
262 
263     public void setPassword( String password )
264     {
265         this.password = password;
266     }
267 
268 }