001 /** 002 * Copyright 2009-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.codehaus.mojo.properties; 017 018 /* 019 * Licensed to the Apache Software Foundation (ASF) under one 020 * or more contributor license agreements. See the NOTICE file 021 * distributed with this work for additional information 022 * regarding copyright ownership. The ASF licenses this file 023 * to you under the Apache License, Version 2.0 (the 024 * "License"); you may not use this file except in compliance 025 * with the License. You may obtain a copy of the License at 026 * 027 * http://www.apache.org/licenses/LICENSE-2.0 028 * 029 * Unless required by applicable law or agreed to in writing, 030 * software distributed under the License is distributed on an 031 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 032 * KIND, either express or implied. See the License for the 033 * specific language governing permissions and limitations 034 * under the License. 035 */ 036 037 import java.io.File; 038 import java.io.FileNotFoundException; 039 import java.io.FileOutputStream; 040 import java.io.IOException; 041 import java.util.Properties; 042 043 import org.apache.maven.plugin.AbstractMojo; 044 import org.apache.maven.plugin.MojoExecutionException; 045 import org.apache.maven.project.MavenProject; 046 047 /** 048 * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a> 049 * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $ 050 */ 051 public abstract class AbstractWritePropertiesMojo extends AbstractMojo { 052 053 /** 054 * @parameter default-value="${project}" 055 * @required 056 * @readonly 057 */ 058 protected MavenProject project; 059 060 /** 061 * The properties file that will be used when writing properties. 062 * 063 * @parameter 064 * @required 065 */ 066 protected File outputFile; 067 068 protected void writeProperties(Properties properties, File file) throws MojoExecutionException { 069 FileOutputStream fos = null; 070 try { 071 fos = new FileOutputStream(file); 072 properties.store(fos, "Properties"); 073 } catch (FileNotFoundException e) { 074 getLog().error("Could not create FileOutputStream: " + fos); 075 throw new MojoExecutionException(e.getMessage(), e); 076 } catch (IOException e) { 077 getLog().error("Error writing properties: " + fos); 078 throw new MojoExecutionException(e.getMessage(), e); 079 } 080 081 try { 082 fos.close(); 083 } catch (IOException e) { 084 getLog().error("Error closing FileOutputStream: " + fos); 085 throw new MojoExecutionException(e.getMessage(), e); 086 } 087 } 088 089 protected void validateOutputFile() throws MojoExecutionException { 090 if (outputFile.isDirectory()) { 091 throw new MojoExecutionException("outputFile must be a file and not a directory"); 092 } 093 // ensure path exists 094 if (outputFile.getParentFile() != null) { 095 outputFile.getParentFile().mkdirs(); 096 } 097 } 098 }