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  import org.apache.maven.project.MavenProject;
25  import org.apache.maven.project.MavenProjectBuilder;
26  
27  import java.io.File;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  import java.util.Properties;
31  
32  /**
33   * @author Benjamin Bentmann
34   */
35  public abstract class AbstractPomMojo
36      extends AbstractMojo
37  {
38  
39      /**
40       * The project builder.
41       *
42       * @component
43       */
44      protected MavenProjectBuilder builder;
45  
46      protected void dump( Properties props, String key, MavenProject project )
47      {
48          put( props, key + "project.id", project.getId() );
49          put( props, key + "project.name", project.getName() );
50          put( props, key + "project.description", project.getDescription() );
51          if ( project.getArtifact() != null )
52          {
53              put( props, key + "artifact.id", project.getArtifact().getId() );
54          }
55      }
56  
57      protected void put( Properties props, String key, Object value )
58      {
59          if ( value != null )
60          {
61              props.setProperty( key, value.toString() );
62          }
63      }
64  
65      protected void store( Properties props, File file )
66          throws MojoExecutionException
67      {
68          try
69          {
70              file.getParentFile().mkdirs();
71  
72              try ( FileOutputStream os = new FileOutputStream( file ) )
73              {
74                  props.store( os, "[MAVEN-CORE-IT-LOG]" );
75              }
76          }
77          catch ( IOException e )
78          {
79              throw new MojoExecutionException( "Failed to dump POMs: " + e.getMessage(), e );
80          }
81      }
82  
83  }