1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */19package org.apache.maven.tools.plugin;
2021import java.io.File;
22import java.net.URI;
23import java.nio.file.FileSystem;
24import java.nio.file.InvalidPathException;
25import java.nio.file.Path;
26import java.util.Collection;
27import java.util.HashSet;
28import java.util.List;
29import java.util.Set;
3031import org.apache.maven.artifact.Artifact;
32import org.apache.maven.plugin.descriptor.PluginDescriptor;
33import org.apache.maven.project.MavenProject;
34import org.apache.maven.settings.Settings;
35import org.codehaus.plexus.util.ReaderFactory;
36import org.eclipse.aether.RepositorySystemSession;
3738/**39 * Default implementation of {@link PluginToolsRequest}, which is used to pass parameters to components used to extract40 * {@link org.apache.maven.plugin.descriptor.MojoDescriptor MojoDescriptor} instances from different types of metadata41 * for a given plugin.42 *43 * @author jdcasey44 * @since 2.545 */46publicclassDefaultPluginToolsRequestimplementsPluginToolsRequest {
4748privatestaticfinal String DEFAULT_ENCODING = ReaderFactory.FILE_ENCODING;
4950private PluginDescriptor pluginDescriptor;
5152private MavenProject project;
5354private String encoding = DEFAULT_ENCODING;
5556privateboolean skipErrorNoDescriptorsFound;
5758private Set<Artifact> dependencies;
5960private RepositorySystemSession repoSession;
6162private URI internalJavadocBaseUrl;
6364private String internalJavadocVersion;
6566private List<URI> externalJavadocBaseUrls;
6768private Settings settings;
6970private String requiredJavaVersion;
7172private String mavenApiVersion;
7374private Collection<String> excludedScanDirectories;
7576publicDefaultPluginToolsRequest(MavenProject project, PluginDescriptor pluginDescriptor) {
77this.project = project;
78this.pluginDescriptor = pluginDescriptor;
79 }
8081/**82 * {@inheritDoc}83 */84 @Override
85public PluginDescriptor getPluginDescriptor() {
86return pluginDescriptor;
87 }
8889/**90 * {@inheritDoc}91 */92 @Override
93publicPluginToolsRequest setPluginDescriptor(PluginDescriptor pluginDescriptor) {
94this.pluginDescriptor = pluginDescriptor;
95returnthis;
96 }
9798/**99 * {@inheritDoc}100 */101 @Override
102public MavenProject getProject() {
103return project;
104 }
105106/**107 * {@inheritDoc}108 */109 @Override
110publicPluginToolsRequest setProject(MavenProject project) {
111this.project = project;
112returnthis;
113 }
114115/**116 * {@inheritDoc}117 */118 @Override
119public String getEncoding() {
120returnthis.encoding;
121 }
122123/**124 * {@inheritDoc}125 */126 @Override
127publicPluginToolsRequest setEncoding(String encoding) {
128if (encoding != null && !encoding.isEmpty()) {
129this.encoding = encoding;
130 } else {
131this.encoding = DEFAULT_ENCODING;
132 }
133134returnthis;
135 }
136137/**138 * {@inheritDoc}139 */140 @Override
141publicboolean isSkipErrorNoDescriptorsFound() {
142return skipErrorNoDescriptorsFound;
143 }
144145/**146 * {@inheritDoc}147 */148 @Override
149publicPluginToolsRequest setSkipErrorNoDescriptorsFound(boolean skipErrorNoDescriptorsFound) {
150this.skipErrorNoDescriptorsFound = skipErrorNoDescriptorsFound;
151returnthis;
152 }
153154 @Override
155public Set<Artifact> getDependencies() {
156if (this.dependencies == null) {
157this.dependencies = new HashSet<>();
158 }
159return dependencies;
160 }
161162 @Override
163publicPluginToolsRequest setDependencies(Set<Artifact> dependencies) {
164this.dependencies = dependencies;
165returnthis;
166 }
167168 @Override
169public RepositorySystemSession getRepoSession() {
170return repoSession;
171 }
172173publicvoid setRepoSession(RepositorySystemSession repoSession) {
174this.repoSession = repoSession;
175 }
176177 @Override
178publicPluginToolsRequest setInternalJavadocBaseUrl(URI baseUrl) {
179 internalJavadocBaseUrl = baseUrl;
180returnthis;
181 }
182183 @Override
184public URI getInternalJavadocBaseUrl() {
185return internalJavadocBaseUrl;
186 }
187188 @Override
189publicPluginToolsRequest setInternalJavadocVersion(String javadocVersion) {
190this.internalJavadocVersion = javadocVersion;
191returnthis;
192 }
193194 @Override
195public String getInternalJavadocVersion() {
196return internalJavadocVersion;
197 }
198199 @Override
200publicPluginToolsRequest setExternalJavadocBaseUrls(List<URI> javadocLinks) {
201this.externalJavadocBaseUrls = javadocLinks;
202returnthis;
203 }
204205 @Override
206public List<URI> getExternalJavadocBaseUrls() {
207return externalJavadocBaseUrls;
208 }
209210 @Override
211publicPluginToolsRequest setSettings(Settings settings) {
212this.settings = settings;
213returnthis;
214 }
215216 @Override
217public Settings getSettings() {
218return settings;
219 }
220221 @Override
222publicPluginToolsRequest setRequiredJavaVersion(String requiredJavaVersion) {
223this.requiredJavaVersion = requiredJavaVersion;
224returnthis;
225 }
226227 @Override
228public String getRequiredJavaVersion() {
229return requiredJavaVersion;
230 }
231232 @Override
233publicPluginToolsRequest setUsedMavenApiVersion(String mavenApiVersion) {
234this.mavenApiVersion = mavenApiVersion;
235returnthis;
236 }
237238 @Override
239public String getUsedMavenApiVersion() {
240return mavenApiVersion;
241 }
242243 @Override
244public Collection<String> getExcludedScanDirectories() {
245if (excludedScanDirectories == null) {
246 excludedScanDirectories = new HashSet<>();
247 }
248return excludedScanDirectories;
249 }
250251 @Override
252publicvoid setExcludedScanDirectories(Collection<String> excludedScanDirectories) {
253this.excludedScanDirectories = excludedScanDirectories;
254 }
255256 @Override
257publicboolean isExcludedScanDirectory(File sourceFile) {
258return isExcluded(sourceFile.toPath(), getExcludedScanDirectories());
259 }
260261/**262 * Determines whether a source directory is covered by any of the configured exclusions.263 * <p>264 * Visible for testing: taking the directory as a {@link Path} keeps the whole decision tied to a single265 * {@link FileSystem}, so the behaviour on Windows separators can be exercised without running on Windows.266 *267 * @param sourceDirectory the source directory to check268 * @param excludedScanDirectories the configured exclusions, either plain directories or globs269 * @return true if excluded, false otherwise270 */271staticboolean isExcluded(Path sourceDirectory, Collection<String> excludedScanDirectories) {
272 FileSystem fileSystem = sourceDirectory.getFileSystem();
273boolean windowsSeparators = "\\".equals(fileSystem.getSeparator());
274 Path sourcePath = sourceDirectory.toAbsolutePath().normalize();
275for (String excludedScanDirectory : excludedScanDirectories) {
276if (excludedScanDirectory == null || excludedScanDirectory.isEmpty()) {
277// an empty entry would resolve to the working directory and exclude it silently278continue;
279 }
280// The documented form of this parameter is a plain directory, so compare as paths first. That281// is safe with respect to separators, trailing separators and case on every file system.282try {
283 Path excludedPath = fileSystem
284 .getPath(excludedScanDirectory)
285 .toAbsolutePath()
286 .normalize();
287if (sourcePath.equals(excludedPath)) {
288returntrue;
289 }
290 } catch (InvalidPathException e) {
291// not a plain directory - the Windows parser rejects wildcards, for instance - so it can292// only ever have been meant as a glob293 }
294if (fileSystem
295 .getPathMatcher("glob:" + toGlobPattern(excludedScanDirectory, windowsSeparators))
296 .matches(sourcePath)) {
297returntrue;
298 }
299 }
300return false;
301 }
302303/**304 * Turns a configured exclusion into a glob pattern.305 * <p>306 * Configured values normally come from an interpolated property such as307 * <code>${project.build.directory}/generated-sources/annotations</code> and therefore hold backslashes on308 * Windows. A backslash is the escape character of the glob syntax and is swallowed by the pattern compiler309 * on every platform, so such a value could never match a real path, and one ending in a separator even310 * failed the build with a {@link java.util.regex.PatternSyntaxException}. A forward slash is compiled back311 * into the Windows separator, so it is the portable way to spell a separator in a glob.312 *313 * @param excludedScanDirectory the configured exclusion314 * @param windowsSeparators whether the file system separates names with a backslash315 * @return the pattern to hand to {@link FileSystem#getPathMatcher(String)}316 */317static String toGlobPattern(String excludedScanDirectory, boolean windowsSeparators) {
318// On a file system that allows backslashes in names they may be a deliberate escape, so leave them be.319return windowsSeparators ? excludedScanDirectory.replace('\\', '/') : excludedScanDirectory;
320 }
321 }