View Javadoc
1   package org.codehaus.mojo.wagon.shared;
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  import java.io.IOException;
24  import java.util.Arrays;
25  
26  import org.apache.maven.plugin.logging.Log;
27  import org.apache.maven.shared.model.fileset.FileSet;
28  import org.apache.maven.shared.model.fileset.util.FileSetManager;
29  import org.apache.maven.wagon.CommandExecutor;
30  import org.apache.maven.wagon.UnsupportedProtocolException;
31  import org.apache.maven.wagon.Wagon;
32  import org.apache.maven.wagon.WagonException;
33  import org.codehaus.plexus.archiver.manager.ArchiverManager;
34  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
35  import org.codehaus.plexus.archiver.zip.ZipArchiver;
36  import org.codehaus.plexus.component.annotations.Component;
37  import org.codehaus.plexus.component.annotations.Requirement;
38  import org.codehaus.plexus.util.StringUtils;
39  
40  @Component(role = WagonUpload.class, hint = "default")
41  public class DefaultWagonUpload
42      implements WagonUpload
43  {
44  
45      @Requirement
46      private ArchiverManager archiverManager;
47  
48      public void upload( Wagon wagon, FileSet fileset, Log logger )
49          throws WagonException
50      {
51  
52          FileSetManager fileSetManager = new FileSetManager( logger, logger.isDebugEnabled() );
53  
54          String[] files = fileSetManager.getIncludedFiles( fileset );
55          Arrays.sort(files);
56  
57          String url = wagon.getRepository().getUrl() + "/";
58  
59          if ( files.length == 0 )
60          {
61              logger.info( "Nothing to upload." );
62              return;
63          }
64  
65          for ( String file : files )
66          {
67              String relativeDestPath = StringUtils.replace( file, "\\", "/" );
68  
69              if ( !StringUtils.isBlank( fileset.getOutputDirectory() ) )
70              {
71                  relativeDestPath = fileset.getOutputDirectory() + "/" + relativeDestPath;
72              }
73  
74              File source = new File( fileset.getDirectory(), file );
75  
76              logger.info( "Uploading " + source + " to " + url + relativeDestPath + " ..." );
77  
78              wagon.put( source, relativeDestPath );
79          }
80  
81      }
82  
83      @Override
84      public void upload( Wagon wagon, FileSet fileset, boolean optimize, Log logger )
85          throws WagonException, IOException
86      {
87          if ( !optimize )
88          {
89              upload( wagon, fileset, logger );
90              return;
91          }
92  
93          if ( !( wagon instanceof CommandExecutor ) )
94          {
95              throw new UnsupportedProtocolException( "Wagon " + wagon.getRepository().getProtocol()
96                  + " does not support optimize upload" );
97          }
98  
99          logger.info( "Uploading " + fileset );
100 
101         File zipFile;
102         zipFile = File.createTempFile( "wagon", ".zip" );
103 
104         try
105         {
106             FileSetManager fileSetManager = new FileSetManager( logger, logger.isDebugEnabled() );
107             String[] files = fileSetManager.getIncludedFiles( fileset );
108 
109             if ( files.length == 0 )
110             {
111                 logger.info( "Nothing to upload." );
112                 return;
113             }
114 
115             logger.info( "Creating " + zipFile + " ..." );
116             createZip( files, zipFile, fileset.getDirectory() );
117 
118             String remoteFile = zipFile.getName();
119             String remoteDir = fileset.getOutputDirectory();
120             if ( !StringUtils.isBlank( remoteDir ) )
121             {
122                 remoteFile = remoteDir + "/" + remoteFile;
123             }
124 
125             logger.info( "Uploading " + zipFile + " to " + wagon.getRepository().getUrl() + "/" + remoteFile + " ..." );
126             wagon.put( zipFile, remoteFile );
127 
128             // We use the super quiet option here as all the noise seems to kill/stall the connection
129             String command = "unzip -o -qq -d " + remoteDir + " " + remoteFile;
130             if ( StringUtils.isBlank( remoteDir ) )
131             {
132                 command = "unzip -o -qq " + remoteFile;
133             }
134 
135             try
136             {
137                 logger.info( "Remote: " + command );
138                 ( (CommandExecutor) wagon ).executeCommand( command );
139             }
140             finally
141             {
142                 command = "rm -f " + remoteFile;
143                 logger.info( "Remote: " + command );
144 
145                 ( (CommandExecutor) wagon ).executeCommand( command );
146             }
147 
148         }
149         finally
150         {
151             zipFile.delete();
152         }
153 
154     }
155 
156     private void createZip( String[] files, File zipFile, String basedir )
157         throws IOException
158     {
159         try
160         {
161             ZipArchiver archiver = (ZipArchiver) this.archiverManager.getArchiver( zipFile );
162             archiver.setDestFile( zipFile );
163             for ( String file : files )
164             {
165                 archiver.addFile( new File( basedir, file ), file );
166             }
167             archiver.createArchive();
168         }
169         catch ( NoSuchArchiverException e )
170         {
171             // should never happen
172         }
173     }
174 }