]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/MavenConventionsUtils.java
Put RAP upload and map widgets to org.argeo.tp.rap
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / maven / MavenConventionsUtils.java
1 package org.argeo.slc.repo.maven;
2
3 import java.io.File;
4 import java.util.Properties;
5 import java.util.Set;
6
7 import javax.xml.parsers.DocumentBuilder;
8 import javax.xml.parsers.DocumentBuilderFactory;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.slc.SlcException;
13 import org.argeo.slc.aether.AetherTemplate;
14 import org.sonatype.aether.artifact.Artifact;
15 import org.sonatype.aether.util.artifact.DefaultArtifact;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18 import org.w3c.dom.NodeList;
19
20 /**
21 * Static utilities around Maven which are NOT using the Maven APIs (conventions
22 * based).
23 */
24 public class MavenConventionsUtils {
25 private final static Log log = LogFactory
26 .getLog(MavenConventionsUtils.class);
27
28 /**
29 * Path to the file identified by this artifact <b>without</b> using Maven
30 * APIs (convention based). Default location of repository
31 * (~/.m2/repository) is used here.
32 *
33 * @see MavenConventionsUtils#artifactToFile(String, Artifact)
34 */
35 public static File artifactToFile(Artifact artifact) {
36 return artifactToFile(System.getProperty("user.home") + File.separator
37 + ".m2" + File.separator + "repository", artifact);
38 }
39
40 /**
41 * Path to the file identified by this artifact <b>without</b> using Maven
42 * APIs (convention based).
43 *
44 * @param repositoryPath
45 * path to the related local repository location
46 * @param artifact
47 * the artifact
48 */
49 public static File artifactToFile(String repositoryPath, Artifact artifact) {
50 return new File(repositoryPath + File.separator
51 + artifact.getGroupId().replace('.', File.separatorChar)
52 + File.separator + artifact.getArtifactId() + File.separator
53 + artifact.getVersion() + File.separator
54 + artifactFileName(artifact)).getAbsoluteFile();
55 }
56
57 /** The file name of this artifact when stored */
58 public static String artifactFileName(Artifact artifact) {
59 return artifact.getArtifactId()
60 + '-'
61 + artifact.getVersion()
62 + (artifact.getClassifier().equals("") ? "" : '-' + artifact
63 .getClassifier()) + '.' + artifact.getExtension();
64 }
65
66 /** Absolute path to the file */
67 public static String artifactPath(String artifactBasePath, Artifact artifact) {
68 return artifactParentPath(artifactBasePath, artifact) + '/'
69 + artifactFileName(artifact);
70 }
71
72 /** Absolute path to the directories where the files will be stored */
73 public static String artifactParentPath(String artifactBasePath,
74 Artifact artifact) {
75 return artifactBasePath + (artifactBasePath.endsWith("/") ? "" : "/")
76 + artifactParentPath(artifact);
77 }
78
79 /** Absolute path to the directory of this group */
80 public static String groupPath(String artifactBasePath, String groupId) {
81 return artifactBasePath + (artifactBasePath.endsWith("/") ? "" : "/")
82 + groupId.replace('.', '/');
83 }
84
85 /** Relative path to the directories where the files will be stored */
86 public static String artifactParentPath(Artifact artifact) {
87 return artifact.getGroupId().replace('.', '/') + '/'
88 + artifact.getArtifactId() + '/' + artifact.getVersion();
89 }
90
91 public static String artifactsAsDependencyPom(Artifact pomArtifact,
92 Set<Artifact> artifacts, Artifact parent) {
93 StringBuffer p = new StringBuffer();
94
95 // XML header
96 p.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
97 p.append("<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");
98 p.append("<modelVersion>4.0.0</modelVersion>");
99
100 // Artifact
101 if (parent != null) {
102 p.append("<parent>\n");
103 p.append("<groupId>").append(parent.getGroupId())
104 .append("</groupId>\n");
105 p.append("<artifactId>").append(parent.getArtifactId())
106 .append("</artifactId>\n");
107 p.append("<version>").append(parent.getVersion())
108 .append("</version>\n");
109 p.append("</parent>\n");
110 }
111 p.append("<groupId>").append(pomArtifact.getGroupId())
112 .append("</groupId>\n");
113 p.append("<artifactId>").append(pomArtifact.getArtifactId())
114 .append("</artifactId>\n");
115 p.append("<version>").append(pomArtifact.getVersion())
116 .append("</version>\n");
117 p.append("<packaging>pom</packaging>\n");
118
119 // Dependencies
120 p.append("<dependencies>\n");
121 for (Artifact a : artifacts) {
122 p.append("\t<dependency>");
123 p.append("<artifactId>").append(a.getArtifactId())
124 .append("</artifactId>");
125 p.append("<groupId>").append(a.getGroupId()).append("</groupId>");
126 if (!a.getExtension().equals("jar"))
127 p.append("<type>").append(a.getExtension()).append("</type>");
128 p.append("</dependency>\n");
129 }
130 p.append("</dependencies>\n");
131
132 // Dependency management
133 p.append("<dependencyManagement>\n");
134 p.append("<dependencies>\n");
135 for (Artifact a : artifacts) {
136 p.append("\t<dependency>");
137 p.append("<artifactId>").append(a.getArtifactId())
138 .append("</artifactId>");
139 p.append("<version>").append(a.getVersion()).append("</version>");
140 p.append("<groupId>").append(a.getGroupId()).append("</groupId>");
141 if (a.getExtension().equals("pom")) {
142 p.append("<type>").append(a.getExtension()).append("</type>");
143 p.append("<scope>import</scope>");
144 }
145 p.append("</dependency>\n");
146 }
147 p.append("</dependencies>\n");
148 p.append("</dependencyManagement>\n");
149
150 // Repositories
151 p.append("<repositories>\n");
152 p.append("<repository><id>argeo</id><url>http://maven.argeo.org/argeo</url></repository>\n");
153 p.append("</repositories>\n");
154
155 p.append("</project>\n");
156 return p.toString();
157 }
158
159 /**
160 * Directly parses Maven POM XML format in order to find all artifacts
161 * references under the dependency and dependencyManagement tags. This is
162 * meant to migrate existing pom registering a lot of artifacts, not to
163 * replace Maven resolving.
164 */
165 public static void gatherPomDependencies(AetherTemplate aetherTemplate,
166 Set<Artifact> artifacts, Artifact pomArtifact) {
167 if (log.isDebugEnabled())
168 log.debug("Gather dependencies for " + pomArtifact);
169
170 try {
171 File file = aetherTemplate.getResolvedFile(pomArtifact);
172 DocumentBuilder documentBuilder = DocumentBuilderFactory
173 .newInstance().newDocumentBuilder();
174 Document doc = documentBuilder.parse(file);
175
176 // properties
177 Properties props = new Properties();
178 props.setProperty("project.version", pomArtifact.getBaseVersion());
179 NodeList properties = doc.getElementsByTagName("properties");
180 if (properties.getLength() > 0) {
181 NodeList propertiesElems = properties.item(0).getChildNodes();
182 for (int i = 0; i < propertiesElems.getLength(); i++) {
183 if (propertiesElems.item(i) instanceof Element) {
184 Element property = (Element) propertiesElems.item(i);
185 props.put(property.getNodeName(),
186 property.getTextContent());
187 }
188 }
189 }
190
191 // dependencies (direct and dependencyManagement)
192 NodeList dependencies = doc.getElementsByTagName("dependency");
193 for (int i = 0; i < dependencies.getLength(); i++) {
194 Element dependency = (Element) dependencies.item(i);
195 String groupId = dependency.getElementsByTagName("groupId")
196 .item(0).getTextContent().trim();
197 String artifactId = dependency
198 .getElementsByTagName("artifactId").item(0)
199 .getTextContent().trim();
200 String version = dependency.getElementsByTagName("version")
201 .item(0).getTextContent().trim();
202 if (version.startsWith("${")) {
203 String versionKey = version.substring(0,
204 version.length() - 1).substring(2);
205 if (!props.containsKey(versionKey))
206 throw new SlcException("Cannot interpret version "
207 + version);
208 version = props.getProperty(versionKey);
209 }
210 NodeList scopes = dependency.getElementsByTagName("scope");
211 if (scopes.getLength() > 0
212 && scopes.item(0).getTextContent().equals("import")) {
213 // recurse
214 gatherPomDependencies(aetherTemplate, artifacts,
215 new DefaultArtifact(groupId, artifactId, "pom",
216 version));
217 } else {
218 // TODO: deal with scope?
219 // TODO: deal with type
220 String type = "jar";
221 Artifact artifact = new DefaultArtifact(groupId,
222 artifactId, type, version);
223 artifacts.add(artifact);
224 }
225 }
226 } catch (Exception e) {
227 throw new SlcException("Cannot process " + pomArtifact, e);
228 }
229 }
230
231 /** Prevent instantiation */
232 private MavenConventionsUtils() {
233 }
234 }