1 package org
.argeo
.slc
.repo
.maven
;
4 import java
.util
.Properties
;
7 import javax
.xml
.parsers
.DocumentBuilder
;
8 import javax
.xml
.parsers
.DocumentBuilderFactory
;
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
;
21 * Static utilities around Maven which are NOT using the Maven APIs (conventions
24 public class MavenConventionsUtils
{
25 private final static Log log
= LogFactory
26 .getLog(MavenConventionsUtils
.class);
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.
33 * @see MavenConventionsUtils#artifactToFile(String, Artifact)
35 public static File
artifactToFile(Artifact artifact
) {
36 return artifactToFile(System
.getProperty("user.home") + File
.separator
37 + ".m2" + File
.separator
+ "repository", artifact
);
41 * Path to the file identified by this artifact <b>without</b> using Maven
42 * APIs (convention based).
44 * @param repositoryPath
45 * path to the related local repository location
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();
57 /** The file name of this artifact when stored */
58 public static String
artifactFileName(Artifact artifact
) {
59 return artifact
.getArtifactId()
61 + artifact
.getVersion()
62 + (artifact
.getClassifier().equals("") ?
"" : '-' + artifact
63 .getClassifier()) + '.' + artifact
.getExtension();
66 /** Absolute path to the file */
67 public static String
artifactPath(String artifactBasePath
, Artifact artifact
) {
68 return artifactParentPath(artifactBasePath
, artifact
) + '/'
69 + artifactFileName(artifact
);
72 /** Absolute path to the directories where the files will be stored */
73 public static String
artifactParentPath(String artifactBasePath
,
75 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/")
76 + artifactParentPath(artifact
);
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('.', '/');
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();
91 public static String
artifactsAsDependencyPom(Artifact pomArtifact
,
92 Set
<Artifact
> artifacts
, Artifact parent
) {
93 StringBuffer p
= new StringBuffer();
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>\n");
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");
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");
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");
130 p
.append("</dependencies>\n");
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>");
145 p
.append("</dependency>\n");
147 p
.append("</dependencies>\n");
148 p
.append("</dependencyManagement>\n");
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");
155 p
.append("</project>\n");
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.
165 public static void gatherPomDependencies(AetherTemplate aetherTemplate
,
166 Set
<Artifact
> artifacts
, Artifact pomArtifact
) {
167 if (log
.isDebugEnabled())
168 log
.debug("Gather dependencies for " + pomArtifact
);
171 File file
= aetherTemplate
.getResolvedFile(pomArtifact
);
172 DocumentBuilder documentBuilder
= DocumentBuilderFactory
173 .newInstance().newDocumentBuilder();
174 Document doc
= documentBuilder
.parse(file
);
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());
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 "
208 version
= props
.getProperty(versionKey
);
210 NodeList scopes
= dependency
.getElementsByTagName("scope");
211 if (scopes
.getLength() > 0
212 && scopes
.item(0).getTextContent().equals("import")) {
214 gatherPomDependencies(aetherTemplate
, artifacts
,
215 new DefaultArtifact(groupId
, artifactId
, "pom",
218 // TODO: deal with scope?
219 // TODO: deal with type
221 Artifact artifact
= new DefaultArtifact(groupId
,
222 artifactId
, type
, version
);
223 artifacts
.add(artifact
);
226 } catch (Exception e
) {
227 throw new SlcException("Cannot process " + pomArtifact
, e
);
231 /** Prevent instantiation */
232 private MavenConventionsUtils() {