View Javadoc
1   package org.apache.maven.plugin.assembly.interpolation;
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.plugin.assembly.AssemblerConfigurationSource;
23  import org.apache.maven.plugin.assembly.model.io.xpp3.AssemblyXpp3Reader;
24  import org.apache.maven.plugin.assembly.model.io.xpp3.ComponentXpp3Reader;
25  import org.apache.maven.plugin.assembly.resolved.AssemblyId;
26  import org.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
29  import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
30  import org.codehaus.plexus.interpolation.fixed.InterpolationState;
31  import org.codehaus.plexus.interpolation.object.FieldBasedObjectInterpolator;
32  import org.codehaus.plexus.logging.Logger;
33  
34  import javax.annotation.Nonnull;
35  import java.io.File;
36  import java.io.IOException;
37  import java.util.HashSet;
38  import java.util.List;
39  import java.util.Set;
40  
41  /**
42   * @version $Id: AssemblyInterpolator.java 1644977 2014-12-12 16:56:16Z krosenvold $
43   */
44  public class AssemblyInterpolator
45  {
46      private static final Set<String> INTERPOLATION_BLACKLIST;
47  
48      static
49      {
50          final Set<String> blacklist = new HashSet<String>();
51  
52          blacklist.add( "outputFileNameMapping" );
53          blacklist.add( "outputDirectoryMapping" );
54          blacklist.add( "outputDirectory" );
55  
56          INTERPOLATION_BLACKLIST = blacklist;
57      }
58  
59      public AssemblyInterpolator()
60          throws IOException
61      {
62      }
63  
64      public static AssemblyXpp3Reader.ContentTransformer assemblyInterpolator(
65          final FixedStringSearchInterpolator interpolator, final InterpolationState is, final Logger logger )
66      {
67          @SuppressWarnings( "unchecked" ) final Set<String> blacklistFields =
68              new HashSet<String>( FieldBasedObjectInterpolator.DEFAULT_BLACKLISTED_FIELD_NAMES );
69          blacklistFields.addAll( INTERPOLATION_BLACKLIST );
70  
71          return new AssemblyXpp3Reader.ContentTransformer()
72          {
73              public String transform( String source, String contextDescription )
74              {
75                  if ( blacklistFields.contains( contextDescription ) )
76                  {
77                      return source;
78                  }
79  
80                  String interpolated = interpolator.interpolate( source, is );
81                  if ( !source.equals( interpolated ) && logger.isDebugEnabled() )
82                  {
83                      logger.debug(
84                          "Field " + contextDescription + " source: " + source + " interpolated to: " + interpolated );
85                  }
86                  return interpolated;
87              }
88          };
89      }
90  
91      public static ComponentXpp3Reader.ContentTransformer componentInterpolator(
92          final FixedStringSearchInterpolator interpolator, final InterpolationState is, final Logger logger )
93      {
94          @SuppressWarnings( "unchecked" ) final Set<String> blacklistFields =
95              new HashSet<String>( FieldBasedObjectInterpolator.DEFAULT_BLACKLISTED_FIELD_NAMES );
96          blacklistFields.addAll( INTERPOLATION_BLACKLIST );
97  
98          return new ComponentXpp3Reader.ContentTransformer()
99          {
100             public String transform( String source, String contextDescription )
101             {
102                 if ( blacklistFields.contains( contextDescription ) )
103                 {
104                     return source;
105                 }
106 
107                 String interpolated = interpolator.interpolate( source, is );
108                 if ( !source.equals( interpolated ) )
109                 {
110                     logger.debug(
111                         "Field " + contextDescription + " source: " + source + " interpolated to: " + interpolated );
112                 }
113                 return interpolated;
114             }
115         };
116     }
117 
118 
119     public static void checkErrors( AssemblyId assemblyId, InterpolationState interpolationState, Logger logger )
120     {
121         if ( interpolationState.asList() != null && interpolationState.asList().size() > 0 && logger.isDebugEnabled() )
122         {
123             final StringBuilder sb = new StringBuilder();
124 
125             sb.append( "One or more minor errors occurred while interpolating the assembly with ID: " ).append(
126                 assemblyId ).append( ":\n" );
127 
128             @SuppressWarnings( "unchecked" ) final List<Object> warnings = interpolationState.asList();
129             for ( final Object warning : warnings )
130             {
131                 sb.append( '\n' ).append( warning );
132             }
133 
134             sb.append( "\n\nThese values were SKIPPED, but the assembly process will continue.\n" );
135 
136             logger.debug( sb.toString() );
137         }
138     }
139 
140     public static FixedStringSearchInterpolator fullInterpolator( final MavenProject project,
141                                                                   @Nonnull FixedStringSearchInterpolator projectIp,
142                                                                   final AssemblerConfigurationSource configSource )
143     {
144         FixedStringSearchInterpolator fixedStringSearchInterpolator =
145             FixedStringSearchInterpolator.create( configSource.getRepositoryInterpolator(),
146                                                   configSource.getCommandLinePropsInterpolator(),
147                                                   configSource.getEnvInterpolator(), projectIp );
148         return fixedStringSearchInterpolator.withPostProcessor(
149             new PathTranslatingPostProcessor( project.getBasedir() ) );
150 
151     }
152 
153     private static final class PathTranslatingPostProcessor
154         implements InterpolationPostProcessor
155     {
156 
157         private final File basedir;
158 
159         public PathTranslatingPostProcessor( final File basedir )
160         {
161             this.basedir = basedir;
162         }
163 
164         public Object execute( final String expression, final Object value )
165         {
166             final String path = String.valueOf( value );
167             return AssemblyFileUtils.makePathRelativeTo( path, basedir );
168         }
169 
170     }
171 }