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.List;
29  import java.util.Map;
30  import java.util.Properties;
31  
32  /**
33   * Requires a singleton component in various ways and dumps the ids to a properties file.
34   *
35   * @author Benjamin Bentmann
36   *
37   * @goal it
38   * @phase initialize
39   */
40  public class ItMojo
41      extends AbstractMojo
42  {
43  
44      /**
45       * The path to the output file.
46       *
47       * @parameter property="touch.outputFile" default-value="target/comp.properties"
48       */
49      private File outputFile;
50  
51      /**
52       * Component lookup without role hint.
53       *
54       * @component
55       */
56      private Component componentWithoutRoleHint;
57  
58      /**
59       * Component lookup with explicit role hint.
60       *
61       * @component roleHint="default"
62       */
63      private Component componentWithRoleHint;
64  
65      /**
66       * Component lookup via active map.
67       *
68       * @component role="org.apache.maven.plugin.coreit.Component"
69       */
70      private Map componentMap;
71  
72      /**
73       * Component lookup via active list.
74       *
75       * @component role="org.apache.maven.plugin.coreit.Component"
76       */
77      private List componentList;
78  
79      /**
80       * Runs this mojo.
81       *
82       * @throws MojoExecutionException If the output file could not be created.
83       */
84      public void execute()
85          throws MojoExecutionException
86      {
87          Component componentFromMap = (Component) componentMap.values().iterator().next();
88          Component componentFromList = (Component) componentList.iterator().next();
89  
90          getLog().info( "[MAVEN-CORE-IT-LOG] Using component: " + componentWithoutRoleHint );
91          getLog().info( "[MAVEN-CORE-IT-LOG] Using component: " + componentWithRoleHint );
92          getLog().info( "[MAVEN-CORE-IT-LOG] Using component: " + componentFromMap );
93          getLog().info( "[MAVEN-CORE-IT-LOG] Using component: " + componentFromList );
94  
95          Properties props = new Properties();
96          props.setProperty( "id.0", componentWithoutRoleHint.getId() );
97          props.setProperty( "id.1", componentWithRoleHint.getId() );
98          props.setProperty( "id.2", componentFromMap.getId() );
99          props.setProperty( "id.3", componentFromList.getId() );
100 
101         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + outputFile );
102 
103         try
104         {
105             outputFile.getParentFile().mkdirs();
106             try ( FileOutputStream os = new FileOutputStream( outputFile ) )
107             {
108                 props.store( os, "MAVEN-CORE-IT-LOG" );
109             }
110         }
111         catch ( IOException e )
112         {
113             throw new MojoExecutionException( "Output file could not be created: " + outputFile, e );
114         }
115 
116         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + outputFile );
117     }
118 
119 }