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 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.project.MavenProject;
26
27 import java.io.File;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.OutputStreamWriter;
31
32 /**
33 * Creates a text file in the project base directory.
34 *
35 * @author Benjamin Bentmann
36 *
37 * @goal resources
38 * @phase process-resources
39 */
40 public class ResourcesMojo
41 extends AbstractMojo
42 {
43
44 /**
45 * The current Maven project.
46 *
47 * @parameter default-value="${project}"
48 * @required
49 * @readonly
50 */
51 private MavenProject project;
52
53 /**
54 * The path to the output file, relative to the project base directory directory.
55 *
56 * @parameter
57 */
58 private String pathname = "target/resources-resources.txt";
59
60 /**
61 * An optional message line to write to the output file (using UTF-8 encoding). If given, the output file will be
62 * opened in append mode.
63 *
64 * @parameter
65 */
66 private String message;
67
68 /**
69 * Runs this mojo.
70 *
71 * @throws MojoExecutionException If the output file could not be created.
72 * @throws MojoFailureException If the output file has not been set.
73 */
74 public void execute()
75 throws MojoExecutionException, MojoFailureException
76 {
77 getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + pathname );
78
79 if ( pathname == null || pathname.length() <= 0 )
80 {
81 throw new MojoFailureException( "Path name for output file has not been specified" );
82 }
83
84 File outputFile = new File( pathname );
85 if ( !outputFile.isAbsolute() )
86 {
87 outputFile = new File( project.getBasedir(), pathname ).getAbsoluteFile();
88 }
89
90 getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + outputFile );
91
92 try
93 {
94 outputFile.getParentFile().mkdirs();
95
96 if ( message != null && message.length() > 0 )
97 {
98 getLog().info( "[MAVEN-CORE-IT-LOG] " + message );
99
100 try ( OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream( outputFile, true ),
101 "UTF-8" ) )
102 {
103 writer.write( message );
104 writer.write( "\n" );
105 }
106 }
107 else
108 {
109 outputFile.createNewFile();
110 }
111 }
112 catch ( IOException e )
113 {
114 throw new MojoExecutionException( "Output file could not be created: " + pathname, e );
115 }
116
117 getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + outputFile );
118 }
119
120 }