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