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 java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStream;
26  import java.io.PrintStream;
27  import java.util.List;
28  
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.codehaus.plexus.util.IOUtil;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  import de.schlichtherle.truezip.file.TFile;
35  
36  /**
37   * List all files in the archive.
38   * 
39   * @goal list
40   * @phase process-resources
41   * @version $Id: $
42   */
43  public class ListMojo
44      extends AbstractManipulateArchiveMojo
45  {
46  
47      /**
48       * Write list output to a file if needed.
49       * 
50       * @parameter
51       * @since 1.0 beta-1
52       */
53      private File outputFile;
54  
55      /**
56       * Use full path to display the list. Useful to get a relative content per fileset.
57       * 
58       * @parameter default-value="true"
59       * @since 1.1
60       */
61      private boolean printFullPath = true;
62  
63      public void execute()
64          throws MojoExecutionException, MojoFailureException
65      {
66  
67          if ( skip )
68          {
69              this.getLog().info( "Skip this execution" );
70              return;
71          }
72  
73          super.execute();
74  
75          intitializeArchiveDectector();
76  
77          PrintStream ps = System.out;
78  
79          OutputStream os = null;
80  
81          if ( this.outputFile != null )
82          {
83              try
84              {
85                  outputFile.getParentFile().mkdirs();
86                  os = new FileOutputStream( outputFile );
87                  ps = new PrintStream( os );
88              }
89              catch ( IOException e )
90              {
91                  throw new MojoFailureException( e.getMessage() );
92              }
93          }
94  
95          try
96          {
97              this.processFileSets( ps );
98          }
99          finally
100         {
101             IOUtil.close( os );
102         }
103 
104         this.tryImmediateUpdate();
105 
106     }
107 
108     private void processFileSets( PrintStream ps )
109         throws MojoExecutionException, MojoFailureException
110     {
111         if ( this.fileset != null )
112         {
113             this.filesets.add( this.fileset );
114             this.fileset = null;
115         }
116 
117         for ( int i = 0; i < filesets.size(); ++i )
118         {
119             Fileset fileSet = (Fileset) this.filesets.get( i );
120 
121             if ( StringUtils.isBlank( fileSet.getDirectory() ) )
122             {
123                 fileSet.setDirectory( this.project.getBasedir().getAbsolutePath() );
124             }
125 
126             this.resolveRelativePath( fileSet );
127 
128             List<TFile> fileList = this.truezip.list( this.filesets.get( i ) );
129 
130             for ( int j = 0; j < fileList.size(); ++j )
131             {
132                 TFile file = fileList.get( j );
133                 if ( printFullPath )
134                 {
135                     ps.println( ( file.getPath() ) );
136                 }
137                 else
138                 {
139                     int startPos = fileSet.getDirectory().length() + 1;
140                     ps.println( ( file.getPath().substring( startPos ) ) );
141                 }
142 
143             }
144         }
145     }
146 }