View Javadoc
1   /*
2    * copy-rename-maven-plugin
3    *
4    * Copyright (c) 2014 Aneesh Joseph
5    * Copyright 2023 Christian Kohlschütter
6    *
7    * SPDX-Identifier: MIT
8    */
9   package com.kohlschutter.mavenplugins.copyrename;
10  
11  import java.io.File;
12  import java.io.IOException;
13  import java.util.List;
14  
15  /*
16   * The MIT License
17   *
18   * Copyright (c) 2004
19   *
20   * Permission is hereby granted, free of charge, to any person obtaining a copy of
21   * this software and associated documentation files (the "Software"), to deal in
22   * the Software without restriction, including without limitation the rights to
23   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
24   * of the Software, and to permit persons to whom the Software is furnished to do
25   * so, subject to the following conditions:
26   *
27   * The above copyright notice and this permission notice shall be included in all
28   * copies or substantial portions of the Software.
29   *
30   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36   * SOFTWARE.
37   */
38  import org.apache.maven.plugin.AbstractMojo;
39  import org.apache.maven.plugin.MojoExecutionException;
40  import org.apache.maven.plugins.annotations.Component;
41  import org.apache.maven.plugins.annotations.LifecyclePhase;
42  import org.apache.maven.plugins.annotations.Mojo;
43  import org.apache.maven.plugins.annotations.Parameter;
44  import org.apache.maven.project.MavenProject;
45  import org.codehaus.plexus.util.FileUtils;
46  import org.sonatype.plexus.build.incremental.BuildContext;
47  
48  // CPD-OFF
49  
50  /**
51   * Rename files or directories during build.
52   *
53   * @author Aneesh Joseph
54   * @since 1.0
55   */
56  @Mojo(name = "rename", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
57  public class RenameMojo extends AbstractMojo {
58    /**
59     * Skip execution.
60     *
61     * @since 2.0.0
62     */
63    @Parameter(property = "copy.skip", defaultValue = "false")
64    boolean skip;
65  
66    /**
67     * The file/directory which has to be renamed.
68     *
69     * @since 1.0
70     */
71    @Parameter(required = false)
72    private File sourceFile;
73    /**
74     * The target file/directory name.
75     *
76     * @since 1.0
77     */
78    @Parameter(required = false)
79    private File destinationFile;
80  
81    /**
82     * Collection of FileSets to work on (FileSet contains sourceFile and destinationFile). See
83     * <a href="./usage.html">Usage</a> for details.
84     *
85     * @since 1.0
86     */
87    @Parameter(required = false)
88    private List<FileSet> fileSets;
89  
90    /**
91     * Overwrite existing files.
92     *
93     * @since 1.0
94     */
95    @Parameter(property = "copy.overWrite", defaultValue = "true")
96    boolean overWrite;
97  
98    /**
99     * Don't throw an error when overWrite is false and target file already exists.
100    *
101    * @since 2.0.0
102    */
103   @Parameter(property = "copy.ignoreExisting", defaultValue = "false")
104   boolean ignoreExisting;
105 
106   /**
107    * Ignore renaming this file if it does not exist.
108    *
109    * @since 2.0.0
110    */
111   @Parameter(property = "copy.ignoreMissing", defaultValue = "false")
112   boolean ignoreMissing;
113 
114   /**
115    * Ignore errors if the source file/directory was not found during incremental build.
116    *
117    * @since 1.0
118    */
119   @Parameter(property = "copy.ignoreFileNotFoundOnIncremental", defaultValue = "true")
120   boolean ignoreFileNotFoundOnIncremental;
121 
122   /**
123    * Reference to the Maven project.
124    *
125    * @since 1.0
126    */
127   @Parameter(defaultValue = "${project}", readonly = true)
128   private MavenProject project;
129 
130   @Component
131   private BuildContext buildContext;
132 
133   /**
134    * Creates a new instance of the "rename" mojo.
135    */
136   public RenameMojo() {
137     super();
138   }
139 
140   @Override
141   public void execute() throws MojoExecutionException {
142     new RenameMojoExecutionContext(this).execute();
143   }
144 
145   private final class RenameMojoExecutionContext extends MojoExecutionContext {
146     protected RenameMojoExecutionContext(RenameMojo mojo) {
147       super(mojo);
148     }
149 
150     @Override
151     public void execute() throws MojoExecutionException {
152       if (skip) {
153         logDebug("Skipping the copy-rename-maven-plugin");
154         return;
155       }
156 
157       logDebug("Executing the copy-rename-maven-plugin");
158       if (fileSets != null && !fileSets.isEmpty()) {
159         for (FileSet fileSet : fileSets) {
160           File srcFile = fileSet.getSourceFile();
161           File destFile = fileSet.getDestinationFile();
162           if (srcFile != null) {
163             rename(srcFile, destFile);
164           }
165         }
166       } else if (sourceFile != null) {
167         rename(sourceFile, destinationFile);
168       } else {
169         logInfo("No Files to process");
170       }
171     }
172 
173     private void rename(File srcFile, File destFile) throws MojoExecutionException {
174       if (!srcFile.exists()) {
175         if (ignoreMissing) {
176           logInfo("Skipping rename of ", srcFile.getAbsolutePath(), " (missing)");
177         } else if (ignoreFileNotFoundOnIncremental && buildContext.isIncremental()) {
178           logWarn("sourceFile ", srcFile.getAbsolutePath(), " not found during incremental build");
179         } else {
180           logError("sourceFile ", srcFile.getAbsolutePath(), " does not exist");
181         }
182       } else if (destFile == null) {
183         logError("destinationFile not specified");
184       } else if (destFile.exists() && (destFile.isFile() == srcFile.isFile()) && !overWrite) {
185         if (ignoreExisting) {
186           logInfo("Skipping ", destFile.getAbsolutePath(), " (already exists)");
187         } else {
188           logError(destFile.getAbsolutePath(), " already exists and overWrite not set");
189         }
190       } else {
191         try {
192           FileUtils.rename(srcFile, destFile);
193           logInfo("Renamed ", srcFile.getAbsolutePath(), " to ", destFile.getAbsolutePath());
194           buildContext.refresh(destFile);
195         } catch (IOException e) {
196           throw new MojoExecutionException("could not rename " + srcFile.getAbsolutePath() + " to "
197               + destFile.getAbsolutePath(), e);
198         }
199       }
200     }
201   }
202 }