1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.codehaus.mojo.license;
17  
18  import org.apache.maven.artifact.Artifact;
19  import org.apache.maven.model.Resource;
20  import org.apache.maven.project.MavenProject;
21  
22  import java.io.File;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.text.MessageFormat;
26  import java.util.Comparator;
27  import java.util.List;
28  
29  
30  
31  
32  
33  
34  
35  public class MojoHelper
36  {
37  
38      
39  
40  
41  
42  
43  
44  
45  
46      public static boolean addResourceDir( File dir, MavenProject project, String... includes )
47      {
48          List<?> resources = project.getResources();
49          return addResourceDir( dir, project, resources, includes );
50      }
51  
52      
53  
54  
55  
56  
57  
58  
59  
60  
61      public static boolean addResourceDir( File dir, MavenProject project, List<?> resources, String... includes )
62      {
63          String newresourceDir = dir.getAbsolutePath();
64          boolean shouldAdd = true;
65          for ( Object o : resources )
66          {
67              Resource r = (Resource) o;
68              if ( !r.getDirectory().equals( newresourceDir ) )
69              {
70                  continue;
71              }
72  
73              for ( String i : includes )
74              {
75                  if ( !r.getIncludes().contains( i ) )
76                  {
77                      r.addInclude( i );
78                  }
79              }
80              shouldAdd = false;
81              break;
82          }
83          if ( shouldAdd )
84          {
85              Resource r = new Resource();
86              r.setDirectory( newresourceDir );
87              for ( String i : includes )
88              {
89                  if ( !r.getIncludes().contains( i ) )
90                  {
91                      r.addInclude( i );
92                  }
93              }
94              project.addResource( r );
95          }
96          return shouldAdd;
97      }
98  
99      public static Comparator<MavenProject> newMavenProjectComparator()
100     {
101         return new Comparator<MavenProject>()
102         {
103             public int compare( MavenProject o1, MavenProject o2 )
104             {
105 
106                 String id1 = getArtifactId( o1.getArtifact() );
107                 String id2 = getArtifactId( o2.getArtifact() );
108                 return id1.compareTo( id2 );
109             }
110         };
111 
112     }
113 
114     static final protected double[] timeFactors = { 1000000, 1000, 60, 60, 24 };
115 
116     static final protected String[] timeUnites = { "ns", "ms", "s", "m", "h", "d" };
117 
118     static public String convertTime( long value )
119     {
120         return convert( value, timeFactors, timeUnites );
121     }
122 
123     static public String convert( long value, double[] factors, String[] unites )
124     {
125         long sign = value == 0 ? 1 : value / Math.abs( value );
126         int i = 0;
127         double tmp = Math.abs( value );
128         while ( i < factors.length && i < unites.length && tmp > factors[i] )
129         {
130             tmp = tmp / factors[i++];
131         }
132 
133         tmp *= sign;
134         String result;
135         result = MessageFormat.format( "{0,number,0.###}{1}", tmp, unites[i] );
136         return result;
137     }
138 
139     
140 
141 
142 
143 
144 
145 
146 
147     public static URL getUrl( URL baseUrl, String suffix )
148         throws IllegalArgumentException
149     {
150         String url = baseUrl.toString() + "/" + suffix;
151         try
152         {
153             return new URL( url );
154         }
155         catch ( MalformedURLException ex )
156         {
157             throw new IllegalArgumentException( "could not obtain url " + url, ex );
158         }
159     }
160 
161     public static String getArtifactId( Artifact artifact )
162     {
163         StringBuilder sb = new StringBuilder();
164         sb.append( artifact.getGroupId() );
165         sb.append( "--" );
166         sb.append( artifact.getArtifactId() );
167         sb.append( "--" );
168         sb.append( artifact.getVersion() );
169         return sb.toString();
170     }
171 
172     public static String getArtifactName( MavenProject project )
173     {
174         StringBuilder sb = new StringBuilder();
175         if ( project.getName().startsWith( "Unnamed -" ) )
176         {
177 
178             
179             sb.append( project.getArtifactId() );
180         }
181         else
182         {
183             sb.append( project.getName() );
184         }
185         sb.append( " (" );
186         sb.append( project.getGroupId() );
187         sb.append( ":" );
188         sb.append( project.getArtifactId() );
189         sb.append( ":" );
190         sb.append( project.getVersion() );
191         sb.append( " - " );
192         String url = project.getUrl();
193         sb.append( url == null ? "no url defined" : url );
194         sb.append( ")" );
195 
196         return sb.toString();
197     }
198 }