
org.bluestemsoftware.open.eoa.plugin.resources.AbstractResourcesMojo Maven / Gradle / Ivy
/**
* Copyright 2008 Bluestem Software LLC. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
package org.bluestemsoftware.open.eoa.plugin.resources;
/*
* Adapted from org.apache.maven.plugin.resources.ResourcesMojo which was released under the following
* license:
*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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 java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;
/**
* Copied/adapted from org.apache.maven.plugin.resources.ResourcesMojo.
*/
public abstract class AbstractResourcesMojo extends AbstractMojo {
/**
* The character encoding scheme to be applied.
*
* @parameter
*/
protected String encoding;
/**
* The list of resources we want to transfer.
*
* @parameter expression="${project.resources}"
* @required
*/
protected List> resources;
/**
* @parameter expression="${project}"
* @required
* @readonly
*/
protected MavenProject project;
protected static final String[] EMPTY_STRING_ARRAY = {};
protected static final String[] DEFAULT_INCLUDES = { "**/**" };
/**
* The output directory into which to copy the resources.
*
* @parameter expression="${project.build.outputDirectory}"
* @required
*/
protected File outputDirectory;
/**
* Base directory of the project.
*
* @parameter default-value="${basedir}"
* @required
* @readonly
*/
protected File basedir;
@SuppressWarnings("unchecked")
protected void copyResources(List> resources, File outputDirectory) throws MojoExecutionException {
if (encoding == null || encoding.length() < 1) {
getLog().info("Using default encoding to copy resources.");
} else {
getLog().info("Using '" + encoding + "' to copy resources.");
}
for (Iterator> i = resources.iterator(); i.hasNext();) {
Resource resource = (Resource)i.next();
String targetPath = resource.getTargetPath();
File resourceDirectory = new File(resource.getDirectory());
if (!resourceDirectory.isAbsolute()) {
resourceDirectory = new File(project.getBasedir(), resourceDirectory.getPath());
}
if (!resourceDirectory.exists()) {
getLog().info("Resource directory does not exist: " + resourceDirectory);
continue;
}
// this part is required in case the user specified "../something" as destination
// see MNG-1345
if (!outputDirectory.exists()) {
if (!outputDirectory.mkdirs()) {
throw new MojoExecutionException("Cannot create resource output directory: " + outputDirectory);
}
}
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(resourceDirectory);
if (resource.getIncludes() != null && !resource.getIncludes().isEmpty()) {
scanner.setIncludes((String[])resource.getIncludes().toArray(EMPTY_STRING_ARRAY));
} else {
scanner.setIncludes(DEFAULT_INCLUDES);
}
if (resource.getExcludes() != null && !resource.getExcludes().isEmpty()) {
scanner.setExcludes((String[])resource.getExcludes().toArray(EMPTY_STRING_ARRAY));
}
scanner.addDefaultExcludes();
scanner.scan();
List includedFiles = Arrays.asList(scanner.getIncludedFiles());
getLog().info(
"Copying "
+ includedFiles.size()
+ " resource"
+ (includedFiles.size() > 1 ? "s" : "")
+ (targetPath == null ? "" : " to " + targetPath));
for (Iterator j = includedFiles.iterator(); j.hasNext();) {
String name = j.next();
String destination = name;
if (targetPath != null) {
destination = targetPath + "/" + name;
}
File source = new File(resourceDirectory, name);
File destinationFile = new File(outputDirectory, destination);
if (!destinationFile.getParentFile().exists()) {
destinationFile.getParentFile().mkdirs();
}
try {
FileUtils.copyFile(source, destinationFile);
} catch (IOException e) {
throw new MojoExecutionException("Error copying resource " + source, e);
}
}
}
}
}