All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
it.anyplace.web.CssProcessorMojo Maven / Gradle / Ivy
/**
* Copyright 2013 Davide Imbriaco
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* 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 it.anyplace.web;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import javax.annotation.Nullable;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.NotFileFilter;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.lesscss.LessCompiler;
import org.lesscss.LessException;
/**
*
* @author aleph
*/
@Mojo(name = "css", defaultPhase = LifecyclePhase.PACKAGE)
public class CssProcessorMojo extends AbstractMojo {
private final Log logger = getLog();
@Parameter(required = true, property = "sourceCssDirectory", defaultValue = "${project.basedir}/src/main/css")
private File sourceCssDirectory;
@Parameter(required = true, property = "targetCssDirectory", defaultValue = "${project.build.directory}/${project.build.finalName}/css")
private File targetCssDirectory;
@Parameter(property = "defaultPrefix", defaultValue = "")
private String defaultPrefix;
@Parameter(property = "compressCss", defaultValue = "false")
private boolean compressCss;
@Parameter(property = "excludedCssRegexp", defaultValue = "^.*[0-9]+.*less$")
private String excludedCssRegexp;
private LessCompiler lessCompiler;
public void execute() throws MojoExecutionException, MojoFailureException {
logger.info("executing a-web css processor");
try {
if (sourceCssDirectory.exists()) {
targetCssDirectory.mkdirs();
for (File file : sourceCssDirectory.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
})) {
if (!file.getName().matches(excludedCssRegexp)) {
processCssFile(file);
}
}
for (File directory : sourceCssDirectory.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
})) {
String prefix = defaultPrefix + "-" + directory.getName();
processCssFiles(prefix, FileUtils.listFiles(directory, cssFileFilter, TrueFileFilter.INSTANCE));
}
}
} catch (Exception ex) {
throw new MojoExecutionException("error processing css", ex);
}
}
private LessCompiler getLessCompiler() {
if (lessCompiler == null) {
// LessOptions lessOptions = new LessOptions();
// lessEngine = new LessEngine(lessOptions, new FilesystemResourceLoader());
lessCompiler = new LessCompiler();
lessCompiler.setCompress(compressCss);
}
return lessCompiler;
}
private final IOFileFilter cssFileFilter = new RegexFileFilter("^.*[.](css|less)$");
private void processCssFile(File inputFile) throws LessException, IOException {
String data = processCssFileToString(inputFile);
if (data != null) {
File outputFile = new File(targetCssDirectory, inputFile.getName().replaceFirst("[.][^.]+$", ".css"));
FileUtils.write(outputFile, data);
}
}
private @Nullable
String processCssFileToString(File inputFile) throws LessException, IOException {
String data;
if (inputFile.getName().endsWith(".less")) {
// data = getLessCompiler().compile(inputFile, compressCss);
logger.debug("processing less file = " + inputFile);
data = getLessCompiler().compile(inputFile);
} else if (inputFile.getName().endsWith(".css")) {
logger.debug("processing css file = " + inputFile);
data = FileUtils.readFileToString(inputFile);
} else {
data = null;
}
return data;
}
private void processCssFiles(String prefix, Iterable inputFiles) throws LessException, IOException {
File targetFile = new File(targetCssDirectory, prefix + ".css");
logger.debug("processing css files to = " + targetFile);
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (File inputFile : inputFiles) {
String data = processCssFileToString(inputFile);
for (File resourceFile : FileUtils.listFiles(inputFile.getParentFile(), new NotFileFilter(cssFileFilter), TrueFileFilter.INSTANCE)) {
// String resourceName=resourceFile.getName();
String cssPath = inputFile.getParentFile().getCanonicalPath(),
resourcePath = resourceFile.getParentFile().getCanonicalPath(),
relativePath = resourcePath.substring(cssPath.length());
logger.debug("css path = " + cssPath + " , resource path = " + resourcePath + " , relative path = " + relativePath);
// String relativePath = resourceFile.getParentFile().getAbsoluteFile().toURI().relativize(inputFile.getParentFile().getAbsoluteFile().toURI()).getPath();
// File resourceDir = relativePath.isEmpty() ? targetCssDirectory : new File(targetCssDirectory, relativePath);
String newResourceDirName = inputFile.getParentFile().getName(), newResourceUrl = newResourceDirName;
File resourceDir = new File(targetCssDirectory, newResourceDirName);
if (!relativePath.isEmpty()) {
resourceDir = new File(resourceDir, relativePath);
newResourceUrl = newResourceUrl + "/" + relativePath;
}
newResourceUrl = newResourceUrl + "/" + resourceFile.getName();
data = data.replaceAll("[a-zA-Z0-9._/-]*" + resourceFile.getName(), newResourceUrl);
// resourceDir.mkdirs();
logger.debug("copying css resource = " + resourceFile + " to dir = " + resourceDir);
FileUtils.copyFileToDirectory(resourceFile, resourceDir);
}
IOUtils.write("\n\n/** BEGIN FILE " + inputFile.getName() + " **/\n\n", out);
IOUtils.write(data, out);
IOUtils.write("\n\n/** END FILE " + inputFile.getName() + " **/\n\n", out);
}
// TODO css compression
FileUtils.writeByteArrayToFile(targetFile, out.toByteArray());
}
}