1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }