blob: ec76b6d8408dfdb4155684085b34e3d97951a836 [file] [log] [blame]
Stuart McCullochb657c2f2008-01-25 08:10:25 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.obr.plugin;
20
21
22import java.io.BufferedWriter;
23import java.io.File;
24import java.io.FileWriter;
25import java.io.IOException;
26import java.io.Writer;
Stuart McCulloch473e9942008-01-28 05:23:50 +000027import java.net.URI;
Stuart McCullochb657c2f2008-01-25 08:10:25 +000028
29import org.apache.maven.artifact.manager.WagonManager;
30import org.apache.maven.artifact.repository.ArtifactRepository;
Stuart McCullochb657c2f2008-01-25 08:10:25 +000031import org.apache.maven.plugin.AbstractMojo;
32import org.apache.maven.plugin.MojoExecutionException;
33import org.apache.maven.plugin.MojoFailureException;
34import org.apache.maven.project.MavenProject;
35import org.apache.maven.settings.Settings;
36import org.apache.maven.wagon.ResourceDoesNotExistException;
37import org.apache.maven.wagon.TransferFailedException;
38import org.apache.maven.wagon.authorization.AuthorizationException;
39
40
41/**
42 * deploy the bundle to a remote site.
43 * this goal is used when you compile a project with a pom file
44 * @goal deployment
45 * @phase deploy
46 * @requiresDependencyResolution compile
47 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
48 */
49
50public class ObrDeploy extends AbstractMojo
51{
52
53 /**
54 * setting of maven.
55 *
56 * @parameter expression="${settings}"
57 * @require
58 */
59 private Settings m_settings;
60
61 /**
62 * name of the repository xml descriptor file.
63 *
64 * @parameter expression="${repository-name}" default-value="repository.xml"
65 */
66 private String m_repositoryName;
67
68 /**
69 * The local Maven repository.
70 *
71 * @parameter expression="${localRepository}"
72 * @required
73 */
74 private ArtifactRepository m_localRepo;
75
76 /**
77 * Project in use.
78 *
79 * @parameter expression="${project}"
80 * @require
81 */
82 private MavenProject m_project;
83
84 /**
85 * Wagon Manager.
86 * @component
87 */
88 private WagonManager m_wagonManager;
89
90 /**
91 * obr file define by the user.
92 *
93 * @parameter expression="${ignore-lock}"
94 *
95 */
96 private boolean m_ignoreLock;
97
98 /**
99 * used to store pathfile in local repo.
100 */
101 private String m_fileInLocalRepo;
102
103 /**
104 * Enable/Disable this goal
105 * @description If true evrything the goal do nothing, the goal just skip over
106 * @parameter expression="${maven.obr.installToRemoteOBR}" default-value="false"
107 */
108 private boolean installToRemoteOBR;
109
110
111 /**
112 * main method for this goal.
113 * @implements org.apache.maven.plugin.Mojo.execute
114 * @throws MojoExecutionException if the plugin failed
115 * @throws MojoFailureException if the plugin failed
116 */
117 public void execute() throws MojoExecutionException, MojoFailureException
118 {
119 getLog().info( "Obr-deploy start:" );
120 if ( !installToRemoteOBR )
121 {
122 getLog().info( "maven-obr-plugin:deploy goal is disable due to one of the following reason:" );
123 getLog().info( " - 'installToRemoteOBR' configuration set to false" );
124 getLog().info( " - JVM property maven.obr.installToRemoteOBR set to false" );
125 return;
126 }
127 ArtifactRepository ar = m_project.getDistributionManagementArtifactRepository();
128
129 // locate the obr.xml file
Stuart McCulloch473e9942008-01-28 05:23:50 +0000130 URI obrXml = ObrUtils.findObrXml( m_project.getResources() );
Stuart McCullochb657c2f2008-01-25 08:10:25 +0000131
132 // the obr.xml file is not present
Stuart McCulloch473e9942008-01-28 05:23:50 +0000133 if ( null == obrXml )
Stuart McCullochb657c2f2008-01-25 08:10:25 +0000134 {
Stuart McCulloch473e9942008-01-28 05:23:50 +0000135 getLog().info( "obr.xml is not present, use default" );
Stuart McCullochb657c2f2008-01-25 08:10:25 +0000136 }
137
138 File repoDescriptorFile = null;
139
140 // init the wagon connection
141 RemoteFileManager remoteFile = new RemoteFileManager( ar, m_wagonManager, m_settings, getLog() );
142 remoteFile.connect();
143
144 // create a non-empty file used to lock the repository descriptor file
145 File lockFile = null;
146 Writer output = null;
147 try
148 {
149 lockFile = File.createTempFile( String.valueOf( System.currentTimeMillis() ), null );
150 output = new BufferedWriter( new FileWriter( lockFile ) );
151 output.write( "locked" );
152 output.close();
153 }
154 catch ( IOException e )
155 {
156 getLog().error( "Unable to create temporary file" );
157 throw new MojoFailureException( "IOException" );
158 }
159
160 if ( m_ignoreLock )
161 {
162 try
163 {
164 remoteFile.put( lockFile, m_repositoryName + ".lock" );
165 }
166 catch ( TransferFailedException e )
167 {
168 getLog().error( "Transfer failed" );
169 e.printStackTrace();
170 throw new MojoFailureException( "TransferFailedException" );
171
172 }
173 catch ( ResourceDoesNotExistException e )
174 {
175 throw new MojoFailureException( "ResourceDoesNotExistException" );
176 }
177 catch ( AuthorizationException e )
178 {
179 getLog().error( "Authorization failed" );
180 e.printStackTrace();
181 throw new MojoFailureException( "AuthorizationException" );
182 }
183
184 }
185 else
186 {
187 int countError = 0;
188 while ( remoteFile.isLockedFile( remoteFile, m_repositoryName ) && countError < 2 )
189 {
190 countError++;
191 getLog().warn( "File is locked, retry in 10s" );
192 try
193 {
194 Thread.sleep( 10000 );
195 }
196 catch ( InterruptedException e )
197 {
198 getLog().warn( "Sleep interupted" );
199 }
200 }
201
202 if ( countError == 2 )
203 {
204 getLog().error(
205 "File: " + m_repositoryName + " is locked. Try -Dignore-lock=true if you want force uploading" );
206 throw new MojoFailureException( "fileLocked" );
207 }
208 }
209
210 // file is not locked, so we lock it now
211 try
212 {
213 remoteFile.put( lockFile, m_repositoryName + ".lock" );
214 }
215 catch ( TransferFailedException e )
216 {
217 getLog().error( "Transfer failed" );
218 e.printStackTrace();
219 throw new MojoFailureException( "TransferFailedException" );
220
221 }
222 catch ( ResourceDoesNotExistException e )
223 {
224 throw new MojoFailureException( "ResourceDoesNotExistException" );
225 }
226 catch ( AuthorizationException e )
227 {
228 getLog().error( "Authorization failed" );
229 e.printStackTrace();
230 throw new MojoFailureException( "AuthorizationException" );
231 }
232
233 try
234 {
235 repoDescriptorFile = remoteFile.get( m_repositoryName );
236 }
237 catch ( TransferFailedException e )
238 {
239 getLog().error( "Transfer failed" );
240 e.printStackTrace();
241 throw new MojoFailureException( "TransferFailedException" );
242
243 }
244 catch ( ResourceDoesNotExistException e )
245 {
246 // file doesn't exist! create a new one
247 getLog().warn( "file specified does not exist: " + m_repositoryName );
248 getLog().warn( "Create a new repository descriptor file " + m_repositoryName );
249 try
250 {
251 File f = File.createTempFile( String.valueOf( System.currentTimeMillis() ), null );
252 repoDescriptorFile = new File( f.getParent() + File.separator
253 + String.valueOf( System.currentTimeMillis() ) + ".xml" );
254 }
255 catch ( IOException e1 )
256 {
257 getLog().error( "canno't create temporary file" );
258 e1.printStackTrace();
259 return;
260 }
261 }
262 catch ( AuthorizationException e )
263 {
264 getLog().error( "Authorization failed" );
265 e.printStackTrace();
266 throw new MojoFailureException( "AuthorizationException" );
267 }
268 catch ( IOException e )
269 {
270 e.printStackTrace();
271 throw new MojoFailureException( "IOException" );
272 }
273
274 Config userConfig = new Config();
275 userConfig.setPathRelative( true );
276 userConfig.setRemotely( true );
277
278 PathFile file = null;
279
280 // get the path to local maven repository
281 file = new PathFile( PathFile.uniformSeparator( m_settings.getLocalRepository() ) + File.separator
282 + PathFile.uniformSeparator( m_localRepo.pathOf( m_project.getArtifact() ) ) );
283 if ( file.isExists() )
284 {
285 m_fileInLocalRepo = file.getOnlyAbsoluteFilename();
286 }
287 else
288 {
289 getLog().error(
290 "file not found in local repository: " + m_settings.getLocalRepository() + File.separator
291 + m_localRepo.pathOf( m_project.getArtifact() ) );
292 return;
293 }
294
295 file = new PathFile( "file:/" + repoDescriptorFile.getAbsolutePath() );
296
Stuart McCulloch473e9942008-01-28 05:23:50 +0000297 ObrUpdate obrUpdate = new ObrUpdate( file, obrXml, m_project, m_fileInLocalRepo, PathFile
Stuart McCullochb657c2f2008-01-25 08:10:25 +0000298 .uniformSeparator( m_settings.getLocalRepository() ), userConfig, getLog() );
299
300 obrUpdate.updateRepository();
301
302 // the reposiroty descriptor file is modified, we upload it on the remote repository
303 try
304 {
305 remoteFile.put( repoDescriptorFile, m_repositoryName );
306 }
307 catch ( TransferFailedException e )
308 {
309 getLog().error( "Transfer failed" );
310 e.printStackTrace();
311 throw new MojoFailureException( "TransferFailedException" );
312 }
313 catch ( ResourceDoesNotExistException e )
314 {
315 getLog().error( "Resource does not exist:" + repoDescriptorFile.getName() );
316 e.printStackTrace();
317 throw new MojoFailureException( "ResourceDoesNotExistException" );
318 }
319 catch ( AuthorizationException e )
320 {
321 getLog().error( "Authorization failed" );
322 e.printStackTrace();
323 throw new MojoFailureException( "AuthorizationException" );
324 }
325 repoDescriptorFile.delete();
326
327 // we remove lockFile activation
328 lockFile = null;
329 try
330 {
331 lockFile = File.createTempFile( String.valueOf( System.currentTimeMillis() ), null );
332 }
333 catch ( IOException e )
334 {
335 e.printStackTrace();
336 throw new MojoFailureException( "IOException" );
337 }
338 try
339 {
340 remoteFile.put( lockFile, m_repositoryName + ".lock" );
341 }
342 catch ( TransferFailedException e )
343 {
344 getLog().error( "Transfer failed" );
345 e.printStackTrace();
346 throw new MojoFailureException( "TransferFailedException" );
347 }
348 catch ( ResourceDoesNotExistException e )
349 {
350 e.printStackTrace();
351 throw new MojoFailureException( "ResourceDoesNotExistException" );
352 }
353 catch ( AuthorizationException e )
354 {
355 getLog().error( "Authorization failed" );
356 e.printStackTrace();
357 throw new MojoFailureException( "AuthorizationException" );
358 }
359
360 remoteFile.disconnect();
361 }
362
363}