1 /**
2 * Copyright 2009-2012 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.codehaus.mojo.properties;
17
18 /*
19 * Licensed to the Apache Software Foundation (ASF) under one
20 * or more contributor license agreements. See the NOTICE file
21 * distributed with this work for additional information
22 * regarding copyright ownership. The ASF licenses this file
23 * to you under the Apache License, Version 2.0 (the
24 * "License"); you may not use this file except in compliance
25 * with the License. You may obtain a copy of the License at
26 *
27 * http://www.apache.org/licenses/LICENSE-2.0
28 *
29 * Unless required by applicable law or agreed to in writing,
30 * software distributed under the License is distributed on an
31 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
32 * KIND, either express or implied. See the License for the
33 * specific language governing permissions and limitations
34 * under the License.
35 */
36
37 import org.apache.maven.plugin.AbstractMojo;
38 import org.apache.maven.plugin.MojoExecutionException;
39 import org.apache.maven.project.MavenProject;
40
41 import java.io.File;
42 import java.io.FileNotFoundException;
43 import java.io.FileOutputStream;
44 import java.io.IOException;
45 import java.util.Properties;
46
47 /**
48 * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
49 * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
50 */
51 public abstract class AbstractWritePropertiesMojo extends AbstractMojo
52 {
53
54 /**
55 * @parameter default-value="${project}"
56 * @required
57 * @readonly
58 */
59 protected MavenProject project;
60
61 /**
62 * The properties file that will be used when writing properties.
63 *
64 * @parameter
65 * @required
66 */
67 protected File outputFile;
68
69 protected void writeProperties( Properties properties, File file )
70 throws MojoExecutionException
71 {
72 FileOutputStream fos = null;
73 try
74 {
75 fos = new FileOutputStream( file );
76 properties.store( fos, "Properties" );
77 }
78 catch ( FileNotFoundException e )
79 {
80 getLog().error( "Could not create FileOutputStream: " + fos );
81 throw new MojoExecutionException( e.getMessage(), e );
82 }
83 catch ( IOException e )
84 {
85 getLog().error( "Error writing properties: " + fos );
86 throw new MojoExecutionException( e.getMessage(), e );
87 }
88
89 try
90 {
91 fos.close();
92 }
93 catch ( IOException e )
94 {
95 getLog().error( "Error closing FileOutputStream: " + fos );
96 throw new MojoExecutionException( e.getMessage(), e );
97 }
98 }
99
100 protected void validateOutputFile()
101 throws MojoExecutionException
102 {
103 if ( outputFile.isDirectory() )
104 {
105 throw new MojoExecutionException( "outputFile must be a file and not a directory" );
106 }
107 // ensure path exists
108 if ( outputFile.getParentFile() != null )
109 {
110 outputFile.getParentFile().mkdirs();
111 }
112 }
113 }