Add .tox/ to .gitignore
[odlparent.git] / karaf / karaf-maven-plugin / src / main / java / org / apache / karaf / tooling / utils / MavenUtil.java
1 /*
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
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  * under the License.
18  */
19 package org.apache.karaf.tooling.utils;
20
21 import java.io.File;
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;
28
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;
36
37 /**
38  * Util method for Maven manipulation (URL convert, metadata generation, etc).
39  */
40 public class MavenUtil {
41
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:([^/ ]+)/([^/ ]+)/([^/\\$ ]*)(/([^/\\$ ]+)(/([^/\\$ ]+))?)?(/\\$.+)?");
45
46     /**
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.
50      *
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>
53      */
54     public static String mvnToAether(String name) {
55         Matcher m = mvnPattern.matcher(name);
56         if (!m.matches()) {
57             return name;
58         }
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(":");
67             } else {
68                 b.append("jar:");
69             }
70             b.append(classifier).append(":");
71         } else {
72             if (present(extension) && !"jar".equals(extension)) {
73                 b.append(extension).append(":");
74             }
75         }
76         b.append(m.group(3));
77         return b.toString();
78     }
79
80     private static boolean present(String part) {
81         return part != null && !part.isEmpty();
82     }
83
84     /**
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.
88      *
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 ] ] ] ]
91      */
92     public static String aetherToMvn(String name) {
93         Matcher m = aetherPattern.matcher(name);
94         if (!m.matches()) {
95             return name;
96         }
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);
106             } else {
107                 b.append("/jar");
108             }
109             b.append("/").append(classifier);
110         } else if (present(extension)) {
111             b.append("/").append(extension);
112         }
113
114         return b.toString();
115     }
116
117     public static boolean isEmpty(String classifier) {
118         return classifier == null || classifier.length() == 0;
119     }
120
121     /**
122      * Generate the maven-metadata-local.xml for the given Maven <code>Artifact</code>.
123      *
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.
127      */
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");
135
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);
147
148         metadata.setVersioning(versioning);
149
150         MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer();
151         Writer writer = new FileWriter(target);
152         metadataWriter.write(writer, metadata);
153     }
154     
155     public static String getFileName(Artifact artifact) {
156         String name = artifact.getArtifactId() + "-" + artifact.getBaseVersion()
157             + (artifact.getClassifier() != null ? "-" + artifact.getClassifier() : "") + "." + artifact.getType();
158         return name;
159     }
160     
161     public static String getDir(Artifact artifact) {
162         return artifact.getGroupId().replace('.', '/') + "/" + artifact.getArtifactId() + "/" + artifact.getBaseVersion() + "/";
163     }
164
165 }