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   * @goal touch
34   *
35   * @phase process-sources
36   *
37   * @description Goal which cleans the build
38   */
39  public class CoreItMojo
40      extends AbstractMojo
41  {
42      /**
43       * @parameter expression="${project}"
44       */
45      private MavenProject project;
46  
47      /**
48       * @parameter expression="${project.build.directory}"
49       * @required
50       */
51      private String outputDirectory;
52  
53      /** Test setting of plugin-artifacts on the PluginDescriptor instance.
54       * @parameter expression="${plugin.artifactMap}"
55       * @required
56       */
57      private Map pluginArtifacts;
58  
59      /**
60       * @parameter expression="target/test-basedir-alignment"
61       */
62      private File basedirAlignmentDirectory;
63  
64      /**
65       * @parameter alias="pluginFile"
66       */
67      private String pluginItem = "foo";
68  
69      /**
70       * @parameter
71       */
72      private String goalItem = "bar";
73  
74      /**
75       * @parameter expression="${artifactToFile}"
76       */
77      private String artifactToFile;
78  
79      /**
80       * @parameter expression="${fail}"
81       */
82      private boolean fail = false;
83  
84      public void execute()
85          throws MojoExecutionException
86      {
87          if ( fail )
88          {
89              throw new MojoExecutionException( "Failing per \'fail\' parameter (specified in pom or system properties)" );
90          }
91  
92          touch( new File( outputDirectory ), "touch.txt" );
93  
94          // This parameter should be aligned to the basedir as the parameter type is specified
95          // as java.io.File
96  
97          if ( !basedirAlignmentDirectory.isAbsolute() )
98          {
99              throw new MojoExecutionException( "basedirAlignmentDirectory not aligned" );
100         }
101 
102         touch( basedirAlignmentDirectory, "touch.txt" );
103 
104         File outDir = new File( outputDirectory );
105 
106         // Test parameter setting
107         if ( pluginItem != null )
108         {
109             touch( outDir, pluginItem );
110         }
111 
112         if ( goalItem != null )
113         {
114             touch( outDir, goalItem );
115         }
116 
117         if ( artifactToFile != null )
118         {
119             Artifact artifact = (Artifact) pluginArtifacts.get( artifactToFile );
120 
121             File artifactFile = artifact.getFile();
122 
123             String filename = artifactFile.getAbsolutePath().replace('/', '_').replace(':', '_') + ".txt";
124 
125             touch( outDir, filename );
126         }
127 
128         project.getBuild().setFinalName( "coreitified" );
129     }
130 
131     private void touch( File dir, String file )
132         throws MojoExecutionException
133     {
134         try
135         {
136              if ( !dir.exists() )
137              {
138                  dir.mkdirs();
139              }
140 
141              File touch = new File( dir, file );
142 
143              getLog().info( "Touching file: " + touch.getAbsolutePath() );
144 
145              FileWriter w = new FileWriter( touch );
146 
147              w.write( file );
148 
149              w.close();
150         }
151         catch ( IOException e )
152         {
153             throw new MojoExecutionException( "Error touching file", e );
154         }
155     }
156 }