]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.factory/src/org/argeo/slc/factory/m2/MavenConventionsUtils.java
Add OSGi category
[gpl/argeo-slc.git] / org.argeo.slc.factory / src / org / argeo / slc / factory / m2 / MavenConventionsUtils.java
1 package org.argeo.slc.factory.m2;
2
3 import java.io.File;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.Set;
7
8 /**
9 * Static utilities around Maven which are NOT using the Maven APIs (conventions
10 * based).
11 */
12 public class MavenConventionsUtils {
13 public final static String MAVEN_CENTRAL_BASE_URL = "https://repo1.maven.org/maven2/";
14
15 /**
16 * Path to the file identified by this artifact <b>without</b> using Maven APIs
17 * (convention based). Default location of repository (~/.m2/repository) is used
18 * here.
19 *
20 * @see MavenConventionsUtils#artifactToFile(String, Artifact)
21 */
22 public static File artifactToFile(Artifact artifact) {
23 return artifactToFile(System.getProperty("user.home") + File.separator + ".m2" + File.separator + "repository",
24 artifact);
25 }
26
27 /**
28 * Path to the file identified by this artifact <b>without</b> using Maven APIs
29 * (convention based).
30 *
31 * @param repositoryPath path to the related local repository location
32 * @param artifact the artifact
33 */
34 public static File artifactToFile(String repositoryPath, Artifact artifact) {
35 return new File(repositoryPath + File.separator + artifact.getGroupId().replace('.', File.separatorChar)
36 + File.separator + artifact.getArtifactId() + File.separator + artifact.getVersion() + File.separator
37 + artifactFileName(artifact)).getAbsoluteFile();
38 }
39
40 /** The file name of this artifact when stored */
41 public static String artifactFileName(Artifact artifact) {
42 return artifact.getArtifactId() + '-' + artifact.getVersion()
43 + (artifact.getClassifier().equals("") ? "" : '-' + artifact.getClassifier()) + '.'
44 + artifact.getExtension();
45 }
46
47 /** Absolute path to the file */
48 public static String artifactPath(String artifactBasePath, Artifact artifact) {
49 return artifactParentPath(artifactBasePath, artifact) + '/' + artifactFileName(artifact);
50 }
51
52 /** Absolute path to the file */
53 public static String artifactUrl(String repoUrl, Artifact artifact) {
54 if (repoUrl.endsWith("/"))
55 return repoUrl + artifactPath("/", artifact).substring(1);
56 else
57 return repoUrl + artifactPath("/", artifact);
58 }
59
60 /** Absolute path to the file */
61 // public static URL mavenCentralUrl(Artifact artifact) {
62 // return mavenRepoUrl(MAVEN_CENTRAL_BASE_URL, artifact);
63 // }
64
65 /** Absolute path to the file */
66 public static URL mavenRepoUrl(String repoBase, Artifact artifact) {
67 String url = artifactUrl(repoBase == null ? MAVEN_CENTRAL_BASE_URL : repoBase, artifact);
68 try {
69 return new URL(url);
70 } catch (MalformedURLException e) {
71 // it should not happen
72 throw new IllegalStateException(e);
73 }
74 }
75
76 /** Absolute path to the directories where the files will be stored */
77 public static String artifactParentPath(String artifactBasePath, Artifact artifact) {
78 return artifactBasePath + (artifactBasePath.endsWith("/") ? "" : "/") + artifactParentPath(artifact);
79 }
80
81 /** Absolute path to the directory of this group */
82 public static String groupPath(String artifactBasePath, String groupId) {
83 return artifactBasePath + (artifactBasePath.endsWith("/") ? "" : "/") + groupId.replace('.', '/');
84 }
85
86 /** Relative path to the directories where the files will be stored */
87 public static String artifactParentPath(Artifact artifact) {
88 return artifact.getGroupId().replace('.', '/') + '/' + artifact.getArtifactId() + '/'
89 + artifact.getBaseVersion();
90 }
91
92 public static String artifactsAsDependencyPom(Artifact pomArtifact, Set<Artifact> artifacts, Artifact parent) {
93 StringBuffer p = new StringBuffer();
94
95 // XML header
96 p.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
97 p.append(
98 "<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");
99 p.append("<modelVersion>4.0.0</modelVersion>\n");
100
101 // Artifact
102 if (parent != null) {
103 p.append("<parent>\n");
104 p.append("<groupId>").append(parent.getGroupId()).append("</groupId>\n");
105 p.append("<artifactId>").append(parent.getArtifactId()).append("</artifactId>\n");
106 p.append("<version>").append(parent.getVersion()).append("</version>\n");
107 p.append("</parent>\n");
108 }
109 p.append("<groupId>").append(pomArtifact.getGroupId()).append("</groupId>\n");
110 p.append("<artifactId>").append(pomArtifact.getArtifactId()).append("</artifactId>\n");
111 p.append("<version>").append(pomArtifact.getVersion()).append("</version>\n");
112 p.append("<packaging>pom</packaging>\n");
113
114 // Dependencies
115 p.append("<dependencies>\n");
116 for (Artifact a : artifacts) {
117 p.append("\t<dependency>");
118 p.append("<artifactId>").append(a.getArtifactId()).append("</artifactId>");
119 p.append("<groupId>").append(a.getGroupId()).append("</groupId>");
120 if (!a.getExtension().equals("jar"))
121 p.append("<type>").append(a.getExtension()).append("</type>");
122 p.append("</dependency>\n");
123 }
124 p.append("</dependencies>\n");
125
126 // Dependency management
127 p.append("<dependencyManagement>\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("<version>").append(a.getVersion()).append("</version>");
133 p.append("<groupId>").append(a.getGroupId()).append("</groupId>");
134 if (a.getExtension().equals("pom")) {
135 p.append("<type>").append(a.getExtension()).append("</type>");
136 p.append("<scope>import</scope>");
137 }
138 p.append("</dependency>\n");
139 }
140 p.append("</dependencies>\n");
141 p.append("</dependencyManagement>\n");
142
143 // Repositories
144 // p.append("<repositories>\n");
145 // p.append("<repository><id>argeo</id><url>http://maven.argeo.org/argeo</url></repository>\n");
146 // p.append("</repositories>\n");
147
148 p.append("</project>\n");
149 return p.toString();
150 }
151
152 /** Singleton */
153 private MavenConventionsUtils() {
154 }
155 }