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