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