1 package org
.argeo
.slc
.factory
.m2
;
4 import java
.net
.MalformedURLException
;
9 * Static utilities around Maven which are NOT using the Maven APIs (conventions
12 public class MavenConventionsUtils
{
13 public final static String MAVEN_CENTRAL_BASE_URL
= "https://repo1.maven.org/maven2/";
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
20 * @see MavenConventionsUtils#artifactToFile(String, Artifact)
22 public static File
artifactToFile(Artifact artifact
) {
23 return artifactToFile(System
.getProperty("user.home") + File
.separator
+ ".m2" + File
.separator
+ "repository",
28 * Path to the file identified by this artifact <b>without</b> using Maven APIs
31 * @param repositoryPath path to the related local repository location
32 * @param artifact the artifact
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();
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();
47 /** Absolute path to the file */
48 public static String
artifactPath(String artifactBasePath
, Artifact artifact
) {
49 return artifactParentPath(artifactBasePath
, artifact
) + '/' + artifactFileName(artifact
);
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);
57 return repoUrl
+ artifactPath("/", artifact
);
60 /** Absolute path to the file */
61 public static URL
mavenCentralUrl(Artifact artifact
) {
62 String url
= artifactUrl(MAVEN_CENTRAL_BASE_URL
, artifact
);
65 } catch (MalformedURLException e
) {
66 // it should not happen
67 throw new IllegalStateException(e
);
71 /** Absolute path to the directories where the files will be stored */
72 public static String
artifactParentPath(String artifactBasePath
, Artifact artifact
) {
73 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + artifactParentPath(artifact
);
76 /** Absolute path to the directory of this group */
77 public static String
groupPath(String artifactBasePath
, String groupId
) {
78 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + groupId
.replace('.', '/');
81 /** Relative path to the directories where the files will be stored */
82 public static String
artifactParentPath(Artifact artifact
) {
83 return artifact
.getGroupId().replace('.', '/') + '/' + artifact
.getArtifactId() + '/'
84 + artifact
.getBaseVersion();
87 public static String
artifactsAsDependencyPom(Artifact pomArtifact
, Set
<Artifact
> artifacts
, Artifact parent
) {
88 StringBuffer p
= new StringBuffer();
91 p
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
93 "<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");
94 p
.append("<modelVersion>4.0.0</modelVersion>\n");
98 p
.append("<parent>\n");
99 p
.append("<groupId>").append(parent
.getGroupId()).append("</groupId>\n");
100 p
.append("<artifactId>").append(parent
.getArtifactId()).append("</artifactId>\n");
101 p
.append("<version>").append(parent
.getVersion()).append("</version>\n");
102 p
.append("</parent>\n");
104 p
.append("<groupId>").append(pomArtifact
.getGroupId()).append("</groupId>\n");
105 p
.append("<artifactId>").append(pomArtifact
.getArtifactId()).append("</artifactId>\n");
106 p
.append("<version>").append(pomArtifact
.getVersion()).append("</version>\n");
107 p
.append("<packaging>pom</packaging>\n");
110 p
.append("<dependencies>\n");
111 for (Artifact a
: artifacts
) {
112 p
.append("\t<dependency>");
113 p
.append("<artifactId>").append(a
.getArtifactId()).append("</artifactId>");
114 p
.append("<groupId>").append(a
.getGroupId()).append("</groupId>");
115 if (!a
.getExtension().equals("jar"))
116 p
.append("<type>").append(a
.getExtension()).append("</type>");
117 p
.append("</dependency>\n");
119 p
.append("</dependencies>\n");
121 // Dependency management
122 p
.append("<dependencyManagement>\n");
123 p
.append("<dependencies>\n");
124 for (Artifact a
: artifacts
) {
125 p
.append("\t<dependency>");
126 p
.append("<artifactId>").append(a
.getArtifactId()).append("</artifactId>");
127 p
.append("<version>").append(a
.getVersion()).append("</version>");
128 p
.append("<groupId>").append(a
.getGroupId()).append("</groupId>");
129 if (a
.getExtension().equals("pom")) {
130 p
.append("<type>").append(a
.getExtension()).append("</type>");
131 p
.append("<scope>import</scope>");
133 p
.append("</dependency>\n");
135 p
.append("</dependencies>\n");
136 p
.append("</dependencyManagement>\n");
139 // p.append("<repositories>\n");
140 // p.append("<repository><id>argeo</id><url>http://maven.argeo.org/argeo</url></repository>\n");
141 // p.append("</repositories>\n");
143 p
.append("</project>\n");
148 private MavenConventionsUtils() {