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 directories where the files will be stored */
67 public static String
artifactParentPath(String artifactBasePath
,
69 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/")
70 + artifactParentPath(artifact
);
73 /** Relative path to the directories where the files will be stored */
74 public static String
artifactParentPath(Artifact artifact
) {
75 return artifact
.getGroupId().replace('.', '/') + '/'
76 + artifact
.getArtifactId() + '/' + artifact
.getVersion();
79 public static String
artifactsAsDependencyPom(Artifact pomArtifact
,
80 Set
<Artifact
> artifacts
) {
81 StringBuffer b
= new StringBuffer();
84 b
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
85 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");
86 b
.append("<modelVersion>4.0.0</modelVersion>");
89 b
.append("<parent><groupId>org.argeo</groupId><artifactId>parent</artifactId><version>1.2.0</version></parent>\n");
90 b
.append("<groupId>").append(pomArtifact
.getGroupId())
91 .append("</groupId>\n");
92 b
.append("<artifactId>").append(pomArtifact
.getArtifactId())
93 .append("</artifactId>\n");
94 b
.append("<version>").append(pomArtifact
.getVersion())
95 .append("</version>\n");
96 b
.append("<packaging>pom</packaging>\n");
99 b
.append("<dependencies>\n");
100 for (Artifact artifact
: artifacts
) {
101 b
.append("\t<dependency>");
102 b
.append("<artifactId>").append(artifact
.getArtifactId())
103 .append("</artifactId>");
104 b
.append("<groupId>").append(artifact
.getGroupId())
105 .append("</groupId>");
106 b
.append("</dependency>\n");
108 b
.append("</dependencies>\n");
110 // Dependency management
111 b
.append("<dependencyManagement>\n");
112 b
.append("<dependencies>\n");
113 for (Artifact artifact
: artifacts
) {
114 b
.append("\t<dependency>");
115 b
.append("<artifactId>").append(artifact
.getArtifactId())
116 .append("</artifactId>");
117 b
.append("<version>").append(artifact
.getVersion())
118 .append("</version>");
119 b
.append("<groupId>").append(artifact
.getGroupId())
120 .append("</groupId>");
121 b
.append("</dependency>\n");
123 b
.append("</dependencies>\n");
124 b
.append("</dependencyManagement>\n");
127 b
.append("<repositories>\n");
128 b
.append("<repository><id>argeo</id><url>http://maven.argeo.org/argeo</url></repository>\n");
129 b
.append("</repositories>\n");
131 b
.append("</project>\n");
136 * Directly parses Maven POM XML format in order to find all artifacts
137 * references under the dependency and dependencyManagement tags. This is
138 * meant to migrate existing pom registering a lot of artifacts, not to
139 * replace Maven resolving.
141 public static void gatherPomDependencies(AetherTemplate aetherTemplate
,
142 Set
<Artifact
> artifacts
, Artifact pomArtifact
) {
143 if (log
.isDebugEnabled())
144 log
.debug("Gather dependencies for " + pomArtifact
);
147 File file
= aetherTemplate
.getResolvedFile(pomArtifact
);
148 DocumentBuilder documentBuilder
= DocumentBuilderFactory
149 .newInstance().newDocumentBuilder();
150 Document doc
= documentBuilder
.parse(file
);
153 Properties props
= new Properties();
154 props
.setProperty("project.version", pomArtifact
.getBaseVersion());
155 NodeList properties
= doc
.getElementsByTagName("properties");
156 if (properties
.getLength() > 0) {
157 NodeList propertiesElems
= properties
.item(0).getChildNodes();
158 for (int i
= 0; i
< propertiesElems
.getLength(); i
++) {
159 if (propertiesElems
.item(i
) instanceof Element
) {
160 Element property
= (Element
) propertiesElems
.item(i
);
161 props
.put(property
.getNodeName(),
162 property
.getTextContent());
167 // dependencies (direct and dependencyManagement)
168 NodeList dependencies
= doc
.getElementsByTagName("dependency");
169 for (int i
= 0; i
< dependencies
.getLength(); i
++) {
170 Element dependency
= (Element
) dependencies
.item(i
);
171 String groupId
= dependency
.getElementsByTagName("groupId")
172 .item(0).getTextContent().trim();
173 String artifactId
= dependency
174 .getElementsByTagName("artifactId").item(0)
175 .getTextContent().trim();
176 String version
= dependency
.getElementsByTagName("version")
177 .item(0).getTextContent().trim();
178 if (version
.startsWith("${")) {
179 String versionKey
= version
.substring(0,
180 version
.length() - 1).substring(2);
181 if (!props
.containsKey(versionKey
))
182 throw new SlcException("Cannot interpret version "
184 version
= props
.getProperty(versionKey
);
186 NodeList scopes
= dependency
.getElementsByTagName("scope");
187 if (scopes
.getLength() > 0
188 && scopes
.item(0).getTextContent().equals("import")) {
190 gatherPomDependencies(aetherTemplate
, artifacts
,
191 new DefaultArtifact(groupId
, artifactId
, "pom",
194 // TODO: deal with scope?
195 // TODO: deal with type
197 Artifact artifact
= new DefaultArtifact(groupId
,
198 artifactId
, type
, version
);
199 artifacts
.add(artifact
);
202 } catch (Exception e
) {
203 throw new SlcException("Cannot process " + pomArtifact
, e
);
207 /** Prevent instantiation */
208 private MavenConventionsUtils() {