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 java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.maven.coreit.component.StatefulSingleton;
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  
29  /**
30   * Updates the state of the singleton component and optionally dumps the updated state to a properties file.
31   *
32   * @goal update-singleton
33   * @phase initialize
34   *
35   * @author Benjamin Bentmann
36   */
37  public class UpdateSingletonMojo
38      extends AbstractMojo
39  {
40  
41      /**
42       * @parameter default-value="provider"
43       */
44      private String key;
45  
46      /**
47       * @parameter default-value="passed"
48       */
49      private String value;
50  
51      /**
52       * @parameter
53       */
54      private File propertiesFile;
55  
56      /**
57       * @component
58       */
59      private StatefulSingleton singleton;
60  
61      /**
62       * Runs this mojo.
63       *
64       * @throws MojoExecutionException If the output file could not be created.
65       */
66      public void execute()
67          throws MojoExecutionException
68      {
69          getLog().info( "[MAVEN-CORE-IT-LOG] Singleton Instance: " + System.identityHashCode( singleton ) );
70          getLog().info( "[MAVEN-CORE-IT-LOG] Singleton Class Loader: " + singleton.getClass().getClassLoader() );
71  
72          getLog().info( "[MAVEN-CORE-IT-LOG] Setting property " + key + " = " + value );
73  
74          singleton.setProperty( key, value );
75  
76          if ( propertiesFile != null )
77          {
78              getLog().info( "[MAVEN-CORE-IT-LOG] Saving properties to " + propertiesFile );
79  
80              try
81              {
82                  singleton.saveProperties( propertiesFile );
83              }
84              catch ( IOException e )
85              {
86                  throw new MojoExecutionException( "Failed to save properties to " + propertiesFile, e );
87              }
88          }
89      }
90  
91  }