com.soebes.itf.maven.plugin.ResourcesMojo Maven / Gradle / Ivy
The newest version!
package com.soebes.itf.maven.plugin;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.filtering.MavenFilteringException;
import org.apache.maven.shared.filtering.MavenResourcesExecution;
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
/**
* Copy resources from src/test/resources-its to the appropriate location.
*
* @implNote Copied the majority of the code from maven-resources-plugin.
*/
@Mojo(name = "resources-its", defaultPhase = LifecyclePhase.PROCESS_TEST_RESOURCES, threadSafe = true)
public class ResourcesMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;
/**
* The character encoding to use when reading and writing filtered resources.
*/
@Parameter(defaultValue = "${project.build.sourceEncoding}")
private String encoding;
/**
* The character encoding to use when reading and writing filtered properties files.
* If not specified, it will default to the value of the "encoding" parameter.
*/
@Parameter
private String propertiesEncoding;
/**
* The list of additional filter properties files to be used along with System and project properties, which would
* be used for the filtering.
*
*/
@Parameter(defaultValue = "${project.build.filters}", readonly = true)
private List buildFilters;
@Component(role = MavenResourcesFiltering.class, hint = "default")
private MavenResourcesFiltering mavenResourcesFiltering;
/**
* Expressions preceded with this string won't be interpolated. Anything else preceded with this string will be
* passed through unchanged. For example {@code \${foo}} will be replaced with {@code ${foo}} but {@code \\${foo}}
* will be replaced with {@code \\value of foo}, if this parameter has been set to the backslash.
*/
@Parameter
private String escapeString;
/**
* Copy any empty directories included in the Resources.
*/
@Parameter(defaultValue = "false")
private boolean includeEmptyDirs;
/**
* The following extensions are already defined and will not being filtered.
*
* - jpg
* - jar
* - war
* - ear
* - aar
* - rar
* - har
* - sar
* - zip
* - tar
* - tar.gz
*
*/
@Parameter
private List nonFilteredFileExtensions;
/**
* Whether to escape backslashes and colons in windows-style paths.
*/
@Parameter(defaultValue = "true")
private boolean escapeWindowsPaths;
/**
*
* Set of delimiters for expressions to filter within the resources. These delimiters are specified in the form
* {@code beginToken*endToken}. If no {@code *} is given, the delimiter is assumed to be the same for start and end.
*
*
* So, the default filtering delimiters might be specified as:
*
*
*
* <delimiters>
* <delimiter>@</delimiter>
* </delimiters>
*
*
* Since the {@code @} delimiter is the same on both ends, we don't need to specify {@code @*@} (though we can).
*
*/
@Parameter(defaultValue = "@")
private LinkedHashSet delimiters;
/**
* By default files like {@code .gitignore}, {@code .cvsignore} etc. are NOT excluded which means they will being
* copied. If you need them for a particular reason you can do that by settings this to {@code }. This means
* all files like the following will be copied.
*
* - Misc: **/*~, **/#*#, **/.#*, **/%*%, **/._*
* - CVS: **/CVS, **/CVS/**, **/.cvsignore
* - RCS: **/RCS, **/RCS/**
* - SCCS: **/SCCS, **/SCCS/**
* - VSSercer: **/vssver.scc
* - MKS: **/project.pj
* - SVN: **/.svn, **/.svn/**
* - GNU: **/.arch-ids, **/.arch-ids/**
* - Bazaar: **/.bzr, **/.bzr/**
* - SurroundSCM: **/.MySCMServerInfo
* - Mac: **/.DS_Store
* - Serena Dimension: **/.metadata, **/.metadata/**
* - Mercurial: **/.hg, **/.hg/**
* - GIT: **/.git, **/.gitignore, **/.gitattributes, **/.git/**
* - Bitkeeper: **/BitKeeper, **/BitKeeper/**, **/ChangeSet,
* **/ChangeSet/**
* - Darcs: **/_darcs, **/_darcs/**, **/.darcsrepo,
* **/.darcsrepo/****/-darcs-backup*, **/.darcs-temp-mail
*
*/
@Parameter(defaultValue = "false")
private boolean addDefaultExcludes;
/**
* The output directory into which to copy the resources.
*/
@Parameter(defaultValue = "${project.build.outputDirectory}", required = true)
private File outputDirectory;
/**
* The list of resources we want to transfer.
*
*
default: src/test/resources-its
*
*/
@Parameter(readonly = true)
private List resources;
/**
* Overwrite existing files even if the destination files are newer.
*/
@Parameter(defaultValue = "false")
private boolean overwrite;
/**
*
* List of plexus components hint which implements
* {@link MavenResourcesFiltering#filterResources(MavenResourcesExecution)}. They will be executed after the
* resources copying/filtering.
*
*/
@Parameter
private List mavenFilteringHints;
private List mavenFilteringComponents = new ArrayList<>();
/**
* stop searching endToken at the end of line
*/
@Parameter(defaultValue = "false")
private boolean supportMultiLineFiltering;
/**
* Support filtering of filenames folders etc.
*/
@Parameter(defaultValue = "false")
private boolean fileNameFiltering;
/**
* {@inheritDoc}
*/
public void execute()
throws MojoExecutionException {
Resource resource = new Resource();
resource.setDirectory("src/test/resources-its");
resource.setTargetPath(project.getBuild().getTestOutputDirectory());
ArrayList includes = new ArrayList<>();
includes.add("**/**");
resource.setIncludes(includes);
//TODO: Document the default excludes we are using?
resource.setExcludes(Collections.emptyList());
// Enable filtering of resources
resource.setFiltering(true);
//TODO: Need to clean up.
this.resources = new ArrayList<>();
this.resources.add(resource);
if (Helper.isBlank(encoding) && isFilteringEnabled(this.resources)) {
getLog().warn("File encoding has not been set, using platform encoding "
+ System.getProperty("file.encoding")
+ ". Build is platform dependent!");
getLog().warn("See https://maven.apache.org/general.html#encoding-warning");
}
try {
MavenResourcesExecution mavenResourcesExecution =
new MavenResourcesExecution(this.resources, this.outputDirectory, project, encoding, Collections.emptyList(),
Collections.emptyList(), session);
mavenResourcesExecution.setEscapeWindowsPaths(escapeWindowsPaths);
// never include project build filters in this call, since we've already accounted for the POM build filters
// above, in getCombinedFiltersList().
mavenResourcesExecution.setInjectProjectBuildFilters(false);
mavenResourcesExecution.setEscapeString(escapeString);
mavenResourcesExecution.setOverwrite(overwrite);
mavenResourcesExecution.setIncludeEmptyDirs(includeEmptyDirs);
mavenResourcesExecution.setSupportMultiLineFiltering(supportMultiLineFiltering);
mavenResourcesExecution.setFilterFilenames(fileNameFiltering);
mavenResourcesExecution.setAddDefaultExcludes(addDefaultExcludes);
// We don't define supplemental properties at the moment.
mavenResourcesExecution.setAdditionalProperties(null);
//Using only `@project.version@`...
mavenResourcesExecution.setDelimiters(delimiters);
mavenResourcesExecution.setPropertiesEncoding(propertiesEncoding);
//Default list of extensions which are not filtered.
List filter = Arrays.asList("jpg", "jar", "war", "ear", "aar", "rar", "har", "sar", "zip", "tar", "tar.gz");
mavenResourcesExecution.setNonFilteredFileExtensions(filter);
if (nonFilteredFileExtensions != null) {
mavenResourcesExecution.setNonFilteredFileExtensions(nonFilteredFileExtensions);
}
mavenResourcesFiltering.filterResources(mavenResourcesExecution);
} catch (MavenFilteringException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
/**
* Determines whether filtering has been enabled for any resource.
*
* @param theResources The set of resources to check for filtering, may be null
.
* @return true
if at least one resource uses filtering, false
otherwise.
*/
private boolean isFilteringEnabled(Collection theResources) {
if (theResources != null) {
return theResources.stream().anyMatch(Resource::isFiltering);
}
return false;
}
}