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
.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
;
36 * Static utilities around Maven which are NOT using the Maven APIs (conventions
39 public class MavenConventionsUtils
{
40 private final static Log log
= LogFactory
41 .getLog(MavenConventionsUtils
.class);
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.
48 * @see MavenConventionsUtils#artifactToFile(String, Artifact)
50 public static File
artifactToFile(Artifact artifact
) {
51 return artifactToFile(System
.getProperty("user.home") + File
.separator
52 + ".m2" + File
.separator
+ "repository", artifact
);
56 * Path to the file identified by this artifact <b>without</b> using Maven
57 * APIs (convention based).
59 * @param repositoryPath
60 * path to the related local repository location
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();
72 /** The file name of this artifact when stored */
73 public static String
artifactFileName(Artifact artifact
) {
74 return artifact
.getArtifactId()
76 + artifact
.getVersion()
77 + (artifact
.getClassifier().equals("") ?
"" : '-' + artifact
78 .getClassifier()) + '.' + artifact
.getExtension();
81 /** Absolute path to the file */
82 public static String
artifactPath(String artifactBasePath
, Artifact artifact
) {
83 return artifactParentPath(artifactBasePath
, artifact
) + '/'
84 + artifactFileName(artifact
);
87 /** Absolute path to the directories where the files will be stored */
88 public static String
artifactParentPath(String artifactBasePath
,
90 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/")
91 + 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("/") ?
"" : "/")
97 + groupId
.replace('.', '/');
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
.getVersion();
106 public static String
artifactsAsDependencyPom(Artifact pomArtifact
,
107 Set
<Artifact
> artifacts
, Artifact parent
) {
108 StringBuffer p
= new StringBuffer();
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");
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");
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");
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");
145 p
.append("</dependencies>\n");
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>");
160 p
.append("</dependency>\n");
162 p
.append("</dependencies>\n");
163 p
.append("</dependencyManagement>\n");
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");
170 p
.append("</project>\n");
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.
180 public static void gatherPomDependencies(AetherTemplate aetherTemplate
,
181 Set
<Artifact
> artifacts
, Artifact pomArtifact
) {
182 if (log
.isDebugEnabled())
183 log
.debug("Gather dependencies for " + pomArtifact
);
186 File file
= aetherTemplate
.getResolvedFile(pomArtifact
);
187 DocumentBuilder documentBuilder
= DocumentBuilderFactory
188 .newInstance().newDocumentBuilder();
189 Document doc
= documentBuilder
.parse(file
);
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());
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 "
223 version
= props
.getProperty(versionKey
);
225 NodeList scopes
= dependency
.getElementsByTagName("scope");
226 if (scopes
.getLength() > 0
227 && scopes
.item(0).getTextContent().equals("import")) {
229 gatherPomDependencies(aetherTemplate
, artifacts
,
230 new DefaultArtifact(groupId
, artifactId
, "pom",
233 // TODO: deal with scope?
234 // TODO: deal with type
236 Artifact artifact
= new DefaultArtifact(groupId
,
237 artifactId
, type
, version
);
238 artifacts
.add(artifact
);
241 } catch (Exception e
) {
242 throw new SlcException("Cannot process " + pomArtifact
, e
);
246 /** Prevent instantiation */
247 private MavenConventionsUtils() {