View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.interpolation;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
28  import org.apache.maven.plugins.assembly.model.io.xpp3.AssemblyXpp3Reader;
29  import org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Reader;
30  import org.apache.maven.plugins.assembly.resolved.AssemblyId;
31  import org.apache.maven.plugins.assembly.utils.AssemblyFileUtils;
32  import org.apache.maven.project.MavenProject;
33  import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
34  import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
35  import org.codehaus.plexus.interpolation.fixed.InterpolationState;
36  import org.codehaus.plexus.interpolation.object.FieldBasedObjectInterpolator;
37  import org.slf4j.Logger;
38  
39  /**
40   *
41   */
42  public class AssemblyInterpolator {
43      private static final Set<String> INTERPOLATION_BLACKLIST;
44  
45      static {
46          final Set<String> blacklist = new HashSet<>();
47  
48          blacklist.add("outputFileNameMapping");
49          blacklist.add("outputDirectoryMapping");
50          blacklist.add("outputDirectory");
51  
52          INTERPOLATION_BLACKLIST = blacklist;
53      }
54  
55      public AssemblyInterpolator() throws IOException {}
56  
57      public static AssemblyXpp3Reader.ContentTransformer assemblyInterpolator(
58              final FixedStringSearchInterpolator interpolator, final InterpolationState is, final Logger logger) {
59          final Set<String> blacklistFields = new HashSet<>(FieldBasedObjectInterpolator.DEFAULT_BLACKLISTED_FIELD_NAMES);
60          blacklistFields.addAll(INTERPOLATION_BLACKLIST);
61  
62          return new AssemblyXpp3Reader.ContentTransformer() {
63              @Override
64              public String transform(String source, String contextDescription) {
65                  if (blacklistFields.contains(contextDescription)) {
66                      return source;
67                  }
68  
69                  String interpolated = interpolator.interpolate(source, is);
70                  if (!source.equals(interpolated) && logger.isDebugEnabled()) {
71                      logger.debug(
72                              "Field " + contextDescription + " source: " + source + " interpolated to: " + interpolated);
73                  }
74                  return interpolated;
75              }
76          };
77      }
78  
79      public static ComponentXpp3Reader.ContentTransformer componentInterpolator(
80              final FixedStringSearchInterpolator interpolator, final InterpolationState is, final Logger logger) {
81          final Set<String> blacklistFields = new HashSet<>(FieldBasedObjectInterpolator.DEFAULT_BLACKLISTED_FIELD_NAMES);
82          blacklistFields.addAll(INTERPOLATION_BLACKLIST);
83  
84          return new ComponentXpp3Reader.ContentTransformer() {
85              @Override
86              public String transform(String source, String contextDescription) {
87                  if (blacklistFields.contains(contextDescription)) {
88                      return source;
89                  }
90  
91                  String interpolated = interpolator.interpolate(source, is);
92                  if (!source.equals(interpolated)) {
93                      logger.debug(
94                              "Field " + contextDescription + " source: " + source + " interpolated to: " + interpolated);
95                  }
96                  return interpolated;
97              }
98          };
99      }
100 
101     public static void checkErrors(AssemblyId assemblyId, InterpolationState interpolationState, Logger logger) {
102         if (interpolationState.asList() != null && interpolationState.asList().size() > 0 && logger.isDebugEnabled()) {
103             final StringBuilder sb = new StringBuilder();
104 
105             sb.append("One or more minor errors occurred while interpolating the assembly with ID: ")
106                     .append(assemblyId)
107                     .append(":\n");
108 
109             @SuppressWarnings("unchecked")
110             final List<Object> warnings = interpolationState.asList();
111             for (final Object warning : warnings) {
112                 sb.append('\n').append(warning);
113             }
114 
115             sb.append("\n\nThese values were SKIPPED, but the assembly process will continue.\n");
116 
117             logger.debug(sb.toString());
118         }
119     }
120 
121     public static FixedStringSearchInterpolator fullInterpolator(
122             final MavenProject project,
123             final FixedStringSearchInterpolator projectIp,
124             final AssemblerConfigurationSource configSource) {
125         FixedStringSearchInterpolator fixedStringSearchInterpolator = FixedStringSearchInterpolator.create(
126                 configSource.getRepositoryInterpolator(),
127                 configSource.getCommandLinePropsInterpolator(),
128                 configSource.getEnvInterpolator(),
129                 projectIp);
130         return fixedStringSearchInterpolator.withPostProcessor(new PathTranslatingPostProcessor(project.getBasedir()));
131     }
132 
133     private static final class PathTranslatingPostProcessor implements InterpolationPostProcessor {
134 
135         private final File basedir;
136 
137         PathTranslatingPostProcessor(final File basedir) {
138             this.basedir = basedir;
139         }
140 
141         @Override
142         public Object execute(final String expression, final Object value) {
143             final String path = String.valueOf(value);
144             return AssemblyFileUtils.makePathRelativeTo(path, basedir);
145         }
146     }
147 }