View Javadoc

1   /*
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  */
19  package org.apache.myfaces.buildtools.maven2.plugin.wagon;
20  
21  import java.io.File;
22  
23  import org.apache.maven.artifact.manager.WagonManager;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.wagon.ConnectionException;
28  import org.apache.maven.wagon.ResourceDoesNotExistException;
29  import org.apache.maven.wagon.TransferFailedException;
30  import org.apache.maven.wagon.UnsupportedProtocolException;
31  import org.apache.maven.wagon.Wagon;
32  import org.apache.maven.wagon.authentication.AuthenticationException;
33  import org.apache.maven.wagon.authorization.AuthorizationException;
34  import org.apache.maven.wagon.observers.Debug;
35  import org.apache.maven.wagon.repository.Repository;
36  
37  
38  /**
39   * Deploys the content of a directory using scp/file protocol.
40   * For scp protocol, files are packaged into zip archive,
41   * then archive is transfred to remote host, nextly it is un-archived.
42   * This method of deployment should normally be much faster
43   * then making file by file copy.  For file protocol, the files are copied
44   * directly to the destination directory.
45   *
46   * @version 
47   * @goal deploy
48   */
49  public class WagonMojo extends AbstractMojo
50  {
51      /**
52       * Directory containing the files for wagon.
53       *
54       * @parameter
55       * @required
56       */
57      private File inputDirectory;
58  
59      /**
60        * Specifies the server id.
61        *
62        * @parameter
63        * * @required
64        */
65      private String id;
66  
67      /**
68       * The full URL of the server.
69       *
70       * @parameter
71       * @required
72       */
73      private String url;
74      /**
75       * @parameter expression="${project}"
76       * @required
77       * @readonly
78       */
79      private MavenProject project;
80  
81      /**
82       * @component
83       */
84      private WagonManager wagonManager;
85  
86      public void execute()
87          throws MojoExecutionException
88      {
89          if ( !inputDirectory.exists() )
90          {
91              throw new MojoExecutionException( "The inputDirectory does not exist" );
92          }
93  
94          Repository repository = new Repository( id, url );
95  
96  
97          Wagon wagon = null;
98          try
99          {
100             wagon = wagonManager.getWagon( repository.getProtocol() );
101         }
102         catch ( UnsupportedProtocolException e )
103         {
104             throw new MojoExecutionException( "Unsupported protocol: '" + repository.getProtocol() + "'", e );
105         }
106 
107         if ( !wagon.supportsDirectoryCopy() )
108         {
109             throw new MojoExecutionException(
110                 "Wagon protocol '" + repository.getProtocol() + "' doesn't support directory copying" );
111         }
112 
113         try
114         {
115             Debug debug = new Debug();
116 
117             wagon.addSessionListener( debug );
118 
119             wagon.addTransferListener( debug );
120 
121             wagon.connect( repository, wagonManager.getAuthenticationInfo( id ) );
122 
123             wagon.putDirectory( inputDirectory, "." );
124 
125         }
126         catch ( ResourceDoesNotExistException e )
127         {
128             throw new MojoExecutionException( "Error uploading", e );
129         }
130         catch ( TransferFailedException e )
131         {
132             throw new MojoExecutionException( "Error uploading", e );
133         }
134         catch ( AuthorizationException e )
135         {
136             throw new MojoExecutionException( "Error uploading", e );
137         }
138         catch ( ConnectionException e )
139         {
140             throw new MojoExecutionException( "Error uploading", e );
141         }
142         catch ( AuthenticationException e )
143         {
144             throw new MojoExecutionException( "Error uploading", e );
145         }
146         finally
147         {
148             try
149             {
150                 wagon.disconnect();
151             }
152             catch ( ConnectionException e )
153             {
154                 getLog().error( "Error disconnecting wagon - ignored", e );
155             }
156         }
157     }
158 }