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   * Mojo to check that attribute injection through setter method (instead of direct parameter injection) works.
31   * @goal setter-touch
32   */
33  public class CoreItMojoWithSetters
34      extends AbstractMojo
35  {
36      /**
37       * @parameter default-value="${project.build.directory}"
38       * @required
39       */
40      private String outputDirectoryValue;
41  
42      /**
43       * @parameter name="foo"
44       */
45      private String fooValue;
46  
47      /**
48       * @parameter
49       */
50      private String bar;
51  
52      // ----------------------------------------------------------------------
53      // Setters
54      // ----------------------------------------------------------------------
55  
56      public void setOutputDirectory( String outputDirectory )
57      {
58          this.outputDirectoryValue = outputDirectory;
59      }
60  
61      boolean setFooSetterExecuted;
62  
63      public void setFoo( String fooValue )
64      {
65  
66          getLog().info( "setFoo: " + fooValue );
67  
68          this.fooValue = fooValue;
69  
70          setFooSetterExecuted = true;
71      }
72  
73      boolean setBarSetterExecuted;
74  
75      public void setBar( String barValue )
76      {
77  
78          getLog().info( "setBar: " + barValue );
79  
80          this.bar = barValue + ".baz";
81  
82          setBarSetterExecuted = true;
83      }
84  
85      // ----------------------------------------------------------------------
86      //
87      // ----------------------------------------------------------------------
88  
89  
90      public void execute()
91          throws MojoExecutionException
92      {
93          touch( new File( outputDirectoryValue ), "touch.txt" );
94  
95          File outDir = new File( outputDirectoryValue );
96  
97          // Test parameter setting
98          if ( fooValue != null && setFooSetterExecuted )
99          {
100 
101             getLog().info( "fooValue != null && setFooSetterExecuted" );
102 
103             touch( outDir, fooValue );
104         }
105 
106         if ( bar != null && setBarSetterExecuted )
107         {
108 
109             getLog().info( "bar != null && setBarSetterExecuted" );
110 
111             touch( outDir, bar );
112         }
113     }
114 
115     private void touch( File dir, String file )
116         throws MojoExecutionException
117     {
118 
119         getLog().info( "touch: " + dir.getPath() + ":" + file );
120 
121         try
122         {
123              if ( !dir.exists() )
124              {
125                  dir.mkdirs();
126              }
127 
128              File touch = new File( dir, file );
129 
130              FileWriter w = new FileWriter( touch );
131 
132              w.write( file );
133 
134              w.close();
135         }
136         catch ( IOException e )
137         {
138             throw new MojoExecutionException( "Error touching file", e );
139         }
140     }
141 }