View Javadoc
1   package org.codehaus.mojo.truezip;
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.plugin.MojoFailureException;
24  import org.codehaus.plexus.util.StringUtils;
25  
26  import de.schlichtherle.truezip.file.TFile;
27  
28  /**
29   * Copy a set of files in and out of an existing archive.
30   * 
31   * @goal copy
32   * @phase process-resources
33   * @version $Id: $
34   */
35  public class CopyMojo
36      extends AbstractManipulateArchiveMojo
37  {
38  
39      /**
40       * The list of FileItem to manipulate the archive with. Use this configuration when you have a need to do copying
41       * with the option to change the file name.
42       * 
43       * @parameter
44       * @since 1.0 beta-1
45       */
46      private FileItem[] files;
47  
48      public void execute()
49          throws MojoExecutionException, MojoFailureException
50      {
51  
52          if ( skip )
53          {
54              this.getLog().info( "Skip this execution" );
55              return;
56          }
57  
58          super.execute();
59  
60          intitializeArchiveDectector();
61  
62          this.processFileItems();
63  
64          this.processFileSets();
65  
66          this.tryImmediateUpdate();
67      }
68  
69      private void processFileSets()
70          throws MojoExecutionException, MojoFailureException
71      {
72          if ( this.fileset != null )
73          {
74              this.filesets.add( this.fileset );
75              this.fileset = null;
76          }
77  
78          for ( int i = 0; i < filesets.size(); ++i )
79          {
80              Fileset fileSet = (Fileset) this.filesets.get( i );
81  
82              if ( StringUtils.isBlank( fileSet.getDirectory() ) )
83              {
84                  fileSet.setDirectory( this.project.getBasedir().getAbsolutePath() );
85              }
86  
87              try
88              {
89                  this.resolveRelativePath( fileSet );
90                  this.truezip.copy( fileSet );
91              }
92              catch ( Exception e )
93              {
94                  throw new MojoExecutionException( "Copy fileset fails", e );
95              }
96          }
97      }
98  
99      private void processFileItems()
100         throws MojoExecutionException, MojoFailureException
101     {
102         for ( int i = 0; files != null && i < files.length; ++i )
103         {
104             FileItem copyInfo = files[i];
105 
106             this.resolveRelativePath( copyInfo );
107 
108             TFile source = new TFile( copyInfo.getSource() );
109 
110             TFile dest = new TFile( copyInfo.getDestinationPath() );
111 
112             try
113             {
114                 this.truezip.copyFile( source, dest );
115             }
116             catch ( Exception e )
117             {
118                 throw new MojoExecutionException( "Copy fileset fails", e );
119             }
120         }
121 
122     }
123 
124 }