001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.release.plugin; 018 019import org.apache.maven.plugin.MojoExecutionException; 020import org.apache.maven.plugin.logging.Log; 021import org.codehaus.plexus.util.FileUtils; 022 023import java.io.File; 024import java.io.FileInputStream; 025import java.io.FileOutputStream; 026import java.io.IOException; 027 028/** 029 * Shared static functions for all of our Mojos. 030 * 031 * @author chtompki 032 * @since 1.0 033 */ 034public final class SharedFunctions { 035 036 /** 037 * I want a buffer that is an array with 1024 elements of bytes. We declare 038 * the constant here for the sake of making the code more readable. 039 */ 040 public static final int BUFFER_BYTE_SIZE = 1024; 041 042 /** 043 * Making the constructor private because the class only contains static methods. 044 */ 045 private SharedFunctions() { 046 //Uitility Class 047 } 048 049 /** 050 * Cleans and then initializes an empty directory that is given by the <code>workingDirectory</code> 051 * parameter. 052 * 053 * @param log is the maven log for output logging, particularly in regards to error management. 054 * @param workingDirectory is a {@link File} that represents the directory to first attempt to delete then create. 055 * @throws MojoExecutionException when an {@link IOException} occurrs for the purpose of bubbling the exception 056 * up to maven properly. 057 */ 058 public static void initDirectory(Log log, File workingDirectory) throws MojoExecutionException { 059 if (workingDirectory.exists()) { 060 try { 061 FileUtils.deleteDirectory(workingDirectory); 062 } catch (IOException e) { 063 log.error(e.getMessage()); 064 throw new MojoExecutionException("Unable to remove directory: " + e.getMessage(), e); 065 } 066 } 067 if (!workingDirectory.exists()) { 068 workingDirectory.mkdirs(); 069 } 070 } 071 072 /** 073 * Copies a {@link File} from the <code>fromfile</code> to the <code>tofile</code> and logs the failure 074 * using the maven {@link Log}. 075 * 076 * @param log the {@link Log}, the maven logger. 077 * @param fromFile the {@link File} from which to copy. 078 * @param toFile the {@link File} to which to copy into. 079 * @throws MojoExecutionException if an {@link IOException} occurs. 080 */ 081 public static void copyFile(Log log, File fromFile, File toFile) throws MojoExecutionException { 082 FileInputStream in; 083 FileOutputStream out; 084 try { 085 in = new FileInputStream(fromFile); 086 out = new FileOutputStream(toFile); 087 byte[] buf = new byte[BUFFER_BYTE_SIZE]; 088 int len; 089 while ((len = in.read(buf)) > 0) { 090 out.write(buf, 0, len); 091 } 092 in.close(); 093 out.close(); 094 } catch (IOException e) { 095 log.error(e.getMessage()); 096 throw new MojoExecutionException("Unable to copy file: " + e.getMessage(), e); 097 } 098 } 099}