1 package org.codehaus.mojo.wagon.shared;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
172 }
173 }
174 }