View Javadoc
1   package org.apache.maven.plugin.ear.output;
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  
24  /**
25   * A base class used to generate the standard name of an artifact instead of relying on the (potentially) wrong file
26   * name provided by {@link org.apache.maven.artifact.Artifact#getFile()}.
27   * 
28   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
29   */
30  public abstract class AbstractFileNameMapping
31      implements FileNameMapping
32  {
33  
34      private boolean useBaseVersion = false;
35  
36      /** {@inheritDoc} */
37      public final void setUseBaseVersion( boolean useBaseVersion )
38      {
39          this.useBaseVersion = useBaseVersion;
40      }
41  
42      /**
43       * Generates a standard file name for the specified {@link Artifact}.
44       * <p/>
45       * Returns something like <tt>artifactId-version[-classifier].extension</tt> if <tt>addVersion</tt> is true.
46       * Otherwise it generates something like <tt>artifactId[-classifier].extension</tt>
47       * 
48       * @param a the artifact to generate a filename from
49       * @param addVersion whether the version should be added
50       * @return the filename, with a standard format
51       */
52      protected String generateFileName( final Artifact a, boolean addVersion )
53      {
54          final String extension = a.getArtifactHandler().getExtension();
55  
56          final StringBuilder buffer = new StringBuilder( 128 );
57          buffer.append( a.getArtifactId() );
58          if ( addVersion )
59          {
60              if ( useBaseVersion )
61              {
62                  buffer.append( '-' ).append( a.getBaseVersion() );
63              }
64              else
65              {
66                  buffer.append( '-' ).append( a.getVersion() );
67              }
68          }
69          if ( a.hasClassifier() )
70          {
71              buffer.append( '-' ).append( a.getClassifier() );
72          }
73          if ( extension != null && extension.length() > 0 )
74          {
75              buffer.append( '.' ).append( extension );
76          }
77  
78          return buffer.toString();
79      }
80  }