|
| 1 | +package com.coderplus.plugins; |
| 2 | + |
| 3 | +/* |
| 4 | + * The MIT License |
| 5 | + * |
| 6 | + * Copyright (c) 2004 |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 9 | + * this software and associated documentation files (the "Software"), to deal in |
| 10 | + * the Software without restriction, including without limitation the rights to |
| 11 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 12 | + * of the Software, and to permit persons to whom the Software is furnished to do |
| 13 | + * so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in all |
| 16 | + * copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import org.apache.maven.plugin.AbstractMojo; |
| 32 | +import org.apache.maven.plugin.MojoExecutionException; |
| 33 | +import org.apache.maven.plugins.annotations.Component; |
| 34 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 35 | +import org.apache.maven.plugins.annotations.Mojo; |
| 36 | +import org.apache.maven.plugins.annotations.Parameter; |
| 37 | +import org.apache.maven.project.MavenProject; |
| 38 | +import org.sonatype.plexus.build.incremental.BuildContext; |
| 39 | +import org.codehaus.plexus.util.FileUtils; |
| 40 | + |
| 41 | + |
| 42 | +/** |
| 43 | + * Copy files during build |
| 44 | + * |
| 45 | + * @author <a href="aneesh@coderplus.com">Aneesh Joseph</a> |
| 46 | + * @since 1.0 |
| 47 | + */ |
| 48 | +@Mojo( name = "copy", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true ) |
| 49 | +public class CopyMojo |
| 50 | +extends AbstractMojo |
| 51 | +{ |
| 52 | + /** |
| 53 | + * The file which has to be copied |
| 54 | + * |
| 55 | + * @since 1.0 |
| 56 | + */ |
| 57 | + @Parameter( required = false ) |
| 58 | + private File sourceFile; |
| 59 | + /** |
| 60 | + * The target file to which the file should be copied(this shouldn't be a directory but a file which does or does not exist) |
| 61 | + * |
| 62 | + * @since 1.0 |
| 63 | + */ |
| 64 | + @Parameter( required = false ) |
| 65 | + private File destinationFile; |
| 66 | + |
| 67 | + /** |
| 68 | + * Collection of FileSets to work on (FileSet contains sourceFile and destinationFile). See <a href="./usage.html">Usage</a> for details. |
| 69 | + * |
| 70 | + * @since 1.0 |
| 71 | + */ |
| 72 | + @Parameter( required = false ) |
| 73 | + private List<FileSet> fileSets; |
| 74 | + |
| 75 | + /** |
| 76 | + * Overwrite files |
| 77 | + * |
| 78 | + * @since 1.0 |
| 79 | + */ |
| 80 | + @Parameter( property = "copy.overWrite", defaultValue = "true" ) |
| 81 | + boolean overWrite; |
| 82 | + |
| 83 | + /** |
| 84 | + * Ignore File Not Found errors during incremental build |
| 85 | + * |
| 86 | + * @since 1.0 |
| 87 | + */ |
| 88 | + @Parameter( property = "copy.ignoreFileNotFoundOnIncremental", defaultValue = "true" ) |
| 89 | + boolean ignoreFileNotFoundOnIncremental; |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * @since 1.0 |
| 94 | + */ |
| 95 | + @Component |
| 96 | + private MavenProject project; |
| 97 | + |
| 98 | + @Component |
| 99 | + private BuildContext buildContext; |
| 100 | + |
| 101 | + public void execute() throws MojoExecutionException |
| 102 | + { |
| 103 | + getLog().debug("Executing the copy-rename-maven-plugin"); |
| 104 | + if(fileSets!= null && fileSets.size() > 0){ |
| 105 | + for(FileSet fileSet: fileSets){ |
| 106 | + File srcFile = fileSet.getSourceFile(); |
| 107 | + File destFile = fileSet.getDestinationFile(); |
| 108 | + if(srcFile!=null){ |
| 109 | + copy(srcFile,destFile); |
| 110 | + } |
| 111 | + } |
| 112 | + } else if(sourceFile!= null){ |
| 113 | + copy(sourceFile,destinationFile); |
| 114 | + } else{ |
| 115 | + getLog().info("No Files to process"); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void copy(File srcFile,File destFile) throws MojoExecutionException{ |
| 120 | + |
| 121 | + if(!srcFile.exists()){ |
| 122 | + if(ignoreFileNotFoundOnIncremental && buildContext.isIncremental()){ |
| 123 | + getLog().warn("sourceFile "+srcFile.getAbsolutePath()+ " not found during incremental build"); |
| 124 | + } else { |
| 125 | + getLog().error("sourceFile "+srcFile.getAbsolutePath()+ " does not exist"); |
| 126 | + } |
| 127 | + } else if(srcFile.isDirectory()){ |
| 128 | + getLog().error("sourceFile "+srcFile.getAbsolutePath()+ " is not a file"); |
| 129 | + } else if(destFile == null){ |
| 130 | + getLog().error("destinationFile not specified"); |
| 131 | + } else if(destFile.exists() && destFile.isFile() && !overWrite){ |
| 132 | + getLog().error(destFile.getAbsolutePath()+" already exists and overWrite not set"); |
| 133 | + } else{ |
| 134 | + try { |
| 135 | + if(buildContext.isIncremental() && destFile.exists() && !buildContext.hasDelta(srcFile) && FileUtils.contentEquals(srcFile, destFile)){ |
| 136 | + getLog().info("No changes detected in "+srcFile.getAbsolutePath()); |
| 137 | + return; |
| 138 | + } |
| 139 | + FileUtils.copyFile(srcFile, destFile); |
| 140 | + getLog().info("Copied "+ srcFile.getAbsolutePath()+ " to "+ destFile.getAbsolutePath()); |
| 141 | + buildContext.refresh(destFile); |
| 142 | + } catch (IOException e) { |
| 143 | + throw new MojoExecutionException("could not copy "+srcFile.getAbsolutePath()+" to "+destFile.getAbsolutePath()); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + } |
| 149 | +} |
0 commit comments