2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.repo
.maven
;
19 import java
.util
.Properties
;
22 import javax
.xml
.parsers
.DocumentBuilder
;
23 import javax
.xml
.parsers
.DocumentBuilderFactory
;
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
.eclipse
.aether
.artifact
.Artifact
;
30 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
31 import org
.w3c
.dom
.Document
;
32 import org
.w3c
.dom
.Element
;
33 import org
.w3c
.dom
.NodeList
;
36 * Static utilities around Maven which are NOT using the Maven APIs (conventions
39 public class MavenConventionsUtils
{
40 private final static Log log
= LogFactory
.getLog(MavenConventionsUtils
.class);
43 * Path to the file identified by this artifact <b>without</b> using Maven
44 * APIs (convention based). Default location of repository
45 * (~/.m2/repository) is used here.
47 * @see MavenConventionsUtils#artifactToFile(String, Artifact)
49 public static File
artifactToFile(Artifact artifact
) {
50 return artifactToFile(System
.getProperty("user.home") + File
.separator
+ ".m2" + File
.separator
+ "repository",
55 * Path to the file identified by this artifact <b>without</b> using Maven
56 * APIs (convention based).
58 * @param repositoryPath
59 * path to the related local repository location
63 public static File
artifactToFile(String repositoryPath
, Artifact artifact
) {
64 return new File(repositoryPath
+ File
.separator
+ artifact
.getGroupId().replace('.', File
.separatorChar
)
65 + File
.separator
+ artifact
.getArtifactId() + File
.separator
+ artifact
.getVersion() + File
.separator
66 + artifactFileName(artifact
)).getAbsoluteFile();
69 /** The file name of this artifact when stored */
70 public static String
artifactFileName(Artifact artifact
) {
71 return artifact
.getArtifactId() + '-' + artifact
.getVersion()
72 + (artifact
.getClassifier().equals("") ?
"" : '-' + artifact
.getClassifier()) + '.'
73 + artifact
.getExtension();
76 /** Absolute path to the file */
77 public static String
artifactPath(String artifactBasePath
, Artifact artifact
) {
78 return artifactParentPath(artifactBasePath
, artifact
) + '/' + artifactFileName(artifact
);
81 /** Absolute path to the file */
82 public static String
artifactUrl(String repoUrl
, Artifact artifact
) {
83 if (repoUrl
.endsWith("/"))
84 return repoUrl
+ artifactPath("/", artifact
).substring(1);
86 return repoUrl
+ artifactPath("/", artifact
);
89 /** Absolute path to the directories where the files will be stored */
90 public static String
artifactParentPath(String artifactBasePath
, Artifact artifact
) {
91 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + artifactParentPath(artifact
);
94 /** Absolute path to the directory of this group */
95 public static String
groupPath(String artifactBasePath
, String groupId
) {
96 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + groupId
.replace('.', '/');
99 /** Relative path to the directories where the files will be stored */
100 public static String
artifactParentPath(Artifact artifact
) {
101 return artifact
.getGroupId().replace('.', '/') + '/' + artifact
.getArtifactId() + '/'
102 + artifact
.getBaseVersion();
105 public static String
artifactsAsDependencyPom(Artifact pomArtifact
, Set
<Artifact
> artifacts
, Artifact parent
) {
106 StringBuffer p
= new StringBuffer();
109 p
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
111 "<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");
112 p
.append("<modelVersion>4.0.0</modelVersion>\n");
115 if (parent
!= null) {
116 p
.append("<parent>\n");
117 p
.append("<groupId>").append(parent
.getGroupId()).append("</groupId>\n");
118 p
.append("<artifactId>").append(parent
.getArtifactId()).append("</artifactId>\n");
119 p
.append("<version>").append(parent
.getVersion()).append("</version>\n");
120 p
.append("</parent>\n");
122 p
.append("<groupId>").append(pomArtifact
.getGroupId()).append("</groupId>\n");
123 p
.append("<artifactId>").append(pomArtifact
.getArtifactId()).append("</artifactId>\n");
124 p
.append("<version>").append(pomArtifact
.getVersion()).append("</version>\n");
125 p
.append("<packaging>pom</packaging>\n");
128 p
.append("<dependencies>\n");
129 for (Artifact a
: artifacts
) {
130 p
.append("\t<dependency>");
131 p
.append("<artifactId>").append(a
.getArtifactId()).append("</artifactId>");
132 p
.append("<groupId>").append(a
.getGroupId()).append("</groupId>");
133 if (!a
.getExtension().equals("jar"))
134 p
.append("<type>").append(a
.getExtension()).append("</type>");
135 p
.append("</dependency>\n");
137 p
.append("</dependencies>\n");
139 // Dependency management
140 p
.append("<dependencyManagement>\n");
141 p
.append("<dependencies>\n");
142 for (Artifact a
: artifacts
) {
143 p
.append("\t<dependency>");
144 p
.append("<artifactId>").append(a
.getArtifactId()).append("</artifactId>");
145 p
.append("<version>").append(a
.getVersion()).append("</version>");
146 p
.append("<groupId>").append(a
.getGroupId()).append("</groupId>");
147 if (a
.getExtension().equals("pom")) {
148 p
.append("<type>").append(a
.getExtension()).append("</type>");
149 p
.append("<scope>import</scope>");
151 p
.append("</dependency>\n");
153 p
.append("</dependencies>\n");
154 p
.append("</dependencyManagement>\n");
157 // p.append("<repositories>\n");
158 // p.append("<repository><id>argeo</id><url>http://maven.argeo.org/argeo</url></repository>\n");
159 // p.append("</repositories>\n");
161 p
.append("</project>\n");
166 * Directly parses Maven POM XML format in order to find all artifacts
167 * references under the dependency and dependencyManagement tags. This is
168 * meant to migrate existing pom registering a lot of artifacts, not to
169 * replace Maven resolving.
171 public static void gatherPomDependencies(AetherTemplate aetherTemplate
, Set
<Artifact
> artifacts
,
172 Artifact pomArtifact
) {
173 if (log
.isDebugEnabled())
174 log
.debug("Gather dependencies for " + pomArtifact
);
177 File file
= aetherTemplate
.getResolvedFile(pomArtifact
);
178 DocumentBuilder documentBuilder
= DocumentBuilderFactory
.newInstance().newDocumentBuilder();
179 Document doc
= documentBuilder
.parse(file
);
182 Properties props
= new Properties();
183 props
.setProperty("project.version", pomArtifact
.getBaseVersion());
184 NodeList properties
= doc
.getElementsByTagName("properties");
185 if (properties
.getLength() > 0) {
186 NodeList propertiesElems
= properties
.item(0).getChildNodes();
187 for (int i
= 0; i
< propertiesElems
.getLength(); i
++) {
188 if (propertiesElems
.item(i
) instanceof Element
) {
189 Element property
= (Element
) propertiesElems
.item(i
);
190 props
.put(property
.getNodeName(), property
.getTextContent());
195 // dependencies (direct and dependencyManagement)
196 NodeList dependencies
= doc
.getElementsByTagName("dependency");
197 for (int i
= 0; i
< dependencies
.getLength(); i
++) {
198 Element dependency
= (Element
) dependencies
.item(i
);
199 String groupId
= dependency
.getElementsByTagName("groupId").item(0).getTextContent().trim();
200 String artifactId
= dependency
.getElementsByTagName("artifactId").item(0).getTextContent().trim();
201 String version
= dependency
.getElementsByTagName("version").item(0).getTextContent().trim();
202 if (version
.startsWith("${")) {
203 String versionKey
= version
.substring(0, version
.length() - 1).substring(2);
204 if (!props
.containsKey(versionKey
))
205 throw new SlcException("Cannot interpret version " + version
);
206 version
= props
.getProperty(versionKey
);
208 NodeList scopes
= dependency
.getElementsByTagName("scope");
209 if (scopes
.getLength() > 0 && scopes
.item(0).getTextContent().equals("import")) {
211 gatherPomDependencies(aetherTemplate
, artifacts
,
212 new DefaultArtifact(groupId
, artifactId
, "pom", version
));
214 // TODO: deal with scope?
215 // TODO: deal with type
217 Artifact artifact
= new DefaultArtifact(groupId
, artifactId
, type
, version
);
218 artifacts
.add(artifact
);
221 } catch (Exception e
) {
222 throw new SlcException("Cannot process " + pomArtifact
, e
);
226 /** Prevent instantiation */
227 private MavenConventionsUtils() {