From f11e7043f8a5c5d35c3e5616f7b9ab3eb224d83e Mon Sep 17 00:00:00 2001 From: Darren Forsythe Date: Thu, 8 Feb 2018 19:57:09 +0000 Subject: [PATCH] Feature/enchance directory (#4) * Adding enchanced directory concatenation to specify a starting file * update README * README update * Test updates * test formatter * test formatter * removing formatting shell script --- README.md | 9 ++- format.sh | 2 - pom.xml | 4 +- .../io/github/flaw101/concat/ConcatMojo.java | 10 +++- .../github/flaw101/concat/ConcatParams.java | 60 ++++++++++++------- .../filewriter/setup/DirectorySetup.java | 3 + .../concat/validate/FileListValidator.java | 4 ++ .../github/flaw101/concat/ConcatMojoTest.java | 18 ++++++ src/test/resources/startingFile | 1 + .../test-pom-competing-arguments.xml | 37 ++++++++++++ .../test-pom-directory-with-starting-file.xml | 36 +++++++++++ 11 files changed, 158 insertions(+), 26 deletions(-) delete mode 100755 format.sh create mode 100644 src/test/resources/startingFile create mode 100644 src/test/resources/test-pom-competing-arguments.xml create mode 100644 src/test/resources/test-pom-directory-with-starting-file.xml diff --git a/README.md b/README.md index 9c2a366..8b204b6 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,8 @@ Will concat all files in the directory to the output file. * For selecting different types of concatentation implementations. Defaults to requiring a list of files as specified in the basic example. * `directory` * When using `ConcatenationType.DIRECTORY` specify the directory from which to get all files. Natural ordering of the file name is used to sort the files. +* `startingFile` + * When using directory this file will be used as the starting file for concatenation. ## Change Log ## @@ -122,4 +124,9 @@ Will concat all files in the directory to the output file. * Make `concatenationType` required. * Add debug logger for the parmas if enable. -* Move verification to CI profile \ No newline at end of file +* Move verification to CI profile + +### 1.3.0 ### + +* Add `startingFile` param used in `DIRECTORY` concatenation. + * If specified this file will be the first file to be concatenated to with the rest following with natural ordering. \ No newline at end of file diff --git a/format.sh b/format.sh deleted file mode 100755 index a4cb6e3..0000000 --- a/format.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -mvn formatter:format \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3b5d79b..c8e3376 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 io.github.flaw101 concat-maven-plugin - 1.2.0 + 1.3.0 maven-plugin Concat Maven Plugin Maven plugin that can concatenate several files into one new file. @@ -218,7 +218,7 @@ hashCode 4 - 68 + 66 88 diff --git a/src/main/java/io/github/flaw101/concat/ConcatMojo.java b/src/main/java/io/github/flaw101/concat/ConcatMojo.java index 2099e70..f078984 100644 --- a/src/main/java/io/github/flaw101/concat/ConcatMojo.java +++ b/src/main/java/io/github/flaw101/concat/ConcatMojo.java @@ -96,6 +96,14 @@ public class ConcatMojo extends AbstractMojo { */ private boolean deleteTargetFile; + /** + * If using {@link ConcantenationType#DIRECTORY} you can specify the file you wish to + * start, the rest will follow in natural order. + * + * @parameter + */ + private File startingFile; + /* * (non-Javadoc) * @@ -104,7 +112,7 @@ public class ConcatMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException { final ConcatParams params = new ConcatParams(directory, concatFiles, outputFile, - deleteTargetFile, appendNewline, concatenationType); + deleteTargetFile, appendNewline, concatenationType, startingFile); if (getLog().isDebugEnabled()) { getLog().debug("Concatenating with params" + params.toString()); } diff --git a/src/main/java/io/github/flaw101/concat/ConcatParams.java b/src/main/java/io/github/flaw101/concat/ConcatParams.java index 92a7dfe..0971f52 100644 --- a/src/main/java/io/github/flaw101/concat/ConcatParams.java +++ b/src/main/java/io/github/flaw101/concat/ConcatParams.java @@ -24,10 +24,9 @@ package io.github.flaw101.concat; import java.io.File; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.List; +import java.util.LinkedList; import io.github.flaw101.concat.service.ConcantenationType; @@ -44,20 +43,22 @@ public class ConcatParams { private final File outputFile; private final boolean deleteTargetFile; private final boolean appendNewline; - private final Collection files = new ArrayList(); + private final Collection files = new LinkedList(); private final ConcantenationType concatenationType; + private final File startingFile; - public ConcatParams(final String directory, final List files, - final File outputFile, final boolean deleteTargetFile, - final boolean appendNewline, final ConcantenationType concatentationType) { + public ConcatParams(String directory, Collection files, File outputFile, + boolean deleteTargetFile, boolean appendNewline, + ConcantenationType concatenationType, File startingFile) { this.directory = directory; if (files != null) { this.files.addAll(files); } - this.concatenationType = concatentationType; + this.concatenationType = concatenationType; this.outputFile = outputFile; this.deleteTargetFile = deleteTargetFile; this.appendNewline = appendNewline; + this.startingFile = startingFile; } public boolean isAppendNewline() { @@ -84,10 +85,30 @@ public void addAll(final Collection files) { this.files.addAll(files); } + public void add(final File file) { + this.files.add(file); + } + public String getDirectory() { return directory; } + public File getStartingFile() { + return startingFile; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("ConcatParams [directory=").append(directory) + .append(", outputFile=").append(outputFile).append(", deleteTargetFile=") + .append(deleteTargetFile).append(", appendNewline=").append(appendNewline) + .append(", files=").append(files).append(", concatenationType=") + .append(concatenationType).append(", startingFile=").append(startingFile) + .append("]"); + return builder.toString(); + } + @Override public int hashCode() { final int prime = 31; @@ -97,13 +118,14 @@ public int hashCode() { + ((concatenationType == null) ? 0 : concatenationType.hashCode()); result = prime * result + (deleteTargetFile ? 1231 : 1237); result = prime * result + ((directory == null) ? 0 : directory.hashCode()); - result = prime * result + (files.hashCode()); + result = prime * result + ((files == null) ? 0 : files.hashCode()); result = prime * result + ((outputFile == null) ? 0 : outputFile.hashCode()); + result = prime * result + ((startingFile == null) ? 0 : startingFile.hashCode()); return result; } @Override - public boolean equals(Object obj) { // NOSONAR + public boolean equals(Object obj) { if (this == obj) { return true; } @@ -133,6 +155,7 @@ else if (!directory.equals(other.directory)) { } if (other.files != null) { return false; + } else if (!files.equals(other.files)) { return false; @@ -145,17 +168,14 @@ else if (!files.equals(other.files)) { else if (!outputFile.equals(other.outputFile)) { return false; } + if (startingFile == null) { + if (other.startingFile != null) { + return false; + } + } + else if (!startingFile.equals(other.startingFile)) { + return false; + } return true; } - - @Override - public String toString() { - final StringBuilder builder = new StringBuilder(); - builder.append("ConcatParams [directory=").append(directory) - .append(", outputFile=").append(outputFile).append(", deleteTargetFile=") - .append(deleteTargetFile).append(", appendNewline=").append(appendNewline) - .append(", files=").append(files).append(", concatenationType=") - .append(concatenationType).append("]"); - return builder.toString(); - } } diff --git a/src/main/java/io/github/flaw101/concat/filewriter/setup/DirectorySetup.java b/src/main/java/io/github/flaw101/concat/filewriter/setup/DirectorySetup.java index 632d4de..3f67f64 100644 --- a/src/main/java/io/github/flaw101/concat/filewriter/setup/DirectorySetup.java +++ b/src/main/java/io/github/flaw101/concat/filewriter/setup/DirectorySetup.java @@ -45,6 +45,9 @@ public class DirectorySetup implements OutputSetup { @Override public void setup(final ConcatParams params) { final File directory = new File(params.getDirectory()); + if (params.getStartingFile() != null) { + params.add(params.getStartingFile()); + } final List listFiles = (List) FileUtils.listFiles(directory, FileFilterUtils.fileFileFilter(), null); Collections.sort(listFiles, new Comparator() { diff --git a/src/main/java/io/github/flaw101/concat/validate/FileListValidator.java b/src/main/java/io/github/flaw101/concat/validate/FileListValidator.java index bd7a0e0..aa68ae4 100644 --- a/src/main/java/io/github/flaw101/concat/validate/FileListValidator.java +++ b/src/main/java/io/github/flaw101/concat/validate/FileListValidator.java @@ -57,6 +57,10 @@ else if (StringUtils.isNotEmpty(concatParams.getDirectory())) { throw new ValidationFailedException( "Directory param is set, when File List conccat type is set. These are mutually exclusive"); } + else if (concatParams.getStartingFile() != null) { + throw new ValidationFailedException( + "Starting file is set. This is not used/required for this type of concatentation"); + } for (final File file : concatParams.getFiles()) { if (!file.exists()) { diff --git a/src/test/java/io/github/flaw101/concat/ConcatMojoTest.java b/src/test/java/io/github/flaw101/concat/ConcatMojoTest.java index d4be02b..fa02e52 100644 --- a/src/test/java/io/github/flaw101/concat/ConcatMojoTest.java +++ b/src/test/java/io/github/flaw101/concat/ConcatMojoTest.java @@ -64,6 +64,12 @@ public void testConcat_Directory() throws Exception { "target/concatfile.output", "file1file2file3"); } + @Test + public void testConcat_Directory_StartingFile() throws Exception { + execute("src/test/resources/test-pom-directory-with-starting-file.xml", "concat", + "target/concatfile.output", "startfile1file2file3"); + } + @Test public void testConcatShouldNotDeleteTarget() throws Exception { FileUtils.copyFile(new File("src/test/resources/testfiles/file_1.input"), @@ -124,6 +130,18 @@ public void testDirectoryCompetingArgs() throws Exception { } } + @Test + public void testListCompetingArgs() throws Exception { + try { + execute("src/test/resources/test-pom-competing-arguments.xml", "concat", + "bla", "nothing"); + } + catch (final Exception e) { + ExceptionUtils.printRootCauseStackTrace(e); + assertEquals(MojoExecutionException.class, e.getClass()); + } + } + @Test public void testDirectoryMissingDirectory() throws Exception { try { diff --git a/src/test/resources/startingFile b/src/test/resources/startingFile new file mode 100644 index 0000000..74853fb --- /dev/null +++ b/src/test/resources/startingFile @@ -0,0 +1 @@ +start \ No newline at end of file diff --git a/src/test/resources/test-pom-competing-arguments.xml b/src/test/resources/test-pom-competing-arguments.xml new file mode 100644 index 0000000..83c735f --- /dev/null +++ b/src/test/resources/test-pom-competing-arguments.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + io.github.flaw101 + test + 1.0-SNAPSHOT + jar + Test ConcatMojo + + + + junit + junit + 4.12 + test + + + + + + + concat-maven-plugin + + target/concatfile.output + FILE_LIST + + src/test/resources/testfiles/file_1.input + src/test/resources/testfiles/file_2.input + src/test/resources/testfiles/file_3.input + + src/test/resources/startingFile + + + + + \ No newline at end of file diff --git a/src/test/resources/test-pom-directory-with-starting-file.xml b/src/test/resources/test-pom-directory-with-starting-file.xml new file mode 100644 index 0000000..1e49336 --- /dev/null +++ b/src/test/resources/test-pom-directory-with-starting-file.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + io.github.flaw101 + test + 1.0-SNAPSHOT + jar + Test ConcatMojo + + + + junit + junit + 4.12 + test + + + + + + + com.flawless101 + concat-maven-plugin + + target/concatfile.output + src/test/resources/testfiles/ + + DIRECTORY + + src/test/resources/startingFile + + + + + \ No newline at end of file