]>
git.argeo.org Git - gpl/argeo-jcr.git/blob - org.argeo.slc.repo/src/org/argeo/slc/repo/maven/MavenConventionsUtils.java
1 package org
.argeo
.slc
.repo
.maven
;
6 import org
.argeo
.api
.cms
.CmsLog
;
7 import org
.eclipse
.aether
.artifact
.Artifact
;
10 * Static utilities around Maven which are NOT using the Maven APIs (conventions
13 public class MavenConventionsUtils
{
14 private final static CmsLog log
= CmsLog
.getLog(MavenConventionsUtils
.class);
17 * Path to the file identified by this artifact <b>without</b> using Maven
18 * APIs (convention based). Default location of repository
19 * (~/.m2/repository) is used here.
21 * @see MavenConventionsUtils#artifactToFile(String, Artifact)
23 public static File
artifactToFile(Artifact artifact
) {
24 return artifactToFile(System
.getProperty("user.home") + File
.separator
+ ".m2" + File
.separator
+ "repository",
29 * Path to the file identified by this artifact <b>without</b> using Maven
30 * APIs (convention based).
32 * @param repositoryPath
33 * path to the related local repository location
37 public static File
artifactToFile(String repositoryPath
, Artifact artifact
) {
38 return new File(repositoryPath
+ File
.separator
+ artifact
.getGroupId().replace('.', File
.separatorChar
)
39 + File
.separator
+ artifact
.getArtifactId() + File
.separator
+ artifact
.getVersion() + File
.separator
40 + artifactFileName(artifact
)).getAbsoluteFile();
43 /** The file name of this artifact when stored */
44 public static String
artifactFileName(Artifact artifact
) {
45 return artifact
.getArtifactId() + '-' + artifact
.getVersion()
46 + (artifact
.getClassifier().equals("") ?
"" : '-' + artifact
.getClassifier()) + '.'
47 + artifact
.getExtension();
50 /** Absolute path to the file */
51 public static String
artifactPath(String artifactBasePath
, Artifact artifact
) {
52 return artifactParentPath(artifactBasePath
, artifact
) + '/' + artifactFileName(artifact
);
55 /** Absolute path to the file */
56 public static String
artifactUrl(String repoUrl
, Artifact artifact
) {
57 if (repoUrl
.endsWith("/"))
58 return repoUrl
+ artifactPath("/", artifact
).substring(1);
60 return repoUrl
+ artifactPath("/", artifact
);
63 /** Absolute path to the directories where the files will be stored */
64 public static String
artifactParentPath(String artifactBasePath
, Artifact artifact
) {
65 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + artifactParentPath(artifact
);
68 /** Absolute path to the directory of this group */
69 public static String
groupPath(String artifactBasePath
, String groupId
) {
70 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + groupId
.replace('.', '/');
73 /** Relative path to the directories where the files will be stored */
74 public static String
artifactParentPath(Artifact artifact
) {
75 return artifact
.getGroupId().replace('.', '/') + '/' + artifact
.getArtifactId() + '/'
76 + artifact
.getBaseVersion();
79 public static String
artifactsAsDependencyPom(Artifact pomArtifact
, Set
<Artifact
> artifacts
, Artifact parent
) {
80 StringBuffer p
= new StringBuffer();
83 p
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
85 "<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 p
.append("<modelVersion>4.0.0</modelVersion>\n");
90 p
.append("<parent>\n");
91 p
.append("<groupId>").append(parent
.getGroupId()).append("</groupId>\n");
92 p
.append("<artifactId>").append(parent
.getArtifactId()).append("</artifactId>\n");
93 p
.append("<version>").append(parent
.getVersion()).append("</version>\n");
94 p
.append("</parent>\n");
96 p
.append("<groupId>").append(pomArtifact
.getGroupId()).append("</groupId>\n");
97 p
.append("<artifactId>").append(pomArtifact
.getArtifactId()).append("</artifactId>\n");
98 p
.append("<version>").append(pomArtifact
.getVersion()).append("</version>\n");
99 p
.append("<packaging>pom</packaging>\n");
102 p
.append("<dependencies>\n");
103 for (Artifact a
: artifacts
) {
104 p
.append("\t<dependency>");
105 p
.append("<artifactId>").append(a
.getArtifactId()).append("</artifactId>");
106 p
.append("<groupId>").append(a
.getGroupId()).append("</groupId>");
107 if (!a
.getExtension().equals("jar"))
108 p
.append("<type>").append(a
.getExtension()).append("</type>");
109 p
.append("</dependency>\n");
111 p
.append("</dependencies>\n");
113 // Dependency management
114 p
.append("<dependencyManagement>\n");
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("<version>").append(a
.getVersion()).append("</version>");
120 p
.append("<groupId>").append(a
.getGroupId()).append("</groupId>");
121 if (a
.getExtension().equals("pom")) {
122 p
.append("<type>").append(a
.getExtension()).append("</type>");
123 p
.append("<scope>import</scope>");
125 p
.append("</dependency>\n");
127 p
.append("</dependencies>\n");
128 p
.append("</dependencyManagement>\n");
131 // p.append("<repositories>\n");
132 // p.append("<repository><id>argeo</id><url>http://maven.argeo.org/argeo</url></repository>\n");
133 // p.append("</repositories>\n");
135 p
.append("</project>\n");
140 // * Directly parses Maven POM XML format in order to find all artifacts
141 // * references under the dependency and dependencyManagement tags. This is
142 // * meant to migrate existing pom registering a lot of artifacts, not to
143 // * replace Maven resolving.
145 // public static void gatherPomDependencies(AetherTemplate aetherTemplate, Set<Artifact> artifacts,
146 // Artifact pomArtifact) {
147 // if (log.isDebugEnabled())
148 // log.debug("Gather dependencies for " + pomArtifact);
151 // File file = aetherTemplate.getResolvedFile(pomArtifact);
152 // DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
153 // Document doc = documentBuilder.parse(file);
156 // Properties props = new Properties();
157 // props.setProperty("project.version", pomArtifact.getBaseVersion());
158 // NodeList properties = doc.getElementsByTagName("properties");
159 // if (properties.getLength() > 0) {
160 // NodeList propertiesElems = properties.item(0).getChildNodes();
161 // for (int i = 0; i < propertiesElems.getLength(); i++) {
162 // if (propertiesElems.item(i) instanceof Element) {
163 // Element property = (Element) propertiesElems.item(i);
164 // props.put(property.getNodeName(), property.getTextContent());
169 // // dependencies (direct and dependencyManagement)
170 // NodeList dependencies = doc.getElementsByTagName("dependency");
171 // for (int i = 0; i < dependencies.getLength(); i++) {
172 // Element dependency = (Element) dependencies.item(i);
173 // String groupId = dependency.getElementsByTagName("groupId").item(0).getTextContent().trim();
174 // String artifactId = dependency.getElementsByTagName("artifactId").item(0).getTextContent().trim();
175 // String version = dependency.getElementsByTagName("version").item(0).getTextContent().trim();
176 // if (version.startsWith("${")) {
177 // String versionKey = version.substring(0, version.length() - 1).substring(2);
178 // if (!props.containsKey(versionKey))
179 // throw new SlcException("Cannot interpret version " + version);
180 // version = props.getProperty(versionKey);
182 // NodeList scopes = dependency.getElementsByTagName("scope");
183 // if (scopes.getLength() > 0 && scopes.item(0).getTextContent().equals("import")) {
185 // gatherPomDependencies(aetherTemplate, artifacts,
186 // new DefaultArtifact(groupId, artifactId, "pom", version));
188 // // TODO: deal with scope?
189 // // TODO: deal with type
190 // String type = "jar";
191 // Artifact artifact = new DefaultArtifact(groupId, artifactId, type, version);
192 // artifacts.add(artifact);
195 // } catch (Exception e) {
196 // throw new SlcException("Cannot process " + pomArtifact, e);
200 /** Prevent instantiation */
201 private MavenConventionsUtils() {