2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
19 package org.apache.karaf.tooling.utils;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.Date;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
31 import org.apache.maven.artifact.repository.metadata.Metadata;
32 import org.apache.maven.artifact.repository.metadata.Snapshot;
33 import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
34 import org.apache.maven.artifact.repository.metadata.Versioning;
35 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
38 * Util method for Maven manipulation (URL convert, metadata generation, etc).
40 public class MavenUtil {
42 static final DefaultRepositoryLayout layout = new DefaultRepositoryLayout();
43 private static final Pattern aetherPattern = Pattern.compile("([^: ]+):([^: ]+)(:([^: ]*)(:([^: ]+))?)?:([^: ]+)");
44 private static final Pattern mvnPattern = Pattern.compile("(?:(?:wrap:)|(?:blueprint:))?mvn:([^/ ]+)/([^/ ]+)/([^/\\$ ]*)(/([^/\\$ ]+)(/([^/\\$ ]+))?)?(/\\$.+)?");
47 * Convert PAX URL mvn format to aether coordinate format.
48 * N.B. we do not handle repository-url in mvn urls.
49 * N.B. version is required in mvn urls.
51 * @param name PAX URL mvn format: mvn-uri := [ 'wrap:' ] 'mvn:' [ repository-url '!' ] group-id '/' artifact-id [ '/' [version] [ '/' [type] [ '/' classifier ] ] ] ]
52 * @return aether coordinate format: <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>
54 public static String mvnToAether(String name) {
55 Matcher m = mvnPattern.matcher(name);
59 StringBuilder b = new StringBuilder();
60 b.append(m.group(1)).append(":");//groupId
61 b.append(m.group(2)).append(":");//artifactId
62 String extension = m.group(5);
63 String classifier = m.group(7);
64 if (present(classifier)) {
65 if (present(extension)) {
66 b.append(extension).append(":");
70 b.append(classifier).append(":");
72 if (present(extension) && !"jar".equals(extension)) {
73 b.append(extension).append(":");
80 private static boolean present(String part) {
81 return part != null && !part.isEmpty();
85 * Convert Aether coordinate format to PAX mvn format.
86 * N.B. we do not handle repository-url in mvn urls.
87 * N.B. version is required in mvn urls.
89 * @param name aether coordinate format: <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>
90 * @return PAX URL mvn format: mvn-uri := 'mvn:' [ repository-url '!' ] group-id '/' artifact-id [ '/' [version] [ '/' [type] [ '/' classifier ] ] ] ]
92 public static String aetherToMvn(String name) {
93 Matcher m = aetherPattern.matcher(name);
97 StringBuilder b = new StringBuilder("mvn:");
98 b.append(m.group(1)).append("/");//groupId
99 b.append(m.group(2)).append("/");//artifactId
100 b.append(m.group(7));//version
101 String extension = m.group(4);
102 String classifier = m.group(6);
103 if (present(classifier)) {
104 if (present(extension)) {
105 b.append("/").append(extension);
109 b.append("/").append(classifier);
110 } else if (present(extension)) {
111 b.append("/").append(extension);
117 public static boolean isEmpty(String classifier) {
118 return classifier == null || classifier.length() == 0;
122 * Generate the maven-metadata-local.xml for the given Maven <code>Artifact</code>.
124 * @param artifact the Maven <code>Artifact</code>.
125 * @param target the target maven-metadata-local.xml file to generate.
126 * @throws IOException if the maven-metadata-local.xml can't be generated.
128 public static void generateMavenMetadata(Artifact artifact, File target) throws IOException {
129 target.getParentFile().mkdirs();
130 Metadata metadata = new Metadata();
131 metadata.setGroupId(artifact.getGroupId());
132 metadata.setArtifactId(artifact.getArtifactId());
133 metadata.setVersion(artifact.getVersion());
134 metadata.setModelVersion("1.1.0");
136 Versioning versioning = new Versioning();
137 versioning.setLastUpdatedTimestamp(new Date(System.currentTimeMillis()));
138 Snapshot snapshot = new Snapshot();
139 snapshot.setLocalCopy(true);
140 versioning.setSnapshot(snapshot);
141 SnapshotVersion snapshotVersion = new SnapshotVersion();
142 snapshotVersion.setClassifier(artifact.getClassifier());
143 snapshotVersion.setVersion(artifact.getVersion());
144 snapshotVersion.setExtension(artifact.getType());
145 snapshotVersion.setUpdated(versioning.getLastUpdated());
146 versioning.addSnapshotVersion(snapshotVersion);
148 metadata.setVersioning(versioning);
150 MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer();
151 Writer writer = new FileWriter(target);
152 metadataWriter.write(writer, metadata);
155 public static String getFileName(Artifact artifact) {
156 String name = artifact.getArtifactId() + "-" + artifact.getBaseVersion()
157 + (artifact.getClassifier() != null ? "-" + artifact.getClassifier() : "") + "." + artifact.getType();
161 public static String getDir(Artifact artifact) {
162 return artifact.getGroupId().replace('.', '/') + "/" + artifact.getArtifactId() + "/" + artifact.getBaseVersion() + "/";