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.FileWriter;
27  import java.io.IOException;
28  
29  /**
30   * @goal setter-touch
31   *
32   * @description Goal which cleans the build
33   */
34  public class CoreItMojoWithSetters
35      extends AbstractMojo
36  {
37      /**
38       * @parameter
39       *   property="outputDirectory"
40       *   expression="${project.build.directory}"
41       * @required
42       */
43      private String outputDirectoryValue;
44  
45      /**
46       * @parameter property="foo"
47       */
48      private String fooValue;
49  
50      /**
51       * @parameter
52       */
53      private String bar;
54  
55      // ----------------------------------------------------------------------
56      // Setters
57      // ----------------------------------------------------------------------
58  
59      public void setOutputDirectory( String outputDirectory )
60      {
61          this.outputDirectoryValue = outputDirectory;
62      }
63  
64      boolean setFooSetterExecuted;
65  
66      public void setFoo( String fooValue )
67      {
68          this.fooValue = fooValue;
69  
70          setFooSetterExecuted = true;
71      }
72  
73      boolean setBarSetterExecuted;
74  
75      public void setBar( String barValue )
76      {
77          this.bar = barValue + ".baz";
78  
79          setBarSetterExecuted = true;
80      }
81  
82      // ----------------------------------------------------------------------
83      //
84      // ----------------------------------------------------------------------
85  
86  
87      public void execute()
88          throws MojoExecutionException
89      {
90          touch( new File( outputDirectoryValue ), "touch.txt" );
91  
92          File outDir = new File( outputDirectoryValue );
93  
94          // Test parameter setting
95          if ( fooValue != null && setFooSetterExecuted )
96          {
97              touch( outDir, fooValue );
98          }
99  
100         if ( bar != null && setBarSetterExecuted )
101         {
102             touch( outDir, bar );
103         }
104     }
105 
106     private static void touch( File dir, String file )
107         throws MojoExecutionException
108     {
109         try
110         {
111              if ( !dir.exists() )
112              {
113                  dir.mkdirs();
114              }
115 
116              File touch = new File( dir, file );
117 
118              FileWriter w = new FileWriter( touch );
119 
120              w.write( file );
121 
122              w.close();
123         }
124         catch ( IOException e )
125         {
126             throw new MojoExecutionException( "Error touching file", e );
127         }
128     }
129 }