]>
git.argeo.org Git - gpl/argeo-slc.git/blob - cms/org.argeo.slc.repo/src/org/argeo/slc/repo/maven/MavenConventionsUtils.java
1 package org
.argeo
.slc
.repo
.maven
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.eclipse
.aether
.artifact
.Artifact
;
11 * Static utilities around Maven which are NOT using the Maven APIs (conventions
14 public class MavenConventionsUtils
{
15 private final static Log log
= LogFactory
.getLog(MavenConventionsUtils
.class);
18 * Path to the file identified by this artifact <b>without</b> using Maven
19 * APIs (convention based). Default location of repository
20 * (~/.m2/repository) is used here.
22 * @see MavenConventionsUtils#artifactToFile(String, Artifact)
24 public static File
artifactToFile(Artifact artifact
) {
25 return artifactToFile(System
.getProperty("user.home") + File
.separator
+ ".m2" + File
.separator
+ "repository",
30 * Path to the file identified by this artifact <b>without</b> using Maven
31 * APIs (convention based).
33 * @param repositoryPath
34 * path to the related local repository location
38 public static File
artifactToFile(String repositoryPath
, Artifact artifact
) {
39 return new File(repositoryPath
+ File
.separator
+ artifact
.getGroupId().replace('.', File
.separatorChar
)
40 + File
.separator
+ artifact
.getArtifactId() + File
.separator
+ artifact
.getVersion() + File
.separator
41 + artifactFileName(artifact
)).getAbsoluteFile();
44 /** The file name of this artifact when stored */
45 public static String
artifactFileName(Artifact artifact
) {
46 return artifact
.getArtifactId() + '-' + artifact
.getVersion()
47 + (artifact
.getClassifier().equals("") ?
"" : '-' + artifact
.getClassifier()) + '.'
48 + artifact
.getExtension();
51 /** Absolute path to the file */
52 public static String
artifactPath(String artifactBasePath
, Artifact artifact
) {
53 return artifactParentPath(artifactBasePath
, artifact
) + '/' + artifactFileName(artifact
);
56 /** Absolute path to the file */
57 public static String
artifactUrl(String repoUrl
, Artifact artifact
) {
58 if (repoUrl
.endsWith("/"))
59 return repoUrl
+ artifactPath("/", artifact
).substring(1);
61 return repoUrl
+ artifactPath("/", artifact
);
64 /** Absolute path to the directories where the files will be stored */
65 public static String
artifactParentPath(String artifactBasePath
, Artifact artifact
) {
66 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + artifactParentPath(artifact
);
69 /** Absolute path to the directory of this group */
70 public static String
groupPath(String artifactBasePath
, String groupId
) {
71 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + groupId
.replace('.', '/');
74 /** Relative path to the directories where the files will be stored */
75 public static String
artifactParentPath(Artifact artifact
) {
76 return artifact
.getGroupId().replace('.', '/') + '/' + artifact
.getArtifactId() + '/'
77 + artifact
.getBaseVersion();
80 public static String
artifactsAsDependencyPom(Artifact pomArtifact
, Set
<Artifact
> artifacts
, Artifact parent
) {
81 StringBuffer p
= new StringBuffer();
84 p
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
86 "<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");
87 p
.append("<modelVersion>4.0.0</modelVersion>\n");
91 p
.append("<parent>\n");
92 p
.append("<groupId>").append(parent
.getGroupId()).append("</groupId>\n");
93 p
.append("<artifactId>").append(parent
.getArtifactId()).append("</artifactId>\n");
94 p
.append("<version>").append(parent
.getVersion()).append("</version>\n");
95 p
.append("</parent>\n");
97 p
.append("<groupId>").append(pomArtifact
.getGroupId()).append("</groupId>\n");
98 p
.append("<artifactId>").append(pomArtifact
.getArtifactId()).append("</artifactId>\n");
99 p
.append("<version>").append(pomArtifact
.getVersion()).append("</version>\n");
100 p
.append("<packaging>pom</packaging>\n");
103 p
.append("<dependencies>\n");
104 for (Artifact a
: artifacts
) {
105 p
.append("\t<dependency>");
106 p
.append("<artifactId>").append(a
.getArtifactId()).append("</artifactId>");
107 p
.append("<groupId>").append(a
.getGroupId()).append("</groupId>");
108 if (!a
.getExtension().equals("jar"))
109 p
.append("<type>").append(a
.getExtension()).append("</type>");
110 p
.append("</dependency>\n");
112 p
.append("</dependencies>\n");
114 // Dependency management
115 p
.append("<dependencyManagement>\n");
116 p
.append("<dependencies>\n");
117 for (Artifact a
: artifacts
) {
118 p
.append("\t<dependency>");
119 p
.append("<artifactId>").append(a
.getArtifactId()).append("</artifactId>");
120 p
.append("<version>").append(a
.getVersion()).append("</version>");
121 p
.append("<groupId>").append(a
.getGroupId()).append("</groupId>");
122 if (a
.getExtension().equals("pom")) {
123 p
.append("<type>").append(a
.getExtension()).append("</type>");
124 p
.append("<scope>import</scope>");
126 p
.append("</dependency>\n");
128 p
.append("</dependencies>\n");
129 p
.append("</dependencyManagement>\n");
132 // p.append("<repositories>\n");
133 // p.append("<repository><id>argeo</id><url>http://maven.argeo.org/argeo</url></repository>\n");
134 // p.append("</repositories>\n");
136 p
.append("</project>\n");
141 // * Directly parses Maven POM XML format in order to find all artifacts
142 // * references under the dependency and dependencyManagement tags. This is
143 // * meant to migrate existing pom registering a lot of artifacts, not to
144 // * replace Maven resolving.
146 // public static void gatherPomDependencies(AetherTemplate aetherTemplate, Set<Artifact> artifacts,
147 // Artifact pomArtifact) {
148 // if (log.isDebugEnabled())
149 // log.debug("Gather dependencies for " + pomArtifact);
152 // File file = aetherTemplate.getResolvedFile(pomArtifact);
153 // DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
154 // Document doc = documentBuilder.parse(file);
157 // Properties props = new Properties();
158 // props.setProperty("project.version", pomArtifact.getBaseVersion());
159 // NodeList properties = doc.getElementsByTagName("properties");
160 // if (properties.getLength() > 0) {
161 // NodeList propertiesElems = properties.item(0).getChildNodes();
162 // for (int i = 0; i < propertiesElems.getLength(); i++) {
163 // if (propertiesElems.item(i) instanceof Element) {
164 // Element property = (Element) propertiesElems.item(i);
165 // props.put(property.getNodeName(), property.getTextContent());
170 // // dependencies (direct and dependencyManagement)
171 // NodeList dependencies = doc.getElementsByTagName("dependency");
172 // for (int i = 0; i < dependencies.getLength(); i++) {
173 // Element dependency = (Element) dependencies.item(i);
174 // String groupId = dependency.getElementsByTagName("groupId").item(0).getTextContent().trim();
175 // String artifactId = dependency.getElementsByTagName("artifactId").item(0).getTextContent().trim();
176 // String version = dependency.getElementsByTagName("version").item(0).getTextContent().trim();
177 // if (version.startsWith("${")) {
178 // String versionKey = version.substring(0, version.length() - 1).substring(2);
179 // if (!props.containsKey(versionKey))
180 // throw new SlcException("Cannot interpret version " + version);
181 // version = props.getProperty(versionKey);
183 // NodeList scopes = dependency.getElementsByTagName("scope");
184 // if (scopes.getLength() > 0 && scopes.item(0).getTextContent().equals("import")) {
186 // gatherPomDependencies(aetherTemplate, artifacts,
187 // new DefaultArtifact(groupId, artifactId, "pom", version));
189 // // TODO: deal with scope?
190 // // TODO: deal with type
191 // String type = "jar";
192 // Artifact artifact = new DefaultArtifact(groupId, artifactId, type, version);
193 // artifacts.add(artifact);
196 // } catch (Exception e) {
197 // throw new SlcException("Cannot process " + pomArtifact, e);
201 /** Prevent instantiation */
202 private MavenConventionsUtils() {