]>
git.argeo.org Git - gpl/argeo-slc.git/blob - repo/maven/MavenConventionsUtils.java
2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.repo
.maven
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.eclipse
.aether
.artifact
.Artifact
;
26 * Static utilities around Maven which are NOT using the Maven APIs (conventions
29 public class MavenConventionsUtils
{
30 private final static Log log
= LogFactory
.getLog(MavenConventionsUtils
.class);
33 * Path to the file identified by this artifact <b>without</b> using Maven
34 * APIs (convention based). Default location of repository
35 * (~/.m2/repository) is used here.
37 * @see MavenConventionsUtils#artifactToFile(String, Artifact)
39 public static File
artifactToFile(Artifact artifact
) {
40 return artifactToFile(System
.getProperty("user.home") + File
.separator
+ ".m2" + File
.separator
+ "repository",
45 * Path to the file identified by this artifact <b>without</b> using Maven
46 * APIs (convention based).
48 * @param repositoryPath
49 * path to the related local repository location
53 public static File
artifactToFile(String repositoryPath
, Artifact artifact
) {
54 return new File(repositoryPath
+ File
.separator
+ artifact
.getGroupId().replace('.', File
.separatorChar
)
55 + File
.separator
+ artifact
.getArtifactId() + File
.separator
+ artifact
.getVersion() + File
.separator
56 + artifactFileName(artifact
)).getAbsoluteFile();
59 /** The file name of this artifact when stored */
60 public static String
artifactFileName(Artifact artifact
) {
61 return artifact
.getArtifactId() + '-' + artifact
.getVersion()
62 + (artifact
.getClassifier().equals("") ?
"" : '-' + artifact
.getClassifier()) + '.'
63 + artifact
.getExtension();
66 /** Absolute path to the file */
67 public static String
artifactPath(String artifactBasePath
, Artifact artifact
) {
68 return artifactParentPath(artifactBasePath
, artifact
) + '/' + artifactFileName(artifact
);
71 /** Absolute path to the file */
72 public static String
artifactUrl(String repoUrl
, Artifact artifact
) {
73 if (repoUrl
.endsWith("/"))
74 return repoUrl
+ artifactPath("/", artifact
).substring(1);
76 return repoUrl
+ artifactPath("/", artifact
);
79 /** Absolute path to the directories where the files will be stored */
80 public static String
artifactParentPath(String artifactBasePath
, Artifact artifact
) {
81 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + artifactParentPath(artifact
);
84 /** Absolute path to the directory of this group */
85 public static String
groupPath(String artifactBasePath
, String groupId
) {
86 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + groupId
.replace('.', '/');
89 /** Relative path to the directories where the files will be stored */
90 public static String
artifactParentPath(Artifact artifact
) {
91 return artifact
.getGroupId().replace('.', '/') + '/' + artifact
.getArtifactId() + '/'
92 + artifact
.getBaseVersion();
95 public static String
artifactsAsDependencyPom(Artifact pomArtifact
, Set
<Artifact
> artifacts
, Artifact parent
) {
96 StringBuffer p
= new StringBuffer();
99 p
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
101 "<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");
102 p
.append("<modelVersion>4.0.0</modelVersion>\n");
105 if (parent
!= null) {
106 p
.append("<parent>\n");
107 p
.append("<groupId>").append(parent
.getGroupId()).append("</groupId>\n");
108 p
.append("<artifactId>").append(parent
.getArtifactId()).append("</artifactId>\n");
109 p
.append("<version>").append(parent
.getVersion()).append("</version>\n");
110 p
.append("</parent>\n");
112 p
.append("<groupId>").append(pomArtifact
.getGroupId()).append("</groupId>\n");
113 p
.append("<artifactId>").append(pomArtifact
.getArtifactId()).append("</artifactId>\n");
114 p
.append("<version>").append(pomArtifact
.getVersion()).append("</version>\n");
115 p
.append("<packaging>pom</packaging>\n");
118 p
.append("<dependencies>\n");
119 for (Artifact a
: artifacts
) {
120 p
.append("\t<dependency>");
121 p
.append("<artifactId>").append(a
.getArtifactId()).append("</artifactId>");
122 p
.append("<groupId>").append(a
.getGroupId()).append("</groupId>");
123 if (!a
.getExtension().equals("jar"))
124 p
.append("<type>").append(a
.getExtension()).append("</type>");
125 p
.append("</dependency>\n");
127 p
.append("</dependencies>\n");
129 // Dependency management
130 p
.append("<dependencyManagement>\n");
131 p
.append("<dependencies>\n");
132 for (Artifact a
: artifacts
) {
133 p
.append("\t<dependency>");
134 p
.append("<artifactId>").append(a
.getArtifactId()).append("</artifactId>");
135 p
.append("<version>").append(a
.getVersion()).append("</version>");
136 p
.append("<groupId>").append(a
.getGroupId()).append("</groupId>");
137 if (a
.getExtension().equals("pom")) {
138 p
.append("<type>").append(a
.getExtension()).append("</type>");
139 p
.append("<scope>import</scope>");
141 p
.append("</dependency>\n");
143 p
.append("</dependencies>\n");
144 p
.append("</dependencyManagement>\n");
147 // p.append("<repositories>\n");
148 // p.append("<repository><id>argeo</id><url>http://maven.argeo.org/argeo</url></repository>\n");
149 // p.append("</repositories>\n");
151 p
.append("</project>\n");
156 // * Directly parses Maven POM XML format in order to find all artifacts
157 // * references under the dependency and dependencyManagement tags. This is
158 // * meant to migrate existing pom registering a lot of artifacts, not to
159 // * replace Maven resolving.
161 // public static void gatherPomDependencies(AetherTemplate aetherTemplate, Set<Artifact> artifacts,
162 // Artifact pomArtifact) {
163 // if (log.isDebugEnabled())
164 // log.debug("Gather dependencies for " + pomArtifact);
167 // File file = aetherTemplate.getResolvedFile(pomArtifact);
168 // DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
169 // Document doc = documentBuilder.parse(file);
172 // Properties props = new Properties();
173 // props.setProperty("project.version", pomArtifact.getBaseVersion());
174 // NodeList properties = doc.getElementsByTagName("properties");
175 // if (properties.getLength() > 0) {
176 // NodeList propertiesElems = properties.item(0).getChildNodes();
177 // for (int i = 0; i < propertiesElems.getLength(); i++) {
178 // if (propertiesElems.item(i) instanceof Element) {
179 // Element property = (Element) propertiesElems.item(i);
180 // props.put(property.getNodeName(), property.getTextContent());
185 // // dependencies (direct and dependencyManagement)
186 // NodeList dependencies = doc.getElementsByTagName("dependency");
187 // for (int i = 0; i < dependencies.getLength(); i++) {
188 // Element dependency = (Element) dependencies.item(i);
189 // String groupId = dependency.getElementsByTagName("groupId").item(0).getTextContent().trim();
190 // String artifactId = dependency.getElementsByTagName("artifactId").item(0).getTextContent().trim();
191 // String version = dependency.getElementsByTagName("version").item(0).getTextContent().trim();
192 // if (version.startsWith("${")) {
193 // String versionKey = version.substring(0, version.length() - 1).substring(2);
194 // if (!props.containsKey(versionKey))
195 // throw new SlcException("Cannot interpret version " + version);
196 // version = props.getProperty(versionKey);
198 // NodeList scopes = dependency.getElementsByTagName("scope");
199 // if (scopes.getLength() > 0 && scopes.item(0).getTextContent().equals("import")) {
201 // gatherPomDependencies(aetherTemplate, artifacts,
202 // new DefaultArtifact(groupId, artifactId, "pom", version));
204 // // TODO: deal with scope?
205 // // TODO: deal with type
206 // String type = "jar";
207 // Artifact artifact = new DefaultArtifact(groupId, artifactId, type, version);
208 // artifacts.add(artifact);
211 // } catch (Exception e) {
212 // throw new SlcException("Cannot process " + pomArtifact, e);
216 /** Prevent instantiation */
217 private MavenConventionsUtils() {