1 package org.apache.maven.shared.mapping;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.artifact.Artifact;
23 import org.codehaus.plexus.interpolation.InterpolationException;
24 import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
25 import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
26
27 /**
28 * <p>
29 * Utilities used to evaluate an expression.
30 * </p>
31 * <p>
32 * The expression might use any field of the {@link Artifact} interface. Some examples might be:
33 * </p>
34 * <ul>
35 * <li>@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@</li>
36 * <li>@{artifactId}@-@{version}@.@{extension}@</li>
37 * <li>@{artifactId}@.@{extension}@</li>
38 * </ul>
39 * <p>
40 * Although parts of this code comes from the Assembly Plugin, it cannot be shared with the Assembly Plugin. The reason
41 * for this is that the Assembly Plugin always uses a prefix for the expressions, whereas this code does not.
42 * <p/>
43 *
44 * @author Stephane Nicoll
45 * @author Dennis Lundberg
46 * @version $Id: MappingUtils.java 1714435 2015-11-15 11:55:21Z khmarbaise $
47 */
48 public final class MappingUtils
49 {
50 private MappingUtils()
51 {
52 // prevent instantiation.
53 }
54
55 /**
56 * Default file name mapping.
57 */
58 public static final String DEFAULT_FILE_NAME_MAPPING = "@{artifactId}@-@{baseVersion}@.@{extension}@";
59
60 /**
61 * Default file name mapping incl. classifier.
62 */
63 public static final String DEFAULT_FILE_NAME_MAPPING_CLASSIFIER =
64 "@{artifactId}@-@{baseVersion}@-@{classifier}@.@{extension}@";
65
66 /**
67 * Evaluates the specified expression for the given artifact.
68 *
69 * @param expression the expression to evaluate
70 * @param artifact the artifact to use as value object for tokens
71 * @throws InterpolationException In case of an error.
72 * @return expression the evaluated expression
73 */
74 public static String evaluateFileNameMapping( String expression, Artifact artifact )
75 throws InterpolationException
76 {
77 String value = expression;
78
79 RegexBasedInterpolator interpolator = new RegexBasedInterpolator( "\\@\\{(", ")?([^}]+)\\}@" );
80 interpolator.addValueSource( new ObjectBasedValueSource( artifact ) );
81 interpolator.addValueSource( new ObjectBasedValueSource( artifact.getArtifactHandler() ) );
82
83 // Support for special expressions, like @{dashClassifier?}@, see MWAR-212
84 interpolator.addValueSource( new DashClassifierValueSource( artifact.getClassifier() ) );
85
86 value = interpolator.interpolate( value, "__artifact" );
87
88 return value;
89 }
90 }