From 0ae6dc716428e70f659e37d650b6619987428ae3 Mon Sep 17 00:00:00 2001 From: Richard Snijders Date: Sun, 3 Feb 2019 18:23:13 +0100 Subject: [PATCH] Fix #166 update-file-header changes file permissions: clears executable bit --- src/it/ISSUE-166/invoker.properties | 1 + src/it/ISSUE-166/pom.xml | 44 +++++++++++++++++++ src/it/ISSUE-166/postbuild.groovy | 17 +++++++ src/it/ISSUE-166/prebuild.groovy | 14 ++++++ .../ISSUE-166/src/main/bash/license-test.sh | 3 ++ .../mojo/license/AbstractFileHeaderMojo.java | 9 ++-- 6 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/it/ISSUE-166/invoker.properties create mode 100644 src/it/ISSUE-166/pom.xml create mode 100644 src/it/ISSUE-166/postbuild.groovy create mode 100644 src/it/ISSUE-166/prebuild.groovy create mode 100644 src/it/ISSUE-166/src/main/bash/license-test.sh diff --git a/src/it/ISSUE-166/invoker.properties b/src/it/ISSUE-166/invoker.properties new file mode 100644 index 000000000..144905625 --- /dev/null +++ b/src/it/ISSUE-166/invoker.properties @@ -0,0 +1 @@ +invoker.goals=clean license:update-file-header diff --git a/src/it/ISSUE-166/pom.xml b/src/it/ISSUE-166/pom.xml new file mode 100644 index 000000000..3ee5e11dc --- /dev/null +++ b/src/it/ISSUE-166/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + org.codehaus.mojo.license.test + ISSUE-166 + @pom.version@ + 2019 + + Test ISSUE-166 + http://www.mojohaus.org + + + MojoHaus + + + + + The GNU Lesser General Public License, Version 3.0 + http://www.gnu.org/licenses/lgpl-3.0.txt + repo + + + + + UTF-8 + gpl_v3 + true + + + + + + + org.codehaus.mojo + license-maven-plugin + @pom.version@ + + + + + diff --git a/src/it/ISSUE-166/postbuild.groovy b/src/it/ISSUE-166/postbuild.groovy new file mode 100644 index 000000000..3186df7c0 --- /dev/null +++ b/src/it/ISSUE-166/postbuild.groovy @@ -0,0 +1,17 @@ +import java.nio.file.Files +import java.nio.file.Paths + +scriptWithHeader = Paths.get(basedir.getAbsolutePath(), "src", "main", "bash", "license-test.sh") +backupFile = Paths.get(basedir.getAbsolutePath(), "src", "main", "bash", "license-test.sh~") + +if(!Files.isExecutable(scriptWithHeader)) { + println("Executable bit of the file $scriptWithHeader was cleared") + return false +} + +if(!Files.isExecutable(backupFile)) { + println("Executable bit of the backup file $backupFile was cleared") + reutrn false +} + +return true diff --git a/src/it/ISSUE-166/prebuild.groovy b/src/it/ISSUE-166/prebuild.groovy new file mode 100644 index 000000000..8cf4899c7 --- /dev/null +++ b/src/it/ISSUE-166/prebuild.groovy @@ -0,0 +1,14 @@ +import java.nio.file.Files +import java.nio.file.Paths + +// Ensure the file is executable to begin with. +scriptWithoutHeader = Paths.get(basedir.getAbsolutePath(), "src", "main", "bash", "license-test.sh") + +if(!Files.isExecutable(scriptWithoutHeader)) + scriptWithoutHeader.toFile().setExecutable(true, true) + +if(!Files.isExecutable(scriptWithoutHeader)) { + println("File $scriptWithoutHeader is not executable") + return false +} +return true \ No newline at end of file diff --git a/src/it/ISSUE-166/src/main/bash/license-test.sh b/src/it/ISSUE-166/src/main/bash/license-test.sh new file mode 100644 index 000000000..cd1840126 --- /dev/null +++ b/src/it/ISSUE-166/src/main/bash/license-test.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +# Some executable file +exit 0 \ No newline at end of file diff --git a/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java b/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java index d800965ac..1fdc04042 100644 --- a/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java @@ -41,6 +41,8 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.Arrays; import java.util.Collections; import java.util.EnumMap; @@ -927,7 +929,7 @@ private void finalizeFile( File file, File processFile ) throws IOException getLog().debug( " - backup original file " + file ); } - FileUtil.renameFile( file, backupFile ); + Files.copy( file.toPath(), backupFile.toPath(), StandardCopyOption.COPY_ATTRIBUTES ); } if ( isDryRun() ) @@ -940,9 +942,10 @@ private void finalizeFile( File file, File processFile ) throws IOException { try { - // replace file with the updated one - FileUtil.renameFile( processFile, file ); + String updatedContent = FileUtil.readAsString( processFile, getEncoding() ); + FileUtil.printString( file, updatedContent, getEncoding() ); + FileUtil.deleteFile( processFile ); } catch ( IOException e ) {