View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.command.export;
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 java.io.File;
23  
24  import org.apache.maven.scm.ScmBranch;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmRevision;
28  import org.apache.maven.scm.ScmTag;
29  import org.apache.maven.scm.ScmVersion;
30  import org.apache.maven.scm.command.export.AbstractExportCommand;
31  import org.apache.maven.scm.command.export.ExportScmResult;
32  import org.apache.maven.scm.command.export.ExportScmResultWithRevision;
33  import org.apache.maven.scm.provider.ScmProviderRepository;
34  import org.apache.maven.scm.provider.svn.SvnCommandUtils;
35  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
36  import org.apache.maven.scm.provider.svn.command.SvnCommand;
37  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
38  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
39  import org.apache.maven.scm.provider.svn.svnexe.command.update.SvnUpdateConsumer;
40  import org.codehaus.plexus.util.Os;
41  import org.codehaus.plexus.util.StringUtils;
42  import org.codehaus.plexus.util.cli.CommandLineException;
43  import org.codehaus.plexus.util.cli.CommandLineUtils;
44  import org.codehaus.plexus.util.cli.Commandline;
45  
46  /**
47   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
48   *
49   */
50  public class SvnExeExportCommand
51      extends AbstractExportCommand
52      implements SvnCommand
53  {
54      /** {@inheritDoc} */
55      protected ExportScmResult executeExportCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version,
56                                                      String outputDirectory )
57          throws ScmException
58      {
59  
60          if ( outputDirectory == null )
61          {
62              outputDirectory = fileSet.getBasedir().getAbsolutePath();
63          }
64  
65          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
66  
67          String url = repository.getUrl();
68  
69          if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
70          {
71              if ( version instanceof ScmTag )
72              {
73                  url = SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) version );
74              }
75              else if ( version instanceof ScmBranch )
76              {
77                  url = SvnTagBranchUtils.resolveBranchUrl( repository, (ScmBranch) version );
78              }
79          }
80  
81          url = SvnCommandUtils.fixUrl( url, repository.getUser() );
82  
83          Commandline cl =
84              createCommandLine( (SvnScmProviderRepository) repo, fileSet.getBasedir(), version, url, outputDirectory );
85  
86          SvnUpdateConsumer consumer = new SvnUpdateConsumer( getLogger(), fileSet.getBasedir() );
87  
88          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
89  
90          if ( getLogger().isInfoEnabled() )
91          {
92              getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
93  
94              if ( cl.getWorkingDirectory() != null && Os.isFamily( Os.FAMILY_WINDOWS ) )
95              {
96                  getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
97              }
98          }
99  
100         int exitCode;
101 
102         try
103         {
104             exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
105         }
106         catch ( CommandLineException ex )
107         {
108             throw new ScmException( "Error while executing command.", ex );
109         }
110 
111         if ( exitCode != 0 )
112         {
113             return new ExportScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
114         }
115 
116         return new ExportScmResultWithRevision( cl.toString(), consumer.getUpdatedFiles(),
117                                                 String.valueOf( consumer.getRevision() ) );
118     }
119 
120     // ----------------------------------------------------------------------
121     //
122     // ----------------------------------------------------------------------
123 
124     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
125                                                  ScmVersion version, String url, String outputSirectory )
126     {
127         if ( version != null && StringUtils.isEmpty( version.getName() ) )
128         {
129             version = null;
130         }
131 
132         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
133 
134         cl.createArg().setValue( "export" );
135 
136         if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
137         {
138             if ( version instanceof ScmRevision )
139             {
140                 cl.createArg().setValue( "-r" );
141 
142                 cl.createArg().setValue( version.getName() );
143             }
144         }
145         
146         //support exporting to an existing directory
147         cl.createArg().setValue( "--force" );
148 
149         cl.createArg().setValue( url );
150 
151         if ( StringUtils.isNotEmpty( outputSirectory ) )
152         {
153             cl.createArg().setValue( outputSirectory );
154         }
155 
156         return cl;
157     }
158 }