View Javadoc
1   package org.codehaus.mojo.wagon;
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 org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.Parameter;
24  import org.apache.maven.wagon.ConnectionException;
25  import org.apache.maven.wagon.Wagon;
26  import org.apache.maven.wagon.WagonException;
27  
28  import java.io.IOException;
29  
30  /**
31   * Provides base functionality for dealing with I/O using single wagon.
32   */
33  public abstract class AbstractSingleWagonMojo
34      extends AbstractWagonMojo
35  {
36  
37      /**
38       * URL to upload to or download from or list. Must exist and point to a directory.
39       */
40      @Parameter( property = "wagon.url", required = true )
41      protected String url;
42  
43      /**
44       * settings.xml's server id for the URL. This is used when wagon needs extra authentication information.
45       */
46      @Parameter( property = "wagon.serverId", defaultValue = "serverId")
47      private String serverId;
48  
49      @Override
50      public void execute()
51          throws MojoExecutionException
52      {
53          if ( this.skip )
54          {
55              this.getLog().info( "Skip execution." );
56              return;
57          }
58  
59          Wagon wagon = null;
60          try
61          {
62              wagon = createWagon( serverId, url );
63              execute( wagon );
64          }
65          catch ( WagonException | IOException e )
66          {
67              throw new MojoExecutionException( "Error handling resource", e );
68          } finally
69          {
70              try
71              {
72                  if ( wagon != null )
73                  {
74                      wagon.disconnect();
75                  }
76              }
77              catch ( ConnectionException e )
78              {
79                  getLog().debug( "Error disconnecting wagon - ignored", e );
80              }
81          }
82      }
83  
84      /**
85       * Perform the necessary action. To be implemented in the child mojo.
86       *
87       * @param wagon - wagon instance to use
88       * @throws MojoExecutionException if any execution error
89       * @throws WagonException if any wagon error
90       * @throws IOException if any io error
91       */
92      protected abstract void execute( Wagon wagon )
93          throws MojoExecutionException, WagonException, IOException;
94  
95  }