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