]> git.argeo.org Git - gpl/argeo-slc.git/blob - maven/MavenConventionsUtils.java
Prepare next development cycle
[gpl/argeo-slc.git] / 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 /** Relative path to the directories where the files will be stored */
80 public static String artifactParentPath(Artifact artifact) {
81 return artifact.getGroupId().replace('.', '/') + '/'
82 + artifact.getArtifactId() + '/' + artifact.getVersion();
83 }
84
85 public static String artifactsAsDependencyPom(Artifact pomArtifact,
86 Set<Artifact> artifacts) {
87 StringBuffer b = new StringBuffer();
88
89 // XML header
90 b.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
91 b.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");
92 b.append("<modelVersion>4.0.0</modelVersion>");
93
94 // Artifact
95 b.append("<parent><groupId>org.argeo</groupId><artifactId>parent</artifactId><version>1.2.0</version></parent>\n");
96 b.append("<groupId>").append(pomArtifact.getGroupId())
97 .append("</groupId>\n");
98 b.append("<artifactId>").append(pomArtifact.getArtifactId())
99 .append("</artifactId>\n");
100 b.append("<version>").append(pomArtifact.getVersion())
101 .append("</version>\n");
102 b.append("<packaging>pom</packaging>\n");
103
104 // Dependencies
105 b.append("<dependencies>\n");
106 for (Artifact artifact : artifacts) {
107 b.append("\t<dependency>");
108 b.append("<artifactId>").append(artifact.getArtifactId())
109 .append("</artifactId>");
110 b.append("<groupId>").append(artifact.getGroupId())
111 .append("</groupId>");
112 b.append("</dependency>\n");
113 }
114 b.append("</dependencies>\n");
115
116 // Dependency management
117 b.append("<dependencyManagement>\n");
118 b.append("<dependencies>\n");
119 for (Artifact artifact : artifacts) {
120 b.append("\t<dependency>");
121 b.append("<artifactId>").append(artifact.getArtifactId())
122 .append("</artifactId>");
123 b.append("<version>").append(artifact.getVersion())
124 .append("</version>");
125 b.append("<groupId>").append(artifact.getGroupId())
126 .append("</groupId>");
127 b.append("</dependency>\n");
128 }
129 b.append("</dependencies>\n");
130 b.append("</dependencyManagement>\n");
131
132 // Repositories
133 b.append("<repositories>\n");
134 b.append("<repository><id>argeo</id><url>http://maven.argeo.org/argeo</url></repository>\n");
135 b.append("</repositories>\n");
136
137 b.append("</project>\n");
138 return b.toString();
139 }
140
141 /**
142 * Directly parses Maven POM XML format in order to find all artifacts
143 * references under the dependency and dependencyManagement tags. This is
144 * meant to migrate existing pom registering a lot of artifacts, not to
145 * replace Maven resolving.
146 */
147 public static void gatherPomDependencies(AetherTemplate aetherTemplate,
148 Set<Artifact> artifacts, Artifact pomArtifact) {
149 if (log.isDebugEnabled())
150 log.debug("Gather dependencies for " + pomArtifact);
151
152 try {
153 File file = aetherTemplate.getResolvedFile(pomArtifact);
154 DocumentBuilder documentBuilder = DocumentBuilderFactory
155 .newInstance().newDocumentBuilder();
156 Document doc = documentBuilder.parse(file);
157
158 // properties
159 Properties props = new Properties();
160 props.setProperty("project.version", pomArtifact.getBaseVersion());
161 NodeList properties = doc.getElementsByTagName("properties");
162 if (properties.getLength() > 0) {
163 NodeList propertiesElems = properties.item(0).getChildNodes();
164 for (int i = 0; i < propertiesElems.getLength(); i++) {
165 if (propertiesElems.item(i) instanceof Element) {
166 Element property = (Element) propertiesElems.item(i);
167 props.put(property.getNodeName(),
168 property.getTextContent());
169 }
170 }
171 }
172
173 // dependencies (direct and dependencyManagement)
174 NodeList dependencies = doc.getElementsByTagName("dependency");
175 for (int i = 0; i < dependencies.getLength(); i++) {
176 Element dependency = (Element) dependencies.item(i);
177 String groupId = dependency.getElementsByTagName("groupId")
178 .item(0).getTextContent().trim();
179 String artifactId = dependency
180 .getElementsByTagName("artifactId").item(0)
181 .getTextContent().trim();
182 String version = dependency.getElementsByTagName("version")
183 .item(0).getTextContent().trim();
184 if (version.startsWith("${")) {
185 String versionKey = version.substring(0,
186 version.length() - 1).substring(2);
187 if (!props.containsKey(versionKey))
188 throw new SlcException("Cannot interpret version "
189 + version);
190 version = props.getProperty(versionKey);
191 }
192 NodeList scopes = dependency.getElementsByTagName("scope");
193 if (scopes.getLength() > 0
194 && scopes.item(0).getTextContent().equals("import")) {
195 // recurse
196 gatherPomDependencies(aetherTemplate, artifacts,
197 new DefaultArtifact(groupId, artifactId, "pom",
198 version));
199 } else {
200 // TODO: deal with scope?
201 // TODO: deal with type
202 String type = "jar";
203 Artifact artifact = new DefaultArtifact(groupId,
204 artifactId, type, version);
205 artifacts.add(artifact);
206 }
207 }
208 } catch (Exception e) {
209 throw new SlcException("Cannot process " + pomArtifact, e);
210 }
211 }
212
213 /** Prevent instantiation */
214 private MavenConventionsUtils() {
215 }
216 }