View Javadoc
1   package com.codehaus.mojo.vfs;
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.util.List;
23  
24  import org.apache.commons.vfs2.FileObject;
25  import org.apache.commons.vfs2.FileSystemException;
26  import org.apache.commons.vfs2.FileSystemOptions;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.codehaus.mojo.vfs.VfsFileSet;
32  import org.codehaus.mojo.vfs.VfsFileSetManager;
33  import org.codehaus.mojo.vfs.VfsUtils;
34  import org.codehaus.mojo.vfs.internal.DefaultVfsFileSetManager;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
37  
38  /**
39   * Display file list of a virtual file system.
40   */
41  @Mojo( name = "list", requiresProject = false, threadSafe = true )
42  public class ListVfsMojo
43      extends AbstractVfsMojo
44  {
45      /**
46       * Source URL
47       * @since 1.0 beta 1
48       */
49      @Parameter( property = "source", required = true )
50      private String source;
51  
52      /**
53       * Maven settings server's source authentication id
54       * @since 1.0 beta 1
55       */
56      @Parameter( property = "sourceId", required = false )
57      private String sourceId;
58  
59      /**
60       * Comma separated ANT include format
61       * @since 1.0 beta 1
62       */
63      @Parameter( property = "includes", required = false )
64      private String includes;
65  
66      /**
67       * Comma separated ANT exclude format
68       * @since 1.0 beta 1
69       */
70      @Parameter( property = "excludes", required = false )
71      private String excludes;
72  
73      public void execute()
74          throws MojoExecutionException, MojoFailureException
75      {
76          if ( skip )
77          {
78              this.getLog().info( "Skip VFS list operation" );
79              return;
80          }
81  
82          MojoVfsFileSet fileset = new MojoVfsFileSet();
83  
84          fileset.setSource( source );
85          fileset.setSourceId( sourceId );
86  
87          if ( !StringUtils.isBlank( includes ) )
88          {
89              fileset.setIncludes( StringUtils.split( includes, "," ) );
90          }
91  
92          if ( !StringUtils.isBlank( excludes ) )
93          {
94              fileset.setExcludes( StringUtils.split( excludes, "," ) );
95          }
96  
97          try
98          {
99              FileSystemOptions serverOptions = this.getFileSystemOptions( sourceId, source );
100             VfsFileSet vfsFileSet = new VfsFileSet();
101             vfsFileSet.copyBase( fileset );
102 
103             FileObject sourceObj = getFileSystemManager().resolveFile( fileset.getSource(), serverOptions );
104             vfsFileSet.setSource( sourceObj );
105 
106             VfsFileSetManager fileSetManager = new DefaultVfsFileSetManager();
107             List<FileObject> list = fileSetManager.list( vfsFileSet );
108 
109             this.getLog().info( "Directory list: " );
110             for ( FileObject fo : list )
111             {
112                 this.getLog().info( VfsUtils.getRelativePath( sourceObj, fo ) );
113             }
114         }
115         catch ( FileSystemException e )
116         {
117             throw new MojoFailureException( "Unable to perform a list operation", e );
118         }
119         catch ( SecDispatcherException e )
120         {
121             throw new MojoFailureException( "Unable to perform a list operation", e );
122         }
123     }
124 }