org.kuali.maven.plugins.externals.DirSyncMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of externals-maven-plugin Show documentation
Show all versions of externals-maven-plugin Show documentation
Tooling for integrating Maven with Subversion externals
/**
* Copyright 2011-2013 The Kuali Foundation
*
* Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
*
* 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.
*/
package org.kuali.maven.plugins.externals;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.tmatesoft.svn.core.SVNCommitInfo;
/**
* This mojo examines oldDir
for any files that are not also in newDir
. If it finds any, it deletes them from
* Subversion.
*
* @goal dirsync
*/
public class DirSyncMojo extends AbstractMojo {
SVNUtils svnUtils = SVNUtils.getInstance();
/**
* The Maven project object
*
* @parameter expression="${project}"
* @readonly
*/
private MavenProject project;
/**
* The message to associate with the commit
*
* @parameter expression="${externals.commitMessage}" default-value="Sync directories"
*/
private String commitMessage;
/**
* This is the directory containing files that should be under version control.
*
* @parameter expression="${externals.newDir}"
* @required
*/
private File newDir;
/**
* This is the directory containing files that are already under version control.
*
* @parameter expression="${externals.oldDir}"
* @required
*/
private File oldDir;
/**
* CSV list of regex patterns for files that should be included
*
* @parameter expression="${externals.include}" default-value="\*\*\/*"
*/
private String includes;
/**
* CSV list of regex patterns for files that should be excluded
*
* @parameter expression="${externals.exclude}" default-value="\*\*\/.svn/\*\*,\*\*\/.git/\*\*"
*/
private String excludes;
@Override
public void execute() throws MojoExecutionException {
try {
getLog().info("Syncing directories");
getLog().info("New Dir - " + newDir.getCanonicalPath());
getLog().info("Old Dir - " + oldDir.getCanonicalPath());
getLog().info("Include - " + includes);
getLog().info("Exclude - " + excludes);
List oldFiles = getFiles(oldDir, includes, excludes);
List newFiles = getFiles(newDir, includes, excludes);
List deletes = getDeletableFiles(newDir, oldDir, newFiles, oldFiles);
if (deletes.size() == 0) {
getLog().info("No files to delete.");
return;
}
getLog().info("Located - " + deletes.size() + " files to delete");
for (File delete : deletes) {
getLog().info("Deleting " + delete.getCanonicalPath());
}
svnUtils.markForDeletion(deletes);
SVNCommitInfo info = svnUtils.commit(oldDir, commitMessage, null, null);
getLog().info("Committed revision " + info.getNewRevision() + ".");
} catch (Exception e) {
throw new MojoExecutionException("Unexpected error", e);
}
}
protected List getRelativePaths(File dir, List files) throws IOException {
String path = dir.getCanonicalPath();
List paths = new ArrayList();
for (File file : files) {
String filePath = file.getCanonicalPath();
String s = StringUtils.remove(filePath, path);
paths.add(s);
}
return paths;
}
protected List getDeletableFiles(File newDir, File oldDir, List newFiles, List oldFiles) throws IOException {
List newPaths = getRelativePaths(newDir, newFiles);
List oldPaths = getRelativePaths(oldDir, oldFiles);
List deletableFiles = new ArrayList();
for (int i = 0; i < oldPaths.size(); i++) {
String path = oldPaths.get(i);
if (!newPaths.contains(path)) {
deletableFiles.add(oldFiles.get(i));
}
}
return deletableFiles;
}
protected List getFiles(File dir, String includes, String excludes) {
String[] includeTokens = StringUtils.split(includes, ",");
String[] excludeTokens = StringUtils.split(excludes, ",");
SimpleScanner scanner = new SimpleScanner(dir, includeTokens, excludeTokens);
return scanner.getFiles();
}
public String getCommitMessage() {
return commitMessage;
}
public void setCommitMessage(String commitMessage) {
this.commitMessage = commitMessage;
}
public File getOldDir() {
return oldDir;
}
public void setOldDir(File oldDir) {
this.oldDir = oldDir;
}
public File getNewDir() {
return newDir;
}
public void setNewDir(File newDir) {
this.newDir = newDir;
}
public String getIncludes() {
return includes;
}
public void setIncludes(String include) {
this.includes = include;
}
public String getExcludes() {
return excludes;
}
public void setExcludes(String exclude) {
this.excludes = exclude;
}
public MavenProject getProject() {
return project;
}
}