1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.slc.repo.maven;
17
18 import java.io.File;
19 import java.util.Set;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.eclipse.aether.artifact.Artifact;
24
25
26
27
28
29 public class MavenConventionsUtils {
30 private final static Log log = LogFactory.getLog(MavenConventionsUtils.class);
31
32
33
34
35
36
37
38
39 public static File artifactToFile(Artifact artifact) {
40 return artifactToFile(System.getProperty("user.home") + File.separator + ".m2" + File.separator + "repository",
41 artifact);
42 }
43
44
45
46
47
48
49
50
51
52
53 public static File artifactToFile(String repositoryPath, Artifact artifact) {
54 return new File(repositoryPath + File.separator + artifact.getGroupId().replace('.', File.separatorChar)
55 + File.separator + artifact.getArtifactId() + File.separator + artifact.getVersion() + File.separator
56 + artifactFileName(artifact)).getAbsoluteFile();
57 }
58
59
60 public static String artifactFileName(Artifact artifact) {
61 return artifact.getArtifactId() + '-' + artifact.getVersion()
62 + (artifact.getClassifier().equals("") ? "" : '-' + artifact.getClassifier()) + '.'
63 + artifact.getExtension();
64 }
65
66
67 public static String artifactPath(String artifactBasePath, Artifact artifact) {
68 return artifactParentPath(artifactBasePath, artifact) + '/' + artifactFileName(artifact);
69 }
70
71
72 public static String artifactUrl(String repoUrl, Artifact artifact) {
73 if (repoUrl.endsWith("/"))
74 return repoUrl + artifactPath("/", artifact).substring(1);
75 else
76 return repoUrl + artifactPath("/", artifact);
77 }
78
79
80 public static String artifactParentPath(String artifactBasePath, Artifact artifact) {
81 return artifactBasePath + (artifactBasePath.endsWith("/") ? "" : "/") + artifactParentPath(artifact);
82 }
83
84
85 public static String groupPath(String artifactBasePath, String groupId) {
86 return artifactBasePath + (artifactBasePath.endsWith("/") ? "" : "/") + groupId.replace('.', '/');
87 }
88
89
90 public static String artifactParentPath(Artifact artifact) {
91 return artifact.getGroupId().replace('.', '/') + '/' + artifact.getArtifactId() + '/'
92 + artifact.getBaseVersion();
93 }
94
95 public static String artifactsAsDependencyPom(Artifact pomArtifact, Set<Artifact> artifacts, Artifact parent) {
96 StringBuffer p = new StringBuffer();
97
98
99 p.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
100 p.append(
101 "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n");
102 p.append("<modelVersion>4.0.0</modelVersion>\n");
103
104
105 if (parent != null) {
106 p.append("<parent>\n");
107 p.append("<groupId>").append(parent.getGroupId()).append("</groupId>\n");
108 p.append("<artifactId>").append(parent.getArtifactId()).append("</artifactId>\n");
109 p.append("<version>").append(parent.getVersion()).append("</version>\n");
110 p.append("</parent>\n");
111 }
112 p.append("<groupId>").append(pomArtifact.getGroupId()).append("</groupId>\n");
113 p.append("<artifactId>").append(pomArtifact.getArtifactId()).append("</artifactId>\n");
114 p.append("<version>").append(pomArtifact.getVersion()).append("</version>\n");
115 p.append("<packaging>pom</packaging>\n");
116
117
118 p.append("<dependencies>\n");
119 for (Artifact a : artifacts) {
120 p.append("\t<dependency>");
121 p.append("<artifactId>").append(a.getArtifactId()).append("</artifactId>");
122 p.append("<groupId>").append(a.getGroupId()).append("</groupId>");
123 if (!a.getExtension().equals("jar"))
124 p.append("<type>").append(a.getExtension()).append("</type>");
125 p.append("</dependency>\n");
126 }
127 p.append("</dependencies>\n");
128
129
130 p.append("<dependencyManagement>\n");
131 p.append("<dependencies>\n");
132 for (Artifact a : artifacts) {
133 p.append("\t<dependency>");
134 p.append("<artifactId>").append(a.getArtifactId()).append("</artifactId>");
135 p.append("<version>").append(a.getVersion()).append("</version>");
136 p.append("<groupId>").append(a.getGroupId()).append("</groupId>");
137 if (a.getExtension().equals("pom")) {
138 p.append("<type>").append(a.getExtension()).append("</type>");
139 p.append("<scope>import</scope>");
140 }
141 p.append("</dependency>\n");
142 }
143 p.append("</dependencies>\n");
144 p.append("</dependencyManagement>\n");
145
146
147
148
149
150
151 p.append("</project>\n");
152 return p.toString();
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 private MavenConventionsUtils() {
218 }
219 }