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.artifact.Artifact;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.project.MavenProject;
26  
27  import java.io.File;
28  import java.io.FileWriter;
29  import java.io.IOException;
30  import java.util.Map;
31  
32  /**
33   * Mojo that creates one <code>touch.txt</code> or more files with configured filenames in <code>target/</code>
34   * directory, or cause failure if desired, and set build final name to '<code>coreitified</code>'
35   *
36   * @goal touch
37   * @phase process-sources
38   */
39  public class CoreItTouchMojo
40      extends AbstractMojo
41  {
42      /**
43       * @parameter default-value="${project}"
44       */
45      private MavenProject project;
46  
47      /**
48       * Output directory for touched files.
49       *
50       * @parameter default-value="${project.build.directory}"
51       * @required
52       */
53      private String outputDirectory;
54  
55      /**
56       * Test setting of plugin-artifacts on the PluginDescriptor instance.
57       *
58       * @parameter default-value="${plugin.artifactMap}"
59       * @required
60       */
61      private Map<String, Artifact> pluginArtifacts;
62  
63      /**
64       * Parameter to check that File attribute is injected with absolute path, even if parameter
65       * value is relative: a <code>touch.txt</code> file will be created in specified directory, to be able
66       * to check that absolute value is at right place.
67       *
68       * @parameter default-value="target/test-basedir-alignment"
69       */
70      private File basedirAlignmentDirectory;
71  
72      /**
73       * @parameter alias="pluginFile"
74       */
75      private String pluginItem = "foo";
76  
77      /**
78       * @parameter
79       */
80      private String goalItem = "bar";
81  
82      /**
83       * Touch a file named after artifact absolute file name, replacing '/' and ':' by '_' and adding ".txt".
84       *
85       * @parameter property="artifactToFile"
86       */
87      private String artifactToFile;
88  
89      /**
90       * Should the goal cause a failure before doing anything else?
91       *
92       * @parameter property="fail"
93       */
94      private boolean fail = false;
95  
96      public void execute()
97          throws MojoExecutionException
98      {
99          if ( fail )
100         {
101             throw new MojoExecutionException( "Failing per \'fail\' parameter"
102                 + " (specified in pom or system properties)" );
103         }
104 
105         File outDir = new File( outputDirectory );
106 
107         touch( outDir, "touch.txt" );
108 
109         // This parameter should be aligned to the basedir as the parameter type is specified
110         // as java.io.File
111         if ( !basedirAlignmentDirectory.isAbsolute() )
112         {
113             throw new MojoExecutionException( "basedirAlignmentDirectory not aligned" );
114         }
115         touch( basedirAlignmentDirectory, "touch.txt" );
116 
117         // Test parameter setting
118         if ( pluginItem != null )
119         {
120             touch( outDir, pluginItem );
121         }
122 
123         if ( goalItem != null )
124         {
125             touch( outDir, goalItem );
126         }
127 
128         if ( artifactToFile != null )
129         {
130             Artifact artifact = pluginArtifacts.get( artifactToFile );
131 
132             File artifactFile = artifact.getFile();
133 
134             String filename = artifactFile.getAbsolutePath().replace( '/', '_' ).replace( ':', '_' ) + ".txt";
135 
136             touch( outDir, filename );
137         }
138 
139         project.getBuild().setFinalName( "coreitified" );
140     }
141 
142     private void touch( File dir, String file )
143         throws MojoExecutionException
144     {
145         try
146         {
147              if ( !dir.exists() )
148              {
149                  dir.mkdirs();
150              }
151 
152              File touch = new File( dir, file );
153 
154              getLog().info( "Touching file: " + touch.getAbsolutePath() );
155 
156              FileWriter w = new FileWriter( touch );
157 
158              w.write( file );
159 
160              w.close();
161         }
162 
163         catch ( IOException e )
164         {
165             throw new MojoExecutionException( "Error touching file", e );
166         }
167     }
168 }