View Javadoc

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
33   * examples might be:
34   * </p>
35   * <ul>
36   * <li>@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@</li>
37   * <li>@{artifactId}@-@{version}@.@{extension}@</li>
38   * <li>@{artifactId}@.@{extension}@</li>
39   * </ul>
40   * <p>
41   * Although parts of this code comes from the Assembly Plugin, it cannot be
42   * shared with the Assembly Plugin. The reason for this is that the Assembly
43   * Plugin always uses a prefix for the expressions, whereas this code does not.
44   * <p/>
45   *
46   * @author Stephane Nicoll
47   * @author Dennis Lundberg
48   * @version $Id: MappingUtils.java 1512271 2013-08-09 12:03:31Z dennisl $
49   */
50  public class MappingUtils
51  {
52      public static final String DEFAULT_FILE_NAME_MAPPING = "@{artifactId}@-@{baseVersion}@.@{extension}@";
53      public static final String DEFAULT_FILE_NAME_MAPPING_CLASSIFIER =
54          "@{artifactId}@-@{baseVersion}@-@{classifier}@.@{extension}@";
55  
56      /**
57       * Evaluates the specified expression for the given artifact.
58       *
59       * @param expression the expression to evaluate
60       * @param artifact   the artifact to use as value object for tokens
61       * @return expression the evaluated expression
62       */
63      public static String evaluateFileNameMapping( String expression, Artifact artifact )
64          throws InterpolationException
65      {
66          String value = expression;
67  
68          // FIXME: This is BAD! Accessors SHOULD NOT change the behavior of the object.
69          // [dennisl; 2013-07-30] This was fixed in Maven 2.0.8
70          artifact.isSnapshot();
71  
72          RegexBasedInterpolator interpolator = new RegexBasedInterpolator( "\\@\\{(", ")?([^}]+)\\}@" );
73          interpolator.addValueSource( new ObjectBasedValueSource( artifact ) );
74          interpolator.addValueSource( new ObjectBasedValueSource( artifact.getArtifactHandler() ) );
75  
76          // Support for special expressions, like @{dashClassifier?}@, see MWAR-212
77          interpolator.addValueSource( new DashClassifierValueSource( artifact.getClassifier() ) );
78  
79          value = interpolator.interpolate( value, "__artifact" );
80  
81          return value;
82      }
83  }