View Javadoc
1   package org.apache.maven.plugins.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.plugins.assembly.AssemblerConfigurationSource;
23  import org.apache.maven.plugins.assembly.model.io.xpp3.AssemblyXpp3Reader;
24  import org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Reader;
25  import org.apache.maven.plugins.assembly.resolved.AssemblyId;
26  import org.apache.maven.plugins.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.html 1016737 2017-08-13 12:01:54Z khmarbaise $
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          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              @Override
74              public String transform( String source, String contextDescription )
75              {
76                  if ( blacklistFields.contains( contextDescription ) )
77                  {
78                      return source;
79                  }
80  
81                  String interpolated = interpolator.interpolate( source, is );
82                  if ( !source.equals( interpolated ) && logger.isDebugEnabled() )
83                  {
84                      logger.debug(
85                          "Field " + contextDescription + " source: " + source + " interpolated to: " + interpolated );
86                  }
87                  return interpolated;
88              }
89          };
90      }
91  
92      public static ComponentXpp3Reader.ContentTransformer componentInterpolator(
93          final FixedStringSearchInterpolator interpolator, final InterpolationState is, final Logger logger )
94      {
95          final Set<String> blacklistFields =
96              new HashSet<String>( FieldBasedObjectInterpolator.DEFAULT_BLACKLISTED_FIELD_NAMES );
97          blacklistFields.addAll( INTERPOLATION_BLACKLIST );
98  
99          return new ComponentXpp3Reader.ContentTransformer()
100         {
101             @Override
102             public String transform( String source, String contextDescription )
103             {
104                 if ( blacklistFields.contains( contextDescription ) )
105                 {
106                     return source;
107                 }
108 
109                 String interpolated = interpolator.interpolate( source, is );
110                 if ( !source.equals( interpolated ) )
111                 {
112                     logger.debug(
113                         "Field " + contextDescription + " source: " + source + " interpolated to: " + interpolated );
114                 }
115                 return interpolated;
116             }
117         };
118     }
119 
120 
121     public static void checkErrors( AssemblyId assemblyId, InterpolationState interpolationState, Logger logger )
122     {
123         if ( interpolationState.asList() != null && interpolationState.asList().size() > 0 && logger.isDebugEnabled() )
124         {
125             final StringBuilder sb = new StringBuilder();
126 
127             sb.append( "One or more minor errors occurred while interpolating the assembly with ID: " ).append(
128                 assemblyId ).append( ":\n" );
129 
130             @SuppressWarnings( "unchecked" ) final List<Object> warnings = interpolationState.asList();
131             for ( final Object warning : warnings )
132             {
133                 sb.append( '\n' ).append( warning );
134             }
135 
136             sb.append( "\n\nThese values were SKIPPED, but the assembly process will continue.\n" );
137 
138             logger.debug( sb.toString() );
139         }
140     }
141 
142     public static FixedStringSearchInterpolator fullInterpolator( final MavenProject project,
143                                                                   @Nonnull FixedStringSearchInterpolator projectIp,
144                                                                   final AssemblerConfigurationSource configSource )
145     {
146         FixedStringSearchInterpolator fixedStringSearchInterpolator =
147             FixedStringSearchInterpolator.create( configSource.getRepositoryInterpolator(),
148                                                   configSource.getCommandLinePropsInterpolator(),
149                                                   configSource.getEnvInterpolator(), projectIp );
150         return fixedStringSearchInterpolator.withPostProcessor(
151             new PathTranslatingPostProcessor( project.getBasedir() ) );
152 
153     }
154 
155     private static final class PathTranslatingPostProcessor
156         implements InterpolationPostProcessor
157     {
158 
159         private final File basedir;
160 
161         public PathTranslatingPostProcessor( final File basedir )
162         {
163             this.basedir = basedir;
164         }
165 
166         @Override
167         public Object execute( final String expression, final Object value )
168         {
169             final String path = String.valueOf( value );
170             return AssemblyFileUtils.makePathRelativeTo( path, basedir );
171         }
172 
173     }
174 }