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.Mojo;
24  import org.apache.maven.plugins.annotations.Parameter;
25  import org.apache.maven.wagon.Wagon;
26  import org.apache.maven.wagon.WagonException;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  import java.io.File;
30  
31  /**
32   * Download a single file.
33   */
34  @Mojo( name = "download-single", requiresProject = false)
35  public class DownloadSingleMojo
36      extends AbstractSingleWagonMojo
37  {
38      /**
39       * Relative path to the base URL. When empty, assume base URL has the full path
40       */
41      @Parameter( property = "wagon.fromFile")
42      private String fromFile;
43  
44      /**
45       * Directory to download the remote file to.
46       */
47      @Parameter( property = "wagon.toDir")
48      private File toDir;
49  
50      /**
51       * File to download the remote file to. Use this option to rename the file after download. When toDir is present,
52       * this argument is ignored.
53       */
54      @Parameter( property = "wagon.toFile")
55      private File toFile;
56  
57      /**
58       * Skip download if local file already exists.
59       */
60      @Parameter( property = "wagon.skipIfExists")
61      private boolean skipIfExists;
62  
63      @Override
64      protected void execute( Wagon wagon )
65          throws MojoExecutionException, WagonException
66      {
67  
68          if ( this.skip )
69          {
70              this.getLog().info( "Skip execution." );
71              return;
72          }
73  
74          if ( toDir != null )
75          {
76              toFile = new File( toDir, new File( fromFile ).getName() );
77          }
78  
79          if ( toFile == null )
80          {
81              throw new MojoExecutionException( "Either toDir or toFile is required" );
82          }
83  
84          if ( skipIfExists && toFile.exists() )
85          {
86              getLog().info("Skip execution - file " + toFile + " already exists." );
87              return;
88          }
89          this.getLog().info( "Downloading: " + wagon.getRepository().getUrl() + "/" + fromFile + " to " + toFile );
90  
91          wagon.get( fromFile, toFile );
92  
93      }
94  
95      @Override
96      public void execute()
97          throws MojoExecutionException
98      {
99          if ( StringUtils.isBlank( this.fromFile ) ) {
100 
101             //recompute base url and fromFile
102             String [] tokens = StringUtils.split( this.url, "/\\" );
103             this.fromFile = tokens[tokens.length - 1];
104             this.url = url.substring( 0, url.length() - this.fromFile.length() - 1 );
105         }
106 
107         super.execute();
108     }
109 }