View Javadoc

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 java.io.File;
38  import java.io.FileNotFoundException;
39  import java.io.FileOutputStream;
40  import java.io.IOException;
41  import java.util.Properties;
42  
43  import org.apache.maven.plugin.AbstractMojo;
44  import org.apache.maven.plugin.MojoExecutionException;
45  import org.apache.maven.project.MavenProject;
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       * @parameter default-value="${project}"
55       * @required
56       * @readonly
57       */
58      protected MavenProject project;
59  
60      /**
61       * The properties file that will be used when writing properties.
62       *
63       * @parameter
64       * @required
65       */
66      protected File outputFile;
67  
68      protected void writeProperties(Properties properties, File file) throws MojoExecutionException {
69          FileOutputStream fos = null;
70          try {
71              fos = new FileOutputStream(file);
72              properties.store(fos, "Properties");
73          } catch (FileNotFoundException e) {
74              getLog().error("Could not create FileOutputStream: " + fos);
75              throw new MojoExecutionException(e.getMessage(), e);
76          } catch (IOException e) {
77              getLog().error("Error writing properties: " + fos);
78              throw new MojoExecutionException(e.getMessage(), e);
79          }
80  
81          try {
82              fos.close();
83          } catch (IOException e) {
84              getLog().error("Error closing FileOutputStream: " + fos);
85              throw new MojoExecutionException(e.getMessage(), e);
86          }
87      }
88  
89      protected void validateOutputFile() throws MojoExecutionException {
90          if (outputFile.isDirectory()) {
91              throw new MojoExecutionException("outputFile must be a file and not a directory");
92          }
93          // ensure path exists
94          if (outputFile.getParentFile() != null) {
95              outputFile.getParentFile().mkdirs();
96          }
97      }
98  }