View Javadoc
1   package org.apache.maven.plugin.coreit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  
25  import java.io.File;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.util.Properties;
29  
30  /**
31   * Check that we correctly use the implementation parameter.
32   *
33   * @goal param-implementation
34   */
35  public class ParameterImplementationMojo
36      extends AbstractMojo
37  {
38  
39      /**
40       * The path to the properties file for the parameter information.
41       *
42       * @parameter
43       */
44      private File outputFile;
45  
46      /**
47       * A parameter whose type is an interface but with a default implementation class.
48       *
49       * @parameter implementation="org.apache.maven.plugin.coreit.sub.AnImplementation"
50       */
51      private AnInterface theParameter;
52  
53      public void execute()
54          throws MojoExecutionException
55      {
56          Properties props = new Properties();
57  
58          if ( theParameter != null )
59          {
60              getLog().info( "[MAVEN-CORE-IT-LOG] theParameter = " + theParameter );
61  
62              props.setProperty( "theParameter.class", theParameter.getClass().getName() );
63              props.setProperty( "theParameter.string", theParameter.toString() );
64          }
65  
66          getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + outputFile );
67  
68          try
69          {
70              outputFile.getParentFile().mkdirs();
71  
72              try ( FileOutputStream os = new FileOutputStream( outputFile ) )
73              {
74                  props.store( os, "[MAVEN-CORE-IT-LOG]" );
75              }
76          }
77          catch ( IOException e )
78          {
79              throw new MojoExecutionException( "Failed to create output file " + outputFile + ": " + e.getMessage(), e );
80          }
81  
82          getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + outputFile );
83      }
84  
85  }