View Javadoc

1   package org.apache.maven.tools.plugin.util;
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.model.Dependency;
23  import org.apache.maven.plugin.descriptor.PluginDescriptor;
24  import org.codehaus.plexus.component.repository.ComponentDependency;
25  import org.codehaus.plexus.util.DirectoryScanner;
26  import org.codehaus.plexus.util.StringUtils;
27  import org.codehaus.plexus.util.xml.XMLWriter;
28  
29  import java.util.Iterator;
30  import java.util.LinkedList;
31  import java.util.List;
32  
33  /**
34   * @author jdcasey
35   */
36  public final class PluginUtils
37  {
38  
39      private PluginUtils()
40      {
41      }
42  
43      public static String[] findSources( String basedir, String include )
44      {
45          return PluginUtils.findSources( basedir, include, null );
46      }
47  
48      public static String[] findSources( String basedir, String include, String exclude )
49      {
50          DirectoryScanner scanner = new DirectoryScanner();
51          scanner.setBasedir( basedir );
52          scanner.setIncludes( new String[]{include} );
53          if ( !StringUtils.isEmpty( exclude ) )
54          {
55              // TODO: need default excludes in scanner
56              scanner.setExcludes( new String[]{exclude, "**/.svn/**"} );
57          }
58          else
59          {
60              scanner.setExcludes( new String[]{"**/.svn/**"} );
61          }
62  
63          scanner.scan();
64  
65          return scanner.getIncludedFiles();
66      }
67  
68      public static void writeDependencies( XMLWriter w, PluginDescriptor pluginDescriptor )
69      {
70  
71          w.startElement( "dependencies" );
72  
73          for ( Iterator it = pluginDescriptor.getDependencies().iterator(); it.hasNext(); )
74          {
75              ComponentDependency dep = (ComponentDependency) it.next();
76  
77              w.startElement( "dependency" );
78  
79              PluginUtils.element( w, "groupId", dep.getGroupId() );
80  
81              PluginUtils.element( w, "artifactId", dep.getArtifactId() );
82  
83              PluginUtils.element( w, "type", dep.getType() );
84  
85              PluginUtils.element( w, "version", dep.getVersion() );
86  
87              w.endElement();
88          }
89  
90          w.endElement();
91      }
92  
93      public static List toComponentDependencies( List dependencies )
94      {
95          List componentDeps = new LinkedList();
96  
97          for ( Iterator it = dependencies.iterator(); it.hasNext(); )
98          {
99              Dependency dependency = (Dependency) it.next();
100 
101             ComponentDependency cd = new ComponentDependency();
102 
103             cd.setArtifactId( dependency.getArtifactId() );
104             cd.setGroupId( dependency.getGroupId() );
105             cd.setVersion( dependency.getVersion() );
106             cd.setType( dependency.getType() );
107 
108             componentDeps.add( cd );
109         }
110 
111         return componentDeps;
112     }
113 
114     private static void element( XMLWriter w, String name, String value )
115     {
116         w.startElement( name );
117 
118         if ( value == null )
119         {
120             value = "";
121         }
122 
123         w.writeText( value );
124 
125         w.endElement();
126     }
127 
128 }