├── .github
└── workflows
│ └── maven.yml
├── .gitignore
├── LICENSE
├── README.md
├── pom.xml
└── src
├── main
└── java
│ └── io
│ └── github
│ └── floverfelt
│ └── find
│ └── and
│ └── replace
│ └── maven
│ └── plugin
│ ├── FindAndReplaceMojo.java
│ └── tasks
│ └── ProcessFilesTask.java
├── site
├── apt
│ ├── examples
│ │ └── basic.apt
│ └── index.apt
└── site.xml
└── test
├── java
└── io
│ └── github
│ └── floverfelt
│ └── find
│ └── and
│ └── replace
│ └── maven
│ └── plugin
│ └── FindAndReplaceMojoTest.java
└── resources
└── integration
├── non-utf
├── test-file.txt
├── test-file.xml
└── test-file.yml
/.github/workflows/maven.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3 |
4 | name: Java CI with Maven
5 |
6 | on: [push]
7 |
8 | jobs:
9 | build:
10 |
11 | runs-on: ubuntu-latest
12 |
13 | steps:
14 | - uses: actions/checkout@v2
15 | - name: Set up JDK
16 | uses: actions/setup-java@v2
17 | with:
18 | java-version: '8'
19 | distribution: 'temurin'
20 | cache: maven
21 | - name: Build with Maven
22 | run: mvn -B package --file pom.xml
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # IntelliJ files
11 | .idea/
12 | *.iml
13 |
14 | # Mobile Tools for Java (J2ME)
15 | .mtj.tmp/
16 |
17 | # Package Files #
18 | *.jar
19 | *.war
20 | *.nar
21 | *.ear
22 | *.zip
23 | *.tar.gz
24 | *.rar
25 | target/
26 |
27 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
28 | hs_err_pid*
29 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Find and Replace Maven Plugin
2 |
3 | There's a lack of an open source find and replace plugin in the Maven repository.
4 |
5 | The most widely recommended one is the [maven-replacer-plugin](https://mvnrepository.com/artifact/com.google.code.maven-replacer-plugin/maven-replacer-plugin) which
6 | is very old and just a wrapper around the [ant task](https://ant.apache.org/manual/Tasks/replace.html) which lacks support for file and directory names.
7 |
8 | The find-and-replace maven plugin is an attempt to remedy these issues. It's open source, uses vanilla Java 8, and allows for replacement within files, for file names, and for directory names.
9 |
10 | It's intended to be simple to read and contribute to.
11 |
12 | ## Documentation
13 |
14 | Documentation can be found [here](https://floverfelt.org/find-and-replace-maven-plugin).
15 |
16 | ## Usage
17 |
18 | The find-and-replace-maven-plugin is lifecycle phase agnostic. Simply add it to your pom wherever you'd like it to execute.
19 |
20 | ```
21 |
22 | io.github.floverfelt
23 | find-and-replace-maven-plugin
24 |
25 |
26 | exec
27 | package
28 |
29 | find-and-replace
30 |
31 |
32 |
33 | directory-names
34 | testing/
35 | _
36 | -
37 | true
38 |
39 |
40 |
41 |
42 | ```
43 |
44 | ## Contribution
45 |
46 | If you'd like to contribute, feel free to raise a pull request or issue. You can support the plugin monetarily [here](https://www.buymeacoffee.com/floverfelt).
47 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | io.github.floverfelt
8 | find-and-replace-maven-plugin
9 | 1.2.1-SNAPSHOT
10 | maven-plugin
11 |
12 | Find and Replace Maven Plugin
13 | A plugin for replacing content in files, filenames, and directories.
14 | https://github.com/floverfelt/find-and-replace-maven-plugin
15 |
16 |
17 |
18 | The Apache Software License, Version 2.0
19 | http://www.apache.org/licenses/LICENSE-2.0.txt
20 | repo
21 |
22 |
23 |
24 |
25 |
26 | Florian Overfelt
27 | foverfelt@gmail.com
28 | https://github.com/floverfelt
29 |
30 |
31 |
32 |
33 | scm:git://github.com/floverfelt/find-and-replace-maven-plugin.git
34 | scm:git:git@github.com:floverfelt/find-and-replace-maven-plugin.git
35 | https://github.com/floverfelt/find-and-replace-maven-plugin
36 | HEAD
37 |
38 |
39 |
40 | GitHub
41 | https://github.com/floverfelt/find-and-replace-maven-plugin/issues
42 |
43 |
44 |
45 |
46 | ossrh
47 | https://s01.oss.sonatype.org/content/repositories/snapshots
48 |
49 |
50 | ossrh
51 | https://s01.oss.sonatype.org/content/repositories/releases/
52 |
53 |
54 |
55 |
56 | 3.8.1
57 | 1.8
58 | 1.8
59 | 3.2.0
60 | 0.12
61 | UTF-8
62 |
63 |
64 |
65 |
66 | org.apache.maven
67 | maven-core
68 | ${maven.plugin.version}
69 | provided
70 |
71 |
72 | org.apache.maven
73 | maven-plugin-api
74 | ${maven.plugin.version}
75 | provided
76 |
77 |
78 | org.apache.maven.plugin-tools
79 | maven-plugin-annotations
80 | ${maven.plugin.version}
81 |
82 |
83 | junit
84 | junit
85 | 4.13.2
86 | test
87 |
88 |
89 |
90 |
91 |
92 |
93 | org.apache.maven.plugins
94 | maven-plugin-plugin
95 | 3.6.0
96 |
97 |
98 | org.apache.maven.plugins
99 | maven-compiler-plugin
100 | 3.8.1
101 |
102 |
103 | org.apache.maven.plugins
104 | maven-javadoc-plugin
105 | ${maven.javadoc.version}
106 |
107 | true
108 | true
109 |
110 | http://docs.oracle.com/javase/8/docs/api/
111 | http://docs.oracle.com/javaee/8/index.html
112 | http://maven.apache.org/ref/3.6.0/maven-core/apidocs/
113 | http://maven.apache.org/ref/3.6.0/maven-plugin-api/apidocs/
114 |
115 |
116 |
117 |
122 |
123 | org.apache.maven.plugins
124 | maven-release-plugin
125 | 3.0.0-M1
126 |
127 | release
128 | deploy
129 |
130 |
131 |
136 |
137 | org.sonatype.plugins
138 | nexus-staging-maven-plugin
139 | 1.6.7
140 | true
141 |
142 | ossrh
143 | https://s01.oss.sonatype.org/
144 | true
145 |
146 |
147 |
148 | org.apache.maven.plugins
149 | maven-project-info-reports-plugin
150 | 3.1.1
151 |
152 |
153 | org.apache.maven.plugins
154 | maven-site-plugin
155 | 3.7.1
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 | org.apache.maven.plugins
164 | maven-plugin-plugin
165 | 3.6.0
166 |
167 |
168 |
169 |
170 |
171 |
172 | release
173 |
174 |
175 |
176 | org.apache.maven.plugins
177 | maven-source-plugin
178 | 3.2.0
179 |
180 |
181 | attach-sources
182 |
183 | jar-no-fork
184 |
185 |
186 |
187 |
188 |
189 | org.apache.maven.plugins
190 | maven-javadoc-plugin
191 | ${maven.javadoc.version}
192 |
193 |
194 | attach-javadocs
195 |
196 | jar
197 |
198 |
199 |
200 |
201 |
202 | org.apache.maven.plugins
203 | maven-gpg-plugin
204 | 1.6
205 |
206 |
207 | sign-artifacts
208 | verify
209 |
210 | sign
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
--------------------------------------------------------------------------------
/src/main/java/io/github/floverfelt/find/and/replace/maven/plugin/FindAndReplaceMojo.java:
--------------------------------------------------------------------------------
1 | package io.github.floverfelt.find.and.replace.maven.plugin;
2 |
3 | import io.github.floverfelt.find.and.replace.maven.plugin.tasks.ProcessFilesTask;
4 | import org.apache.maven.plugin.AbstractMojo;
5 | import org.apache.maven.plugin.MojoExecutionException;
6 | import org.apache.maven.plugin.MojoFailureException;
7 | import org.apache.maven.plugins.annotations.LifecyclePhase;
8 | import org.apache.maven.plugins.annotations.Mojo;
9 | import org.apache.maven.plugins.annotations.Parameter;
10 | import org.apache.maven.project.MavenProject;
11 |
12 | import java.nio.charset.Charset;
13 | import java.nio.file.Path;
14 | import java.nio.file.Paths;
15 | import java.util.ArrayList;
16 | import java.util.Arrays;
17 | import java.util.List;
18 | import java.util.regex.Pattern;
19 |
20 | /**
21 | * The find and replace maven plugin will find a regex string in filenames, file contents, and directory names
22 | * and replace it with a given value.
23 | */
24 | @Mojo(name = "find-and-replace", defaultPhase = LifecyclePhase.NONE, threadSafe = true)
25 | public class FindAndReplaceMojo extends AbstractMojo {
26 |
27 | @Parameter(defaultValue = "${project}", readonly = true, required = true)
28 | private MavenProject project;
29 |
30 | /**
31 | * The base directory from which to perform the find and replace.
32 | * This is relative to the location of the pom.
33 | *
34 | * @parameter baseDir
35 | */
36 | @Parameter(property = "baseDir", defaultValue = "${basedir}")
37 | private String baseDir;
38 |
39 | /**
40 | * Whether the find and replace is recursive from the baseDir.
41 | *
42 | * @parameter recursive
43 | */
44 | @Parameter(property = "recursive", defaultValue = "false")
45 | private boolean recursive;
46 |
47 | /**
48 | * A CSV of what type of replacement(s) being done. Valid values are: file-contents filenames directory-names
49 | *
50 | * file-contents will replace the find regex within a file.
51 | * filenames will replace the find regex within a file's name.
52 | * directory-names will replace the find regex within a directory's name
53 | *
54 | * To run the find and replace for multiple types, pass them as a CSV:
55 | * file-contents,filenames,directory-names
56 | *
57 | * @parameter replacementType
58 | */
59 | @Parameter(property = "replacementType", required = true)
60 | private String replacementType;
61 |
62 | /**
63 | * The regex string to find.
64 | *
65 | * @parameter findRegex
66 | */
67 | @Parameter(property = "findRegex", required = true)
68 | private String findRegex;
69 |
70 | /**
71 | * The value to replace the matching findRegex with.
72 | *
73 | * @parameter replaceValue
74 | */
75 | @Parameter(property = "replaceValue", required = true, defaultValue = "")
76 | private String replaceValue;
77 |
78 | /**
79 | * A CSV of the file types to search in.
80 | * For example for the value: .xml
81 | * Only files ending with .xml will be renamed.
82 | *
83 | * For the value: .xml,.properties
84 | * Only files ending with .xml,.properties will be renamed.
85 | *
86 | * Ignored for directories.
87 | *
88 | * @parameter fileMask
89 | */
90 | @Parameter(property = "fileMask")
91 | private String fileMask;
92 |
93 | /**
94 | * Regex filenames/directory-names to exclude.
95 | *
96 | * @parameter exclusions
97 | */
98 | @Parameter(property = "exclusions")
99 | private String exclusions;
100 |
101 | /**
102 | * Skip execution of the plugin.
103 | *
104 | * @parameter skip
105 | */
106 | @Parameter(property = "skip", defaultValue = "false")
107 | private boolean skip;
108 |
109 | /**
110 | * Specify file encoding during file-contents replacement
111 | *
112 | * Default set to Charset.defaultCharset();
113 | *
114 | *
115 | * @parameter encoding
116 | */
117 | @Parameter(property = "encoding")
118 | private String encoding;
119 |
120 | /**
121 | * Whether the find and replace maven plugin replaces all matches or just the first match.
122 | *
123 | * @parameter replaceAll
124 | */
125 | @Parameter(property = "replaceAll", defaultValue = "true")
126 | private boolean replaceAll;
127 |
128 | private Charset charset = Charset.defaultCharset();
129 |
130 | private static final String FILE_CONTENTS = "file-contents";
131 | private static final String FILENAMES = "filenames";
132 | private static final String DIRECTORY_NAMES = "directory-names";
133 |
134 | private Path baseDirPath;
135 |
136 | private List validReplacementTypes = Arrays.asList(FILE_CONTENTS, FILENAMES, DIRECTORY_NAMES);
137 | private boolean processFileContents = false;
138 | private boolean processFilenames = false;
139 | private boolean processDirectoryNames = false;
140 |
141 | private List fileMaskList = new ArrayList<>();
142 | private List exclusionsList = new ArrayList<>();
143 |
144 | public void execute() throws MojoExecutionException, MojoFailureException {
145 |
146 | if (skip) {
147 | getLog().warn("Skipping execution of find-and-replace-maven-plugin.");
148 | return;
149 | }
150 |
151 | setup();
152 |
153 | getLog().info("Executing find-and-replace maven plugin with options: " + this.toString());
154 |
155 | try {
156 | ProcessFilesTask.process(getLog(), baseDirPath, recursive, Pattern.compile(findRegex), replaceValue, fileMaskList,
157 | exclusionsList, processFileContents, processFilenames, processDirectoryNames, replaceAll, charset);
158 | } catch (Exception e) {
159 | throw new MojoFailureException("Unable to process files.", e);
160 | }
161 |
162 | }
163 |
164 | private void setup() throws MojoExecutionException {
165 |
166 | setupReplacementTypes();
167 |
168 | setupFileMasks();
169 |
170 | setupExclusions();
171 |
172 | setupBaseDir();
173 |
174 | setupEncoding();
175 |
176 | }
177 |
178 | private void setupReplacementTypes() throws MojoExecutionException {
179 | if (replacementType == null || replacementType.isEmpty()) {
180 | throw new MojoExecutionException("replacementType cannot be null or empty.");
181 | }
182 |
183 | String[] replacementTypeList = replacementType.split(",");
184 |
185 | String logMessage = "Mode set to ";
186 |
187 | for (String replacementTypeVal : replacementTypeList) {
188 |
189 | if (!validReplacementTypes.contains(replacementTypeVal)) {
190 | throw new MojoExecutionException("Invalid replacementType specified: " + replacementTypeVal);
191 | }
192 |
193 | if (FILE_CONTENTS.equals(replacementTypeVal)) {
194 | getLog().info(logMessage + FILE_CONTENTS);
195 | processFileContents = true;
196 | }
197 |
198 | if (FILENAMES.equals(replacementTypeVal)) {
199 | getLog().info(logMessage + FILENAMES);
200 | processFilenames = true;
201 | }
202 |
203 | if (DIRECTORY_NAMES.equals(replacementTypeVal)) {
204 | getLog().info(logMessage + DIRECTORY_NAMES);
205 | processDirectoryNames = true;
206 | }
207 |
208 | }
209 |
210 | }
211 |
212 | private void setupFileMasks() {
213 | if (fileMask != null && !fileMask.isEmpty()) {
214 | fileMaskList = Arrays.asList(fileMask.split(","));
215 | getLog().info("fileMasks set to: " + fileMaskList);
216 | }
217 |
218 | }
219 |
220 |
221 | private void setupEncoding() {
222 | if (encoding != null && !encoding.isEmpty()) {
223 | try {
224 | charset = Charset.forName(encoding);
225 | getLog().info("encoding set to: " + charset);
226 | } catch (Exception e) {
227 | getLog().warn("Invalid encoding value " + encoding + ". Using default charset.");
228 | }
229 | }
230 |
231 | }
232 |
233 | private void setupExclusions() {
234 | if (exclusions != null && !exclusions.isEmpty()) {
235 | getLog().info("Compiling regex for exclusions: " + exclusions);
236 | exclusionsList.add(Pattern.compile(exclusions));
237 | }
238 |
239 | }
240 |
241 | private void setupBaseDir() {
242 |
243 | if (project == null) {
244 | baseDirPath = Paths.get(baseDir);
245 | } else if (baseDir.equals(project.getBasedir().getAbsolutePath())) {
246 | baseDirPath = Paths.get(baseDir);
247 | } else {
248 | baseDirPath = Paths.get(project.getBasedir().getAbsolutePath(), baseDir);
249 | }
250 |
251 | getLog().info("baseDir set to: " + baseDirPath.toString());
252 |
253 | }
254 |
255 | @Override
256 | public String toString() {
257 | final StringBuilder sb = new StringBuilder("FindAndReplaceMojo{");
258 | sb.append("baseDir='").append(baseDir).append('\'');
259 | sb.append(", recursive=").append(recursive);
260 | sb.append(", replacementType='").append(replacementType).append('\'');
261 | sb.append(", findRegex='").append(findRegex).append('\'');
262 | sb.append(", replaceValue='").append(replaceValue).append('\'');
263 | sb.append(", fileMask='").append(fileMask).append('\'');
264 | sb.append(", exclusions='").append(exclusions).append('\'');
265 | sb.append(", skip=").append(skip);
266 | sb.append(", baseDirPath=").append(baseDirPath);
267 | sb.append(", processFileContents=").append(processFileContents);
268 | sb.append(", processFilenames=").append(processFilenames);
269 | sb.append(", processDirectoryNames=").append(processDirectoryNames);
270 | sb.append(", fileMaskList=").append(fileMaskList);
271 | sb.append(", exclusionsList=").append(exclusionsList);
272 | sb.append(", encoding=").append(encoding);
273 | sb.append(", replaceAll=").append(replaceAll);
274 | sb.append('}');
275 | return sb.toString();
276 | }
277 | }
278 |
--------------------------------------------------------------------------------
/src/main/java/io/github/floverfelt/find/and/replace/maven/plugin/tasks/ProcessFilesTask.java:
--------------------------------------------------------------------------------
1 | package io.github.floverfelt.find.and.replace.maven.plugin.tasks;
2 |
3 | import org.apache.maven.plugin.logging.Log;
4 |
5 | import java.io.BufferedReader;
6 | import java.io.BufferedWriter;
7 | import java.io.File;
8 | import java.io.FileInputStream;
9 | import java.io.FileOutputStream;
10 | import java.io.IOException;
11 | import java.io.InputStreamReader;
12 | import java.io.OutputStreamWriter;
13 | import java.nio.charset.Charset;
14 | import java.nio.file.Files;
15 | import java.nio.file.Path;
16 | import java.nio.file.Paths;
17 | import java.util.ArrayList;
18 | import java.util.Arrays;
19 | import java.util.List;
20 | import java.util.ListIterator;
21 | import java.util.regex.Matcher;
22 | import java.util.regex.Pattern;
23 |
24 | public class ProcessFilesTask {
25 |
26 | private ProcessFilesTask() {
27 | // do not instantiate
28 | }
29 |
30 | /**
31 | * Stupid simple implementation of file walking, renaming, etc.
32 | *
33 | * @param log the maven-plugin log
34 | * @param baseDir the directory to start in
35 | * @param isRecursive whether to recurse further
36 | * @param findRegex the regex to find
37 | * @param replaceValue the value to replace the found regex
38 | * @param fileMasks the file masks to use
39 | * @param exclusions the filename regex to exclude
40 | * @param processFileContents whether to process file contents
41 | * @param processFilenames whether to process file names
42 | * @param processDirectoryNames whether to process directory names
43 | * @param charset encoding to be used when reading files
44 | */
45 | public static void process(Log log, Path baseDir, boolean isRecursive, Pattern findRegex, String replaceValue,
46 | List fileMasks, List exclusions, boolean processFileContents,
47 | boolean processFilenames, boolean processDirectoryNames, boolean replaceAll, Charset charset) throws IOException {
48 |
49 | // Load in the files in the base dir
50 | File[] baseDirFiles = new File(baseDir.toUri()).listFiles();
51 | if (baseDirFiles == null) {
52 | throw new IOException(String.format("Unable to list file(s) in baseDir='%s'", baseDir));
53 | }
54 | List filesToProcess = new ArrayList<>(Arrays.asList(baseDirFiles));
55 |
56 | ListIterator iterator = filesToProcess.listIterator();
57 |
58 | while (iterator.hasNext()) {
59 |
60 | File file = iterator.next();
61 |
62 | // Remove the file from processing
63 | iterator.remove();
64 |
65 | // Perform dir checks
66 | if (file.isDirectory()) {
67 | iterator = processDirectory(iterator, isRecursive, file, processDirectoryNames, exclusions, findRegex, replaceValue, replaceAll, log);
68 | }
69 |
70 | if (file.isFile()) {
71 | processFile(log, exclusions, file, fileMasks, processFileContents, findRegex, replaceValue, processFilenames, replaceAll, charset);
72 | }
73 |
74 | }
75 |
76 | }
77 |
78 |
79 | private static ListIterator processDirectory(ListIterator iterator, boolean isRecursive, File file,
80 | boolean processDirectoryNames, List exclusions, Pattern findRegex,
81 | String replaceValue, boolean replaceAll, Log log) throws IOException {
82 |
83 | // Rename the directory
84 | if (processDirectoryNames && !shouldExcludeFile(exclusions, file)) {
85 | file = renameFile(log, file, findRegex, replaceValue, replaceAll);
86 | }
87 |
88 | // If recursive, add child files to iterator
89 | if (isRecursive) {
90 | File[] filesToAdd = file.listFiles();
91 | assert filesToAdd != null;
92 | for (File f : filesToAdd) {
93 | iterator.add(f);
94 | iterator.previous();
95 | }
96 | }
97 |
98 | return iterator;
99 |
100 | }
101 |
102 |
103 | private static boolean shouldExcludeFile(List exclusions, File file) {
104 |
105 | for (Pattern p : exclusions) {
106 | if (p.matcher(file.getName()).find()) {
107 | return true;
108 | }
109 | }
110 |
111 | return false;
112 |
113 | }
114 |
115 | private static boolean shouldProcessFile(List fileMasks, File file) {
116 |
117 | if (fileMasks.isEmpty()) {
118 | return true;
119 | }
120 |
121 | for (String fileMask : fileMasks) {
122 | if (file.getName().endsWith(fileMask)) {
123 | return true;
124 | }
125 | }
126 |
127 | return false;
128 |
129 | }
130 |
131 | private static File renameFile(Log log, File file, Pattern findRegex, String replaceValue, boolean replaceAll) throws IOException {
132 |
133 | Path filePath = file.toPath();
134 | Path parentDir = filePath.getParent();
135 | String oldName = file.getName();
136 | Matcher matcher = findRegex.matcher(oldName);
137 | String newName = replaceAll ? matcher.replaceAll(replaceValue) : matcher.replaceFirst(replaceValue);
138 |
139 | if (!newName.equals(oldName)) {
140 | Path targetPath = Paths.get(parentDir.toString(), newName);
141 |
142 | log.info(String.format("Renaming %s to %s", oldName, newName));
143 |
144 | return new File(Files.move(filePath, targetPath).toUri());
145 |
146 | }
147 |
148 | return file;
149 |
150 | }
151 |
152 | private static void processFileContents(File file, Pattern findRegex, String replaceValue, boolean replaceAll, Charset charset) throws IOException {
153 |
154 | File tempFile = File.createTempFile("tmp", "tmp", file.getParentFile());
155 |
156 | try (FileInputStream fis = new FileInputStream(file);
157 | InputStreamReader isr = new InputStreamReader(fis, charset);
158 | BufferedReader fileReader = new BufferedReader(isr)) {
159 | try (FileOutputStream fos = new FileOutputStream(tempFile);
160 | OutputStreamWriter osr = new OutputStreamWriter(fos, charset);
161 | BufferedWriter fileWriter = new BufferedWriter(osr)) {
162 |
163 | boolean alreadyReplaced = false;
164 |
165 | for (String line = fileReader.readLine(); line != null; line = fileReader.readLine()) {
166 | Matcher matcher = findRegex.matcher(line);
167 | if (matcher.find()) {
168 | if (replaceAll) {
169 | line = matcher.replaceAll(replaceValue);
170 | }
171 | else if (!alreadyReplaced) {
172 | line = matcher.replaceFirst(replaceValue);
173 | alreadyReplaced = true;
174 | }
175 | }
176 | try {
177 | fileWriter.write(line + "\n");
178 | } catch (IOException e) {
179 | throw new RuntimeException(e);
180 | }
181 | }
182 | }
183 | }
184 |
185 | if (!file.delete()) {
186 | throw new IOException("Failed to delete file at: " + file.getPath());
187 | }
188 |
189 | if (!tempFile.renameTo(file)) {
190 | throw new IOException("Failed to rename temp file at: " + tempFile.getPath() + " to " + file.getPath());
191 | }
192 |
193 | }
194 |
195 | private static void processFile(Log log, List exclusions, File file, List fileMasks,
196 | boolean processFileContents, Pattern findRegex, String replaceValue,
197 | boolean processFilenames, boolean replaceAll, Charset charset) throws IOException {
198 |
199 | if (shouldExcludeFile(exclusions, file) || !shouldProcessFile(fileMasks, file)) {
200 | return;
201 | }
202 |
203 | if (processFileContents) {
204 | processFileContents(file, findRegex, replaceValue, replaceAll, charset);
205 | }
206 |
207 | if (processFilenames) {
208 | renameFile(log, file, findRegex, replaceValue, replaceAll);
209 | }
210 |
211 | }
212 |
213 | }
214 |
--------------------------------------------------------------------------------
/src/site/apt/examples/basic.apt:
--------------------------------------------------------------------------------
1 | ------
2 | Basic Configuration
3 | ------
4 | Florian Overfelt
5 | ------
6 | 2021-04-13
7 | ------
8 |
9 | Sample configurations
10 |
11 | The following is a configuration to recursively process only directory names in the target directory
12 |
13 | +---+
14 |
15 | io.github.floverfelt
16 | find-and-replace-maven-plugin
17 |
18 |
19 | exec
20 | package
21 |
22 | find-and-replace
23 |
24 |
25 | directory-names
26 | target/
27 | _
28 | -
29 | true
30 |
31 |
32 |
33 |
34 | +---+
35 |
36 | The following is a configuration to recursively process only filenames ending in .xml in the target directory.
37 |
38 | +---+
39 |
40 | io.github.floverfelt
41 | find-and-replace-maven-plugin
42 |
43 |
44 | exec
45 | package
46 |
47 | find-and-replace
48 |
49 |
50 | filenames
51 | target/
52 | _
53 | -
54 | true
55 | .xml
56 |
57 |
58 |
59 |
60 | +---+
61 |
62 | The following is a configuration to recursively process only file contents for files ending in .yml in the target directory.
63 |
64 | +---+
65 |
66 | io.github.floverfelt
67 | find-and-replace-maven-plugin
68 |
69 |
70 | exec
71 | package
72 |
73 | find-and-replace
74 |
75 |
76 | file-contents
77 | target/
78 | _
79 | -
80 | true
81 | .yml
82 |
83 |
84 |
85 |
86 | +---+
87 |
88 | The following is a configuration to recursively process file contents, directory names, and file names in the target directory. It excludes any files ending with "sources".
89 |
90 | +---+
91 |
92 | io.github.floverfelt
93 | find-and-replace-maven-plugin
94 |
95 |
96 | exec
97 | package
98 |
99 | find-and-replace
100 |
101 |
102 | filenames,directory-names,file-contents
103 | target/
104 | _
105 | -
106 | true
107 | sources$
108 |
109 |
110 |
111 |
112 | +---+
--------------------------------------------------------------------------------
/src/site/apt/index.apt:
--------------------------------------------------------------------------------
1 | ------
2 | Introduction
3 | ------
4 | Florian Overfelt
5 | ------
6 | 2021-04-13
7 | ------
8 |
9 |
10 | Find and Replace Maven Plugin
11 |
12 | This plugin uses vanilla Java code to find and replace values within files, in filenames, and in directory names.
13 |
14 | * Goals Overview
15 |
16 | This plugin only supports a single goal bound to no lifecycle phase. That goal is:
17 |
18 | * {{{./find-and-replace-mojo.html}find-and-replace:find-and-replace}} find's and replaces values in files, filenames, and directory names.
19 |
20 | * Usage
21 |
22 | General instructions on how to use the Plugin Name can be found on the {{{usage.html}usage page}}. Some more
23 | specific use cases are described in the examples given below.
24 |
25 | If you feel like the plugin is missing a feature or has a defect, you can fill a feature request or bug report in the
26 | {{{https://github.com/floverfelt/find-and-replace-maven-plugin/issues}issue management system}}. When creating a new issue, please provide a comprehensive description of your
27 | concern. Especially for fixing bugs it is crucial that the developers can reproduce your problem. For this reason,
28 | entire debug logs, POMs or most preferably little demo projects attached to the issue are very much appreciated.
29 | Of course, patches are welcome, too. Contributors can check out the project from our
30 | {{{https://github.com/floverfelt/find-and-replace-maven-plugin}source repository}}.
31 |
32 | * Examples
33 |
34 | Basic syntax and configurations can be found at:
35 |
36 | * {{{./examples/basic.html}Basic Configurations}}
37 |
--------------------------------------------------------------------------------
/src/site/site.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | org.apache.maven.skins
20 | maven-fluido-skin
21 | 1.7
22 |
23 |
24 |
25 |
26 | span3
27 | span9
28 |
29 | floverfelt/find-and-replace-maven-plugin
30 | right
31 | red
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/src/test/java/io/github/floverfelt/find/and/replace/maven/plugin/FindAndReplaceMojoTest.java:
--------------------------------------------------------------------------------
1 | package io.github.floverfelt.find.and.replace.maven.plugin;
2 |
3 | import org.apache.maven.plugin.MojoExecutionException;
4 | import org.apache.maven.plugin.MojoFailureException;
5 | import org.junit.After;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 | import org.junit.runners.BlockJUnit4ClassRunner;
10 |
11 | import java.io.BufferedReader;
12 | import java.io.File;
13 | import java.io.FileInputStream;
14 | import java.io.IOException;
15 | import java.io.InputStreamReader;
16 | import java.lang.reflect.Field;
17 | import java.lang.reflect.Modifier;
18 | import java.net.URISyntaxException;
19 | import java.net.URL;
20 | import java.nio.charset.Charset;
21 | import java.nio.charset.StandardCharsets;
22 | import java.nio.file.Files;
23 | import java.nio.file.Path;
24 | import java.nio.file.Paths;
25 | import java.util.Comparator;
26 | import java.util.Objects;
27 | import java.util.stream.Stream;
28 |
29 | import static junit.framework.TestCase.assertFalse;
30 | import static junit.framework.TestCase.assertTrue;
31 |
32 | /**
33 | * These behave more like integration tests than unit tests.
34 | * They dynamically generate the folders/files, and then check that the plugin is working as expected.
35 | */
36 | @RunWith(BlockJUnit4ClassRunner.class)
37 | public class FindAndReplaceMojoTest {
38 |
39 | private final FindAndReplaceMojo findAndReplaceMojo = new FindAndReplaceMojo();
40 |
41 | private Path runningTestsPath;
42 |
43 | private Path textTestFile;
44 |
45 | private Path xmlTestFile;
46 |
47 | private Path ymlTestFile;
48 |
49 | private Path nonUtfTestFile;
50 |
51 | @Before
52 | public void setUpAll() throws NoSuchFieldException, IllegalAccessException, IOException, URISyntaxException {
53 | final URL integration = ClassLoader.getSystemResource("integration");
54 | Path integrationFolder = Paths.get(Objects.requireNonNull(
55 | integration.toURI()));
56 | runningTestsPath = Paths.get(integrationFolder.toString(), "runner");
57 |
58 | Files.createDirectory(runningTestsPath);
59 |
60 | textTestFile = Files.copy(Paths.get(integrationFolder.toString(), "test-file.txt"),
61 | Paths.get(runningTestsPath.toString(), "test-file.txt"));
62 | xmlTestFile = Files.copy(Paths.get(integrationFolder.toString(), "test-file.xml"),
63 | Paths.get(runningTestsPath.toString(), "test-file.xml"));
64 | ymlTestFile = Files.copy(Paths.get(integrationFolder.toString(), "test-file.yml"),
65 | Paths.get(runningTestsPath.toString(), "test-file.yml"));
66 | nonUtfTestFile = Files.copy(Paths.get(integrationFolder.toString(), "non-utf"),
67 | Paths.get(runningTestsPath.toString(), "non-utf"));
68 |
69 | setFieldValue(findAndReplaceMojo, "baseDir", runningTestsPath.toString());
70 |
71 | }
72 |
73 | @After
74 | public void cleanup() throws IOException {
75 | recursiveDelete(runningTestsPath);
76 | }
77 |
78 | @Test
79 | public void testDirectoryNames() throws IOException, NoSuchFieldException, IllegalAccessException,
80 | MojoExecutionException, MojoFailureException {
81 |
82 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-top-directory"));
83 | String secondDirName = "test-sub-directory";
84 | Files.createDirectories(Paths.get(firstDir.toString(), secondDirName));
85 |
86 | setFieldValue(findAndReplaceMojo, "findRegex", "-");
87 | setFieldValue(findAndReplaceMojo, "replaceValue", "_");
88 | setFieldValue(findAndReplaceMojo, "processDirectoryNames", true);
89 | setFieldValue(findAndReplaceMojo, "replacementType", "directory-names");
90 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
91 |
92 | findAndReplaceMojo.execute();
93 |
94 | Path expectedFirstDirPath = Paths.get(runningTestsPath.toString(), "test_top_directory");
95 | Path expectedSecondDirPath = Paths.get(expectedFirstDirPath.toString(), secondDirName);
96 |
97 | assertTrue(Files.exists(expectedFirstDirPath));
98 |
99 | assertTrue(Files.exists(expectedSecondDirPath));
100 |
101 | }
102 |
103 | @Test
104 | public void testDirectoryNamesReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
105 | MojoExecutionException, MojoFailureException {
106 |
107 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-top-directory"));
108 | String secondDirName = "test-sub-directory";
109 | Files.createDirectories(Paths.get(firstDir.toString(), secondDirName));
110 |
111 | setFieldValue(findAndReplaceMojo, "findRegex", "-");
112 | setFieldValue(findAndReplaceMojo, "replaceValue", "_");
113 | setFieldValue(findAndReplaceMojo, "processDirectoryNames", true);
114 | setFieldValue(findAndReplaceMojo, "replacementType", "directory-names");
115 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
116 |
117 | findAndReplaceMojo.execute();
118 |
119 | Path expectedFirstDirPath = Paths.get(runningTestsPath.toString(), "test_top-directory");
120 | Path expectedSecondDirPath = Paths.get(expectedFirstDirPath.toString(), secondDirName);
121 |
122 | assertTrue(Files.exists(expectedFirstDirPath));
123 |
124 | assertTrue(Files.exists(expectedSecondDirPath));
125 |
126 | }
127 |
128 | @Test
129 | public void testDirectoryNamesRecursive() throws IOException, NoSuchFieldException, IllegalAccessException,
130 | MojoExecutionException, MojoFailureException {
131 |
132 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-top-directory"));
133 | String secondDirName = "test-sub-directory";
134 | Files.createDirectories(Paths.get(firstDir.toString(), secondDirName));
135 |
136 | setFieldValue(findAndReplaceMojo, "findRegex", "-");
137 | setFieldValue(findAndReplaceMojo, "replaceValue", "_");
138 | setFieldValue(findAndReplaceMojo, "processDirectoryNames", true);
139 | setFieldValue(findAndReplaceMojo, "replacementType", "directory-names");
140 | setFieldValue(findAndReplaceMojo, "recursive", true);
141 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
142 |
143 | findAndReplaceMojo.execute();
144 |
145 | Path expectedFirstDirPath = Paths.get(runningTestsPath.toString(), "test_top_directory");
146 | Path expectedSecondDirPath = Paths.get(expectedFirstDirPath.toString(), "test_sub_directory");
147 |
148 | assertTrue(Files.exists(expectedFirstDirPath));
149 |
150 | assertTrue(Files.exists(expectedSecondDirPath));
151 |
152 | }
153 |
154 | @Test
155 | public void testDirectoryNamesRecursiveReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
156 | MojoExecutionException, MojoFailureException {
157 |
158 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-top-directory"));
159 | String secondDirName = "test-sub-directory";
160 | Files.createDirectories(Paths.get(firstDir.toString(), secondDirName));
161 |
162 | setFieldValue(findAndReplaceMojo, "findRegex", "-");
163 | setFieldValue(findAndReplaceMojo, "replaceValue", "_");
164 | setFieldValue(findAndReplaceMojo, "processDirectoryNames", true);
165 | setFieldValue(findAndReplaceMojo, "replacementType", "directory-names");
166 | setFieldValue(findAndReplaceMojo, "recursive", true);
167 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
168 |
169 | findAndReplaceMojo.execute();
170 |
171 | Path expectedFirstDirPath = Paths.get(runningTestsPath.toString(), "test_top-directory");
172 | Path expectedSecondDirPath = Paths.get(expectedFirstDirPath.toString(), "test_sub-directory");
173 |
174 | assertTrue(Files.exists(expectedFirstDirPath));
175 |
176 | assertTrue(Files.exists(expectedSecondDirPath));
177 |
178 | }
179 |
180 | @Test
181 | public void testDirectoryNamesRecursiveExclusions() throws IOException, NoSuchFieldException, IllegalAccessException,
182 | MojoExecutionException, MojoFailureException {
183 |
184 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-top-directory"));
185 | String secondDirName = "test-sub-directory";
186 | Files.createDirectories(Paths.get(firstDir.toString(), secondDirName));
187 |
188 | setFieldValue(findAndReplaceMojo, "findRegex", "-");
189 | setFieldValue(findAndReplaceMojo, "replaceValue", "_");
190 | setFieldValue(findAndReplaceMojo, "processDirectoryNames", true);
191 | setFieldValue(findAndReplaceMojo, "replacementType", "directory-names");
192 | setFieldValue(findAndReplaceMojo, "exclusions", "-top-");
193 | setFieldValue(findAndReplaceMojo, "recursive", true);
194 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
195 |
196 | findAndReplaceMojo.execute();
197 |
198 | Path expectedSecondDirPath = Paths.get(firstDir.toString(), "test_sub_directory");
199 |
200 | assertTrue(Files.exists(firstDir));
201 |
202 | assertTrue(Files.exists(expectedSecondDirPath));
203 |
204 | }
205 |
206 | @Test
207 | public void testDirectoryNamesRecursiveExclusionsReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
208 | MojoExecutionException, MojoFailureException {
209 |
210 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-top-directory"));
211 | String secondDirName = "test-sub-directory";
212 | Files.createDirectories(Paths.get(firstDir.toString(), secondDirName));
213 |
214 | setFieldValue(findAndReplaceMojo, "findRegex", "-");
215 | setFieldValue(findAndReplaceMojo, "replaceValue", "_");
216 | setFieldValue(findAndReplaceMojo, "processDirectoryNames", true);
217 | setFieldValue(findAndReplaceMojo, "replacementType", "directory-names");
218 | setFieldValue(findAndReplaceMojo, "exclusions", "-top-");
219 | setFieldValue(findAndReplaceMojo, "recursive", true);
220 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
221 |
222 | findAndReplaceMojo.execute();
223 |
224 | Path expectedSecondDirPath = Paths.get(firstDir.toString(), "test_sub-directory");
225 |
226 | assertTrue(Files.exists(firstDir));
227 |
228 | assertTrue(Files.exists(expectedSecondDirPath));
229 |
230 | }
231 |
232 | @Test
233 | public void testFilenames() throws IOException, NoSuchFieldException, IllegalAccessException,
234 | MojoExecutionException, MojoFailureException {
235 |
236 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
237 | Files.createFile(Paths.get(runningTestsPath.toString(), "some_file_name")).toUri();
238 | Path nonRecurseFile = Files.createFile(Paths.get(firstDir.toString(), "some_file_name"));
239 |
240 | setFieldValue(findAndReplaceMojo, "findRegex", "_");
241 | setFieldValue(findAndReplaceMojo, "replaceValue", "-");
242 | setFieldValue(findAndReplaceMojo, "processFilenames", true);
243 | setFieldValue(findAndReplaceMojo, "replacementType", "filenames");
244 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
245 |
246 | findAndReplaceMojo.execute();
247 |
248 | assertTrue(Files.exists(Paths.get(runningTestsPath.toString(), "some-file-name")));
249 | assertTrue(Files.exists(nonRecurseFile));
250 |
251 | }
252 |
253 | @Test
254 | public void testFilenamesReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
255 | MojoExecutionException, MojoFailureException {
256 |
257 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
258 | Files.createFile(Paths.get(runningTestsPath.toString(), "some_file_name")).toUri();
259 | Path nonRecurseFile = Files.createFile(Paths.get(firstDir.toString(), "some_file_name"));
260 |
261 | setFieldValue(findAndReplaceMojo, "findRegex", "_");
262 | setFieldValue(findAndReplaceMojo, "replaceValue", "-");
263 | setFieldValue(findAndReplaceMojo, "processFilenames", true);
264 | setFieldValue(findAndReplaceMojo, "replacementType", "filenames");
265 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
266 |
267 | findAndReplaceMojo.execute();
268 |
269 | assertTrue(Files.exists(Paths.get(runningTestsPath.toString(), "some-file_name")));
270 | assertTrue(Files.exists(nonRecurseFile));
271 |
272 | }
273 |
274 | @Test
275 | public void testFilenamesRecursive() throws IOException, NoSuchFieldException, IllegalAccessException,
276 | MojoExecutionException, MojoFailureException {
277 |
278 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
279 | Files.createFile(Paths.get(runningTestsPath.toString(), "some_file_name")).toUri();
280 | Files.createFile(Paths.get(firstDir.toString(), "some_file_name"));
281 |
282 | setFieldValue(findAndReplaceMojo, "findRegex", "_");
283 | setFieldValue(findAndReplaceMojo, "replaceValue", "-");
284 | setFieldValue(findAndReplaceMojo, "processFilenames", true);
285 | setFieldValue(findAndReplaceMojo, "replacementType", "filenames");
286 | setFieldValue(findAndReplaceMojo, "recursive", true);
287 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
288 |
289 | findAndReplaceMojo.execute();
290 |
291 | assertTrue(Files.exists(Paths.get(runningTestsPath.toString(), "some-file-name")));
292 | assertTrue(Files.exists(Paths.get(firstDir.toString(), "some-file-name")));
293 |
294 | }
295 |
296 | @Test
297 | public void testFilenamesRecursiveReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
298 | MojoExecutionException, MojoFailureException {
299 |
300 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
301 | Files.createFile(Paths.get(runningTestsPath.toString(), "some_file_name")).toUri();
302 | Files.createFile(Paths.get(firstDir.toString(), "some_file_name"));
303 |
304 | setFieldValue(findAndReplaceMojo, "findRegex", "_");
305 | setFieldValue(findAndReplaceMojo, "replaceValue", "-");
306 | setFieldValue(findAndReplaceMojo, "processFilenames", true);
307 | setFieldValue(findAndReplaceMojo, "replacementType", "filenames");
308 | setFieldValue(findAndReplaceMojo, "recursive", true);
309 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
310 |
311 | findAndReplaceMojo.execute();
312 |
313 | assertTrue(Files.exists(Paths.get(runningTestsPath.toString(), "some-file_name")));
314 | assertTrue(Files.exists(Paths.get(firstDir.toString(), "some-file_name")));
315 |
316 | }
317 |
318 | @Test
319 | public void testFilenamesRecursiveFileMasks() throws IOException, NoSuchFieldException, IllegalAccessException,
320 | MojoExecutionException, MojoFailureException {
321 |
322 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
323 | Path secondDir = Files.createDirectories(Paths.get(firstDir.toString(), "test-sub-dir"));
324 | Path unchangedFile1 = Files.createFile(Paths.get(runningTestsPath.toString(), "some_file_name"));
325 | Path unchangedFile2 = Files.createFile(Paths.get(firstDir.toString(), "some_file_name"));
326 | Path unchangedFile3 = Files.createFile(Paths.get(secondDir.toString(), "some_file_name"));
327 | Files.createFile(Paths.get(runningTestsPath.toString(), "some_file_name.xml"));
328 | Files.createFile(Paths.get(firstDir.toString(), "some_file_name.txt"));
329 | Files.createFile(Paths.get(secondDir.toString(), "some_file_name.yml"));
330 |
331 | setFieldValue(findAndReplaceMojo, "findRegex", "_");
332 | setFieldValue(findAndReplaceMojo, "replaceValue", "-");
333 | setFieldValue(findAndReplaceMojo, "processFilenames", true);
334 | setFieldValue(findAndReplaceMojo, "replacementType", "filenames");
335 | setFieldValue(findAndReplaceMojo, "fileMask", ".xml,.txt,.yml");
336 | setFieldValue(findAndReplaceMojo, "recursive", true);
337 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
338 |
339 | findAndReplaceMojo.execute();
340 |
341 | assertTrue(Files.exists(unchangedFile1));
342 | assertTrue(Files.exists(unchangedFile2));
343 | assertTrue(Files.exists(unchangedFile3));
344 |
345 | assertTrue(Files.exists(Paths.get(runningTestsPath.toString(), "some-file-name.xml")));
346 | assertTrue(Files.exists(Paths.get(firstDir.toString(), "some-file-name.txt")));
347 | assertTrue(Files.exists(Paths.get(secondDir.toString(), "some-file-name.yml")));
348 |
349 | }
350 |
351 | @Test
352 | public void testFilenamesRecursiveFileMasksReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
353 | MojoExecutionException, MojoFailureException {
354 |
355 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
356 | Path secondDir = Files.createDirectories(Paths.get(firstDir.toString(), "test-sub-dir"));
357 | Path unchangedFile1 = Files.createFile(Paths.get(runningTestsPath.toString(), "some_file_name"));
358 | Path unchangedFile2 = Files.createFile(Paths.get(firstDir.toString(), "some_file_name"));
359 | Path unchangedFile3 = Files.createFile(Paths.get(secondDir.toString(), "some_file_name"));
360 | Files.createFile(Paths.get(runningTestsPath.toString(), "some_file_name.xml"));
361 | Files.createFile(Paths.get(firstDir.toString(), "some_file_name.txt"));
362 | Files.createFile(Paths.get(secondDir.toString(), "some_file_name.yml"));
363 |
364 | setFieldValue(findAndReplaceMojo, "findRegex", "_");
365 | setFieldValue(findAndReplaceMojo, "replaceValue", "-");
366 | setFieldValue(findAndReplaceMojo, "processFilenames", true);
367 | setFieldValue(findAndReplaceMojo, "replacementType", "filenames");
368 | setFieldValue(findAndReplaceMojo, "fileMask", ".xml,.txt,.yml");
369 | setFieldValue(findAndReplaceMojo, "recursive", true);
370 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
371 |
372 | findAndReplaceMojo.execute();
373 |
374 | assertTrue(Files.exists(unchangedFile1));
375 | assertTrue(Files.exists(unchangedFile2));
376 | assertTrue(Files.exists(unchangedFile3));
377 |
378 | assertTrue(Files.exists(Paths.get(runningTestsPath.toString(), "some-file_name.xml")));
379 | assertTrue(Files.exists(Paths.get(firstDir.toString(), "some-file_name.txt")));
380 | assertTrue(Files.exists(Paths.get(secondDir.toString(), "some-file_name.yml")));
381 |
382 | }
383 |
384 | @Test
385 | public void testFilenamesRecursiveFileMasksWithExclusions() throws IOException, NoSuchFieldException, IllegalAccessException,
386 | MojoExecutionException, MojoFailureException {
387 |
388 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
389 | Path secondDir = Files.createDirectories(Paths.get(firstDir.toString(), "test-sub-dir"));
390 | Path unchangedFile1 = Files.createFile(Paths.get(runningTestsPath.toString(), "some_file_name"));
391 | Path unchangedFile2 = Files.createFile(Paths.get(firstDir.toString(), "some_file_name"));
392 | Path unchangedFile3 = Files.createFile(Paths.get(secondDir.toString(), "some_file_name"));
393 | Files.createFile(Paths.get(runningTestsPath.toString(), "some_xml_file_name.xml"));
394 | Files.createFile(Paths.get(firstDir.toString(), "some_txt_file_name.txt"));
395 | Path unchangedExclusion = Files.createFile(Paths.get(secondDir.toString(), "some_yml_file_name.yml"));
396 |
397 | setFieldValue(findAndReplaceMojo, "findRegex", "_");
398 | setFieldValue(findAndReplaceMojo, "replaceValue", "-");
399 | setFieldValue(findAndReplaceMojo, "processFilenames", true);
400 | setFieldValue(findAndReplaceMojo, "replacementType", "filenames");
401 | setFieldValue(findAndReplaceMojo, "fileMask", ".xml,.txt,.yml");
402 | setFieldValue(findAndReplaceMojo, "exclusions", ".yml$");
403 | setFieldValue(findAndReplaceMojo, "recursive", true);
404 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
405 |
406 | findAndReplaceMojo.execute();
407 |
408 | assertTrue(Files.exists(unchangedFile1));
409 | assertTrue(Files.exists(unchangedFile2));
410 | assertTrue(Files.exists(unchangedFile3));
411 |
412 | assertTrue(Files.exists(Paths.get(runningTestsPath.toString(), "some-xml-file-name.xml")));
413 | assertTrue(Files.exists(Paths.get(firstDir.toString(), "some-txt-file-name.txt")));
414 | assertTrue(Files.exists(unchangedExclusion));
415 |
416 | }
417 |
418 | @Test
419 | public void testFilenamesRecursiveFileMasksWithExclusionsReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
420 | MojoExecutionException, MojoFailureException {
421 |
422 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
423 | Path secondDir = Files.createDirectories(Paths.get(firstDir.toString(), "test-sub-dir"));
424 | Path unchangedFile1 = Files.createFile(Paths.get(runningTestsPath.toString(), "some_file_name"));
425 | Path unchangedFile2 = Files.createFile(Paths.get(firstDir.toString(), "some_file_name"));
426 | Path unchangedFile3 = Files.createFile(Paths.get(secondDir.toString(), "some_file_name"));
427 | Files.createFile(Paths.get(runningTestsPath.toString(), "some_xml_file_name.xml"));
428 | Files.createFile(Paths.get(firstDir.toString(), "some_txt_file_name.txt"));
429 | Path unchangedExclusion = Files.createFile(Paths.get(secondDir.toString(), "some_yml_file_name.yml"));
430 |
431 | setFieldValue(findAndReplaceMojo, "findRegex", "_");
432 | setFieldValue(findAndReplaceMojo, "replaceValue", "-");
433 | setFieldValue(findAndReplaceMojo, "processFilenames", true);
434 | setFieldValue(findAndReplaceMojo, "replacementType", "filenames");
435 | setFieldValue(findAndReplaceMojo, "fileMask", ".xml,.txt,.yml");
436 | setFieldValue(findAndReplaceMojo, "exclusions", ".yml$");
437 | setFieldValue(findAndReplaceMojo, "recursive", true);
438 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
439 |
440 | findAndReplaceMojo.execute();
441 |
442 | assertTrue(Files.exists(unchangedFile1));
443 | assertTrue(Files.exists(unchangedFile2));
444 | assertTrue(Files.exists(unchangedFile3));
445 |
446 | assertTrue(Files.exists(Paths.get(runningTestsPath.toString(), "some-xml_file_name.xml")));
447 | assertTrue(Files.exists(Paths.get(firstDir.toString(), "some-txt_file_name.txt")));
448 | assertTrue(Files.exists(unchangedExclusion));
449 |
450 | }
451 |
452 | @Test
453 | public void testFileContents() throws IOException, NoSuchFieldException, IllegalAccessException,
454 | MojoExecutionException, MojoFailureException {
455 |
456 | setFieldValue(findAndReplaceMojo, "findRegex", "asdf");
457 | String replaceValue = "value successfully replaced";
458 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
459 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
460 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
461 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
462 |
463 | findAndReplaceMojo.execute();
464 |
465 | assertTrue(fileContains(textTestFile.toFile(), replaceValue));
466 | assertTrue(fileContains(xmlTestFile.toFile(), replaceValue));
467 | assertTrue(fileContains(ymlTestFile.toFile(), replaceValue));
468 |
469 | assertFalse(fileContains(textTestFile.toFile(), "asdf"));
470 | assertFalse(fileContains(xmlTestFile.toFile(), "asdf"));
471 | assertFalse(fileContains(ymlTestFile.toFile(), "asdf"));
472 |
473 | }
474 |
475 | @Test
476 | public void testFileContentsReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
477 | MojoExecutionException, MojoFailureException {
478 |
479 | setFieldValue(findAndReplaceMojo, "findRegex", "asdf");
480 | String replaceValue = "value successfully replaced";
481 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
482 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
483 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
484 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
485 |
486 | findAndReplaceMojo.execute();
487 |
488 | assertTrue(fileContains(textTestFile.toFile(), replaceValue));
489 | assertTrue(fileContains(xmlTestFile.toFile(), replaceValue));
490 | assertTrue(fileContains(ymlTestFile.toFile(), replaceValue));
491 |
492 | assertTrue(fileContains(textTestFile.toFile(), "asdf"));
493 | assertTrue(fileContains(xmlTestFile.toFile(), "asdf"));
494 | assertTrue(fileContains(ymlTestFile.toFile(), "asdf"));
495 |
496 | }
497 |
498 | @Test
499 | public void testFileContentsNonStandardEncodingFindRegex() throws IOException, NoSuchFieldException, IllegalAccessException,
500 | MojoExecutionException, MojoFailureException {
501 |
502 | // Replace non-standard with standard
503 | setFieldValue(findAndReplaceMojo, "findRegex", "ìíîï");
504 | String replaceValue = "value successfully replaced";
505 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
506 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
507 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
508 | setFieldValue(findAndReplaceMojo, "encoding", "ISO-8859-1");
509 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
510 |
511 | findAndReplaceMojo.execute();
512 |
513 | assertTrue(fileContains(nonUtfTestFile.toFile(), replaceValue, StandardCharsets.ISO_8859_1));
514 | assertFalse(fileContains(nonUtfTestFile.toFile(), "àáâãäåæçèéêëìíîï", StandardCharsets.ISO_8859_1));
515 |
516 | }
517 |
518 | @Test
519 | public void testFileContentsNonStandardEncodingFindRegexReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
520 | MojoExecutionException, MojoFailureException {
521 |
522 | // Replace non-standard with standard
523 | setFieldValue(findAndReplaceMojo, "findRegex", "ìíîï");
524 | String replaceValue = "value successfully replaced";
525 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
526 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
527 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
528 | setFieldValue(findAndReplaceMojo, "encoding", "ISO-8859-1");
529 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
530 |
531 | findAndReplaceMojo.execute();
532 |
533 | assertTrue(fileContains(nonUtfTestFile.toFile(), replaceValue, StandardCharsets.ISO_8859_1));
534 | assertTrue(fileContains(nonUtfTestFile.toFile(), "àáâãäåæçèéêëìíîï", StandardCharsets.ISO_8859_1));
535 |
536 | }
537 |
538 | @Test
539 | public void testFileContentsNonStandardReplaceValue() throws IOException, NoSuchFieldException, IllegalAccessException,
540 | MojoExecutionException, MojoFailureException {
541 |
542 | // Replace standard with non-standard
543 | setFieldValue(findAndReplaceMojo, "findRegex", "test");
544 | String replaceValue = "çåæ";
545 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
546 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
547 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
548 | setFieldValue(findAndReplaceMojo, "encoding", "ISO-8859-1");
549 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
550 |
551 | findAndReplaceMojo.execute();
552 |
553 | assertTrue(fileContains(nonUtfTestFile.toFile(), replaceValue, StandardCharsets.ISO_8859_1));
554 | assertFalse(fileContains(nonUtfTestFile.toFile(), "test", StandardCharsets.ISO_8859_1));
555 |
556 | }
557 |
558 | @Test
559 | public void testFileContentsNonStandardReplaceValueReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
560 | MojoExecutionException, MojoFailureException {
561 |
562 | // Replace standard with non-standard
563 | setFieldValue(findAndReplaceMojo, "findRegex", "test");
564 | String replaceValue = "çåæ";
565 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
566 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
567 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
568 | setFieldValue(findAndReplaceMojo, "encoding", "ISO-8859-1");
569 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
570 |
571 | findAndReplaceMojo.execute();
572 |
573 | assertTrue(fileContains(nonUtfTestFile.toFile(), replaceValue, StandardCharsets.ISO_8859_1));
574 | assertTrue(fileContains(nonUtfTestFile.toFile(), "test", StandardCharsets.ISO_8859_1));
575 |
576 | }
577 |
578 | @Test
579 | public void testFileContentsNonStandardFindAndReplaceValue() throws IOException, NoSuchFieldException, IllegalAccessException,
580 | MojoExecutionException, MojoFailureException {
581 |
582 | // Replace non-standard with non-standard
583 | setFieldValue(findAndReplaceMojo, "findRegex", "àáâãäåæçèéêëìíîï");
584 | String replaceValue = "çåæìíîïàáâãäå";
585 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
586 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
587 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
588 | setFieldValue(findAndReplaceMojo, "encoding", "ISO-8859-1");
589 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
590 |
591 | findAndReplaceMojo.execute();
592 |
593 | assertTrue(fileContains(nonUtfTestFile.toFile(), replaceValue, StandardCharsets.ISO_8859_1));
594 | assertFalse(fileContains(nonUtfTestFile.toFile(), "àáâãäåæçèéêëìíîï", StandardCharsets.ISO_8859_1));
595 |
596 | }
597 |
598 | @Test
599 | public void testFileContentsNonStandardFindAndReplaceValueReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
600 | MojoExecutionException, MojoFailureException {
601 |
602 | // Replace non-standard with non-standard
603 | setFieldValue(findAndReplaceMojo, "findRegex", "àáâãäåæçèéêëìíîï");
604 | String replaceValue = "çåæìíîïàáâãäå";
605 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
606 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
607 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
608 | setFieldValue(findAndReplaceMojo, "encoding", "ISO-8859-1");
609 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
610 |
611 | findAndReplaceMojo.execute();
612 |
613 | assertTrue(fileContains(nonUtfTestFile.toFile(), replaceValue, StandardCharsets.ISO_8859_1));
614 | assertTrue(fileContains(nonUtfTestFile.toFile(), "àáâãäåæçèéêëìíîï", StandardCharsets.ISO_8859_1));
615 |
616 | }
617 |
618 | @Test
619 | public void testFileContentsRecursive() throws IOException, NoSuchFieldException, IllegalAccessException,
620 | MojoExecutionException, MojoFailureException {
621 |
622 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
623 | Path testFileXmlMoved = Files.copy(xmlTestFile, Paths.get(firstDir.toString(), xmlTestFile.toFile().getName()));
624 | Path testFileYmlMoved = Files.copy(ymlTestFile, Paths.get(firstDir.toString(), ymlTestFile.toFile().getName()));
625 | Path testFileTxtMoved = Files.copy(textTestFile, Paths.get(firstDir.toString(), textTestFile.toFile().getName()));
626 |
627 | setFieldValue(findAndReplaceMojo, "findRegex", "asdf");
628 | String replaceValue = "value successfully replaced";
629 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
630 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
631 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
632 | setFieldValue(findAndReplaceMojo, "recursive", true);
633 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
634 |
635 | findAndReplaceMojo.execute();
636 |
637 | assertTrue(fileContains(textTestFile.toFile(), replaceValue));
638 | assertTrue(fileContains(xmlTestFile.toFile(), replaceValue));
639 | assertTrue(fileContains(ymlTestFile.toFile(), replaceValue));
640 | assertTrue(fileContains(testFileXmlMoved.toFile(), replaceValue));
641 | assertTrue(fileContains(testFileYmlMoved.toFile(), replaceValue));
642 | assertTrue(fileContains(testFileTxtMoved.toFile(), replaceValue));
643 |
644 | assertFalse(fileContains(textTestFile.toFile(), "asdf"));
645 | assertFalse(fileContains(xmlTestFile.toFile(), "asdf"));
646 | assertFalse(fileContains(ymlTestFile.toFile(), "asdf"));
647 | assertFalse(fileContains(testFileXmlMoved.toFile(), "asdf"));
648 | assertFalse(fileContains(testFileYmlMoved.toFile(), "asdf"));
649 | assertFalse(fileContains(testFileTxtMoved.toFile(), "asdf"));
650 |
651 | }
652 |
653 | @Test
654 | public void testFileContentsRecursiveReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
655 | MojoExecutionException, MojoFailureException {
656 |
657 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
658 | Path testFileXmlMoved = Files.copy(xmlTestFile, Paths.get(firstDir.toString(), xmlTestFile.toFile().getName()));
659 | Path testFileYmlMoved = Files.copy(ymlTestFile, Paths.get(firstDir.toString(), ymlTestFile.toFile().getName()));
660 | Path testFileTxtMoved = Files.copy(textTestFile, Paths.get(firstDir.toString(), textTestFile.toFile().getName()));
661 |
662 | setFieldValue(findAndReplaceMojo, "findRegex", "asdf");
663 | String replaceValue = "value successfully replaced";
664 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
665 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
666 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
667 | setFieldValue(findAndReplaceMojo, "recursive", true);
668 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
669 |
670 | findAndReplaceMojo.execute();
671 |
672 | assertTrue(fileContains(textTestFile.toFile(), replaceValue));
673 | assertTrue(fileContains(xmlTestFile.toFile(), replaceValue));
674 | assertTrue(fileContains(ymlTestFile.toFile(), replaceValue));
675 | assertTrue(fileContains(testFileXmlMoved.toFile(), replaceValue));
676 | assertTrue(fileContains(testFileYmlMoved.toFile(), replaceValue));
677 | assertTrue(fileContains(testFileTxtMoved.toFile(), replaceValue));
678 |
679 | assertTrue(fileContains(textTestFile.toFile(), "asdf"));
680 | assertTrue(fileContains(xmlTestFile.toFile(), "asdf"));
681 | assertTrue(fileContains(ymlTestFile.toFile(), "asdf"));
682 | assertTrue(fileContains(testFileXmlMoved.toFile(), "asdf"));
683 | assertTrue(fileContains(testFileYmlMoved.toFile(), "asdf"));
684 | assertTrue(fileContains(testFileTxtMoved.toFile(), "asdf"));
685 |
686 | }
687 |
688 | @Test
689 | public void testFileContentsNotRecursive() throws IOException, NoSuchFieldException, IllegalAccessException,
690 | MojoExecutionException, MojoFailureException {
691 |
692 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
693 | Path testFileXmlMoved = Files.copy(xmlTestFile, Paths.get(firstDir.toString(), xmlTestFile.toFile().getName()));
694 | Path testFileYmlMoved = Files.copy(ymlTestFile, Paths.get(firstDir.toString(), ymlTestFile.toFile().getName()));
695 | Path testFileTxtMoved = Files.copy(textTestFile, Paths.get(firstDir.toString(), textTestFile.toFile().getName()));
696 |
697 | setFieldValue(findAndReplaceMojo, "findRegex", "asdf");
698 | String replaceValue = "value successfully replaced";
699 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
700 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
701 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
702 | setFieldValue(findAndReplaceMojo, "recursive", false);
703 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
704 |
705 | findAndReplaceMojo.execute();
706 |
707 | assertTrue(fileContains(textTestFile.toFile(), replaceValue));
708 | assertTrue(fileContains(xmlTestFile.toFile(), replaceValue));
709 | assertTrue(fileContains(ymlTestFile.toFile(), replaceValue));
710 | assertFalse(fileContains(testFileXmlMoved.toFile(), replaceValue));
711 | assertFalse(fileContains(testFileYmlMoved.toFile(), replaceValue));
712 | assertFalse(fileContains(testFileTxtMoved.toFile(), replaceValue));
713 |
714 | assertFalse(fileContains(textTestFile.toFile(), "asdf"));
715 | assertFalse(fileContains(xmlTestFile.toFile(), "asdf"));
716 | assertFalse(fileContains(ymlTestFile.toFile(), "asdf"));
717 | assertTrue(fileContains(testFileXmlMoved.toFile(), "asdf"));
718 | assertTrue(fileContains(testFileYmlMoved.toFile(), "asdf"));
719 | assertTrue(fileContains(testFileTxtMoved.toFile(), "asdf"));
720 |
721 | }
722 |
723 | @Test
724 | public void testFileContentsNotRecursiveReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
725 | MojoExecutionException, MojoFailureException {
726 |
727 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
728 | Path testFileXmlMoved = Files.copy(xmlTestFile, Paths.get(firstDir.toString(), xmlTestFile.toFile().getName()));
729 | Path testFileYmlMoved = Files.copy(ymlTestFile, Paths.get(firstDir.toString(), ymlTestFile.toFile().getName()));
730 | Path testFileTxtMoved = Files.copy(textTestFile, Paths.get(firstDir.toString(), textTestFile.toFile().getName()));
731 |
732 | setFieldValue(findAndReplaceMojo, "findRegex", "asdf");
733 | String replaceValue = "value successfully replaced";
734 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
735 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
736 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
737 | setFieldValue(findAndReplaceMojo, "recursive", false);
738 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
739 |
740 | findAndReplaceMojo.execute();
741 |
742 | assertTrue(fileContains(textTestFile.toFile(), replaceValue));
743 | assertTrue(fileContains(xmlTestFile.toFile(), replaceValue));
744 | assertTrue(fileContains(ymlTestFile.toFile(), replaceValue));
745 | assertFalse(fileContains(testFileXmlMoved.toFile(), replaceValue));
746 | assertFalse(fileContains(testFileYmlMoved.toFile(), replaceValue));
747 | assertFalse(fileContains(testFileTxtMoved.toFile(), replaceValue));
748 |
749 | assertTrue(fileContains(textTestFile.toFile(), "asdf"));
750 | assertTrue(fileContains(xmlTestFile.toFile(), "asdf"));
751 | assertTrue(fileContains(ymlTestFile.toFile(), "asdf"));
752 | assertTrue(fileContains(testFileXmlMoved.toFile(), "asdf"));
753 | assertTrue(fileContains(testFileYmlMoved.toFile(), "asdf"));
754 | assertTrue(fileContains(testFileTxtMoved.toFile(), "asdf"));
755 |
756 | }
757 |
758 | @Test
759 | public void testFileContentsRecursiveFileMasks() throws IOException, NoSuchFieldException, IllegalAccessException,
760 | MojoExecutionException, MojoFailureException {
761 |
762 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
763 | Path testFileXmlMoved = Files.copy(xmlTestFile, Paths.get(firstDir.toString(), xmlTestFile.toFile().getName()));
764 | Path testFileYmlMoved = Files.copy(ymlTestFile, Paths.get(firstDir.toString(), ymlTestFile.toFile().getName()));
765 | Path testFileTxtMoved = Files.copy(textTestFile, Paths.get(firstDir.toString(), textTestFile.toFile().getName()));
766 |
767 | setFieldValue(findAndReplaceMojo, "findRegex", "asdf");
768 | String replaceValue = "value successfully replaced";
769 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
770 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
771 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
772 | setFieldValue(findAndReplaceMojo, "recursive", true);
773 | setFieldValue(findAndReplaceMojo, "fileMask", ".xml,.yml");
774 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
775 |
776 | findAndReplaceMojo.execute();
777 |
778 | assertFalse(fileContains(textTestFile.toFile(), replaceValue));
779 | assertTrue(fileContains(xmlTestFile.toFile(), replaceValue));
780 | assertTrue(fileContains(ymlTestFile.toFile(), replaceValue));
781 | assertTrue(fileContains(testFileXmlMoved.toFile(), replaceValue));
782 | assertTrue(fileContains(testFileYmlMoved.toFile(), replaceValue));
783 | assertFalse(fileContains(testFileTxtMoved.toFile(), replaceValue));
784 |
785 | assertTrue(fileContains(textTestFile.toFile(), "asdf"));
786 | assertFalse(fileContains(xmlTestFile.toFile(), "asdf"));
787 | assertFalse(fileContains(ymlTestFile.toFile(), "asdf"));
788 | assertFalse(fileContains(testFileXmlMoved.toFile(), "asdf"));
789 | assertFalse(fileContains(testFileYmlMoved.toFile(), "asdf"));
790 | assertTrue(fileContains(testFileTxtMoved.toFile(), "asdf"));
791 |
792 | }
793 |
794 | @Test
795 | public void testFileContentsRecursiveFileMasksReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
796 | MojoExecutionException, MojoFailureException {
797 |
798 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
799 | Path testFileXmlMoved = Files.copy(xmlTestFile, Paths.get(firstDir.toString(), xmlTestFile.toFile().getName()));
800 | Path testFileYmlMoved = Files.copy(ymlTestFile, Paths.get(firstDir.toString(), ymlTestFile.toFile().getName()));
801 | Path testFileTxtMoved = Files.copy(textTestFile, Paths.get(firstDir.toString(), textTestFile.toFile().getName()));
802 |
803 | setFieldValue(findAndReplaceMojo, "findRegex", "asdf");
804 | String replaceValue = "value successfully replaced";
805 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
806 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
807 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
808 | setFieldValue(findAndReplaceMojo, "recursive", true);
809 | setFieldValue(findAndReplaceMojo, "fileMask", ".xml,.yml");
810 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
811 |
812 | findAndReplaceMojo.execute();
813 |
814 | assertFalse(fileContains(textTestFile.toFile(), replaceValue));
815 | assertTrue(fileContains(xmlTestFile.toFile(), replaceValue));
816 | assertTrue(fileContains(ymlTestFile.toFile(), replaceValue));
817 | assertTrue(fileContains(testFileXmlMoved.toFile(), replaceValue));
818 | assertTrue(fileContains(testFileYmlMoved.toFile(), replaceValue));
819 | assertFalse(fileContains(testFileTxtMoved.toFile(), replaceValue));
820 |
821 | assertTrue(fileContains(textTestFile.toFile(), "asdf"));
822 | assertTrue(fileContains(xmlTestFile.toFile(), "asdf"));
823 | assertTrue(fileContains(ymlTestFile.toFile(), "asdf"));
824 | assertTrue(fileContains(testFileXmlMoved.toFile(), "asdf"));
825 | assertTrue(fileContains(testFileYmlMoved.toFile(), "asdf"));
826 | assertTrue(fileContains(testFileTxtMoved.toFile(), "asdf"));
827 |
828 | }
829 |
830 | @Test
831 | public void testFileContentsRecursiveFileMasksExclusions() throws IOException, NoSuchFieldException, IllegalAccessException,
832 | MojoExecutionException, MojoFailureException {
833 |
834 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
835 | Path testFileXmlMoved = Files.copy(xmlTestFile, Paths.get(firstDir.toString(), xmlTestFile.toFile().getName()));
836 | Path testFileYmlMoved = Files.copy(ymlTestFile, Paths.get(firstDir.toString(), ymlTestFile.toFile().getName()));
837 | Path testFileTxtMoved = Files.copy(textTestFile, Paths.get(firstDir.toString(), textTestFile.toFile().getName()));
838 |
839 | setFieldValue(findAndReplaceMojo, "findRegex", "asdf");
840 | String replaceValue = "value successfully replaced";
841 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
842 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
843 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
844 | setFieldValue(findAndReplaceMojo, "recursive", true);
845 | setFieldValue(findAndReplaceMojo, "fileMask", ".xml,.yml");
846 | setFieldValue(findAndReplaceMojo, "exclusions", ".yml$");
847 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
848 |
849 | findAndReplaceMojo.execute();
850 |
851 | assertFalse(fileContains(textTestFile.toFile(), replaceValue));
852 | assertTrue(fileContains(xmlTestFile.toFile(), replaceValue));
853 | assertFalse(fileContains(ymlTestFile.toFile(), replaceValue));
854 | assertTrue(fileContains(testFileXmlMoved.toFile(), replaceValue));
855 | assertFalse(fileContains(testFileYmlMoved.toFile(), replaceValue));
856 | assertFalse(fileContains(testFileTxtMoved.toFile(), replaceValue));
857 |
858 | assertTrue(fileContains(textTestFile.toFile(), "asdf"));
859 | assertFalse(fileContains(xmlTestFile.toFile(), "asdf"));
860 | assertTrue(fileContains(ymlTestFile.toFile(), "asdf"));
861 | assertFalse(fileContains(testFileXmlMoved.toFile(), "asdf"));
862 | assertTrue(fileContains(testFileYmlMoved.toFile(), "asdf"));
863 | assertTrue(fileContains(testFileTxtMoved.toFile(), "asdf"));
864 |
865 | }
866 |
867 | @Test
868 | public void testFileContentsRecursiveFileMasksExclusionsReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
869 | MojoExecutionException, MojoFailureException {
870 |
871 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
872 | Path testFileXmlMoved = Files.copy(xmlTestFile, Paths.get(firstDir.toString(), xmlTestFile.toFile().getName()));
873 | Path testFileYmlMoved = Files.copy(ymlTestFile, Paths.get(firstDir.toString(), ymlTestFile.toFile().getName()));
874 | Path testFileTxtMoved = Files.copy(textTestFile, Paths.get(firstDir.toString(), textTestFile.toFile().getName()));
875 |
876 | setFieldValue(findAndReplaceMojo, "findRegex", "asdf");
877 | String replaceValue = "value successfully replaced";
878 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
879 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
880 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents");
881 | setFieldValue(findAndReplaceMojo, "recursive", true);
882 | setFieldValue(findAndReplaceMojo, "fileMask", ".xml,.yml");
883 | setFieldValue(findAndReplaceMojo, "exclusions", ".yml$");
884 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
885 |
886 | findAndReplaceMojo.execute();
887 |
888 | assertFalse(fileContains(textTestFile.toFile(), replaceValue));
889 | assertTrue(fileContains(xmlTestFile.toFile(), replaceValue));
890 | assertFalse(fileContains(ymlTestFile.toFile(), replaceValue));
891 | assertTrue(fileContains(testFileXmlMoved.toFile(), replaceValue));
892 | assertFalse(fileContains(testFileYmlMoved.toFile(), replaceValue));
893 | assertFalse(fileContains(testFileTxtMoved.toFile(), replaceValue));
894 |
895 | assertTrue(fileContains(textTestFile.toFile(), "asdf"));
896 | assertTrue(fileContains(xmlTestFile.toFile(), "asdf"));
897 | assertTrue(fileContains(ymlTestFile.toFile(), "asdf"));
898 | assertTrue(fileContains(testFileXmlMoved.toFile(), "asdf"));
899 | assertTrue(fileContains(testFileYmlMoved.toFile(), "asdf"));
900 | assertTrue(fileContains(testFileTxtMoved.toFile(), "asdf"));
901 |
902 | }
903 |
904 | @Test
905 | public void testEverything() throws IOException, NoSuchFieldException, IllegalAccessException,
906 | MojoExecutionException, MojoFailureException {
907 |
908 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-directory"));
909 | Path secondDir = Files.createDirectory(Paths.get(firstDir.toString(), "test-sub-directory"));
910 | Files.copy(xmlTestFile, Paths.get(firstDir.toString(), xmlTestFile.toFile().getName()));
911 | Files.copy(ymlTestFile, Paths.get(firstDir.toString(), ymlTestFile.toFile().getName()));
912 | Files.copy(textTestFile, Paths.get(firstDir.toString(), textTestFile.toFile().getName()));
913 | Files.copy(xmlTestFile, Paths.get(secondDir.toString(), xmlTestFile.toFile().getName()));
914 | Files.copy(ymlTestFile, Paths.get(secondDir.toString(), ymlTestFile.toFile().getName()));
915 | Files.copy(textTestFile, Paths.get(secondDir.toString(), textTestFile.toFile().getName()));
916 |
917 | setFieldValue(findAndReplaceMojo, "findRegex", "test-");
918 | String replaceValue = "rep-";
919 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
920 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
921 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents,filenames,directory-names");
922 | setFieldValue(findAndReplaceMojo, "recursive", true);
923 | // Only xml files should be processed
924 | setFieldValue(findAndReplaceMojo, "fileMask", ".xml,.yml");
925 | setFieldValue(findAndReplaceMojo, "exclusions", ".yml$");
926 | setFieldValue(findAndReplaceMojo, "replaceAll", true);
927 |
928 | findAndReplaceMojo.execute();
929 |
930 | Path firstDirRenamed = Paths.get(runningTestsPath.toString(), "rep-directory");
931 | Path secondDirRenamed = Paths.get(firstDirRenamed.toString(), "rep-sub-directory");
932 | Path firstXmlFile = Paths.get(runningTestsPath.toString(), "rep-file.xml");
933 | Path firstTxtFile = Paths.get(runningTestsPath.toString(), "test-file.txt");
934 | Path firstYmlFile = Paths.get(runningTestsPath.toString(), "test-file.yml");
935 | Path secondXmlFile = Paths.get(firstDirRenamed.toString(), "rep-file.xml");
936 | Path secondTxtFile = Paths.get(firstDirRenamed.toString(), "test-file.txt");
937 | Path secondYmlFile = Paths.get(firstDirRenamed.toString(), "test-file.yml");
938 | Path thirdXmlFile = Paths.get(secondDirRenamed.toString(), "rep-file.xml");
939 | Path thirdTxtFile = Paths.get(secondDirRenamed.toString(), "test-file.txt");
940 | Path thirdYmlFile = Paths.get(secondDirRenamed.toString(), "test-file.yml");
941 |
942 | // Assert root dir
943 | assertTrue(Files.exists(firstXmlFile));
944 | assertTrue(fileContains(firstXmlFile.toFile(), replaceValue));
945 | assertFalse(fileContains(firstXmlFile.toFile(), "test-"));
946 | assertTrue(Files.exists(firstTxtFile));
947 | assertFalse(fileContains(firstTxtFile.toFile(), replaceValue));
948 | assertTrue(fileContains(firstTxtFile.toFile(), "test-"));
949 | assertTrue(Files.exists(firstYmlFile));
950 | assertFalse(fileContains(firstYmlFile.toFile(), replaceValue));
951 | assertTrue(fileContains(firstYmlFile.toFile(), "test-"));
952 |
953 | // Assert first dir
954 | assertTrue(Files.exists(firstDirRenamed));
955 | assertTrue(Files.exists(secondXmlFile));
956 | assertTrue(fileContains(secondXmlFile.toFile(), replaceValue));
957 | assertFalse(fileContains(secondXmlFile.toFile(), "test-"));
958 | assertTrue(Files.exists(secondTxtFile));
959 | assertFalse(fileContains(secondTxtFile.toFile(), replaceValue));
960 | assertTrue(fileContains(secondTxtFile.toFile(), "test-"));
961 | assertTrue(Files.exists(secondYmlFile));
962 | assertFalse(fileContains(secondYmlFile.toFile(), replaceValue));
963 | assertTrue(fileContains(secondYmlFile.toFile(), "test-"));
964 |
965 | // Assert second dir
966 | assertTrue(Files.exists(secondDirRenamed));
967 | assertTrue(Files.exists(thirdXmlFile));
968 | assertTrue(fileContains(thirdXmlFile.toFile(), replaceValue));
969 | assertFalse(fileContains(thirdXmlFile.toFile(), "test-"));
970 | assertTrue(Files.exists(thirdTxtFile));
971 | assertFalse(fileContains(thirdTxtFile.toFile(), replaceValue));
972 | assertTrue(fileContains(thirdTxtFile.toFile(), "test-"));
973 | assertTrue(Files.exists(thirdYmlFile));
974 | assertFalse(fileContains(thirdYmlFile.toFile(), replaceValue));
975 | assertTrue(fileContains(thirdYmlFile.toFile(), "test-"));
976 |
977 | }
978 |
979 | @Test
980 | public void testEverythingReplaceFirst() throws IOException, NoSuchFieldException, IllegalAccessException,
981 | MojoExecutionException, MojoFailureException {
982 |
983 | Path firstDir = Files.createDirectory(Paths.get(runningTestsPath.toString(), "test-test-directory"));
984 | Path secondDir = Files.createDirectory(Paths.get(firstDir.toString(), "test-test-sub-directory"));
985 | Files.copy(xmlTestFile, Paths.get(firstDir.toString(), "test-" + xmlTestFile.toFile().getName()));
986 | Files.copy(ymlTestFile, Paths.get(firstDir.toString(), "test-" + ymlTestFile.toFile().getName()));
987 | Files.copy(textTestFile, Paths.get(firstDir.toString(), "test-" + textTestFile.toFile().getName()));
988 | Files.copy(xmlTestFile, Paths.get(secondDir.toString(), "test-" + xmlTestFile.toFile().getName()));
989 | Files.copy(ymlTestFile, Paths.get(secondDir.toString(), "test-" + ymlTestFile.toFile().getName()));
990 | Files.copy(textTestFile, Paths.get(secondDir.toString(), "test-" + textTestFile.toFile().getName()));
991 |
992 | setFieldValue(findAndReplaceMojo, "findRegex", "test-");
993 | String replaceValue = "rep-";
994 | setFieldValue(findAndReplaceMojo, "replaceValue", replaceValue);
995 | setFieldValue(findAndReplaceMojo, "processFileContents", true);
996 | setFieldValue(findAndReplaceMojo, "replacementType", "file-contents,filenames,directory-names");
997 | setFieldValue(findAndReplaceMojo, "recursive", true);
998 | // Only xml files should be processed
999 | setFieldValue(findAndReplaceMojo, "fileMask", ".xml,.yml");
1000 | setFieldValue(findAndReplaceMojo, "exclusions", ".yml$");
1001 | setFieldValue(findAndReplaceMojo, "replaceAll", false);
1002 |
1003 | findAndReplaceMojo.execute();
1004 |
1005 | Path firstDirRenamed = Paths.get(runningTestsPath.toString(), "rep-test-directory");
1006 | Path secondDirRenamed = Paths.get(firstDirRenamed.toString(), "rep-test-sub-directory");
1007 | Path firstXmlFile = Paths.get(runningTestsPath.toString(), "rep-file.xml");
1008 | Path firstTxtFile = Paths.get(runningTestsPath.toString(), "test-file.txt");
1009 | Path firstYmlFile = Paths.get(runningTestsPath.toString(), "test-file.yml");
1010 | Path secondXmlFile = Paths.get(firstDirRenamed.toString(), "rep-test-file.xml");
1011 | Path secondTxtFile = Paths.get(firstDirRenamed.toString(), "test-test-file.txt");
1012 | Path secondYmlFile = Paths.get(firstDirRenamed.toString(), "test-test-file.yml");
1013 | Path thirdXmlFile = Paths.get(secondDirRenamed.toString(), "rep-test-file.xml");
1014 | Path thirdTxtFile = Paths.get(secondDirRenamed.toString(), "test-test-file.txt");
1015 | Path thirdYmlFile = Paths.get(secondDirRenamed.toString(), "test-test-file.yml");
1016 |
1017 | // Assert root dir
1018 | assertTrue(Files.exists(firstXmlFile));
1019 | assertTrue(fileContains(firstXmlFile.toFile(), replaceValue));
1020 | assertTrue(fileContains(firstXmlFile.toFile(), "test-"));
1021 | assertTrue(Files.exists(firstTxtFile));
1022 | assertFalse(fileContains(firstTxtFile.toFile(), replaceValue));
1023 | assertTrue(fileContains(firstXmlFile.toFile(), "test-"));
1024 | assertTrue(Files.exists(firstYmlFile));
1025 | assertFalse(fileContains(firstYmlFile.toFile(), replaceValue));
1026 | assertTrue(fileContains(firstXmlFile.toFile(), "test-"));
1027 |
1028 | // Assert first dir
1029 | assertTrue(Files.exists(firstDirRenamed));
1030 | assertTrue(Files.exists(secondXmlFile));
1031 | assertTrue(fileContains(secondXmlFile.toFile(), replaceValue));
1032 | assertTrue(fileContains(secondXmlFile.toFile(), "test-"));
1033 | assertTrue(Files.exists(secondTxtFile));
1034 | assertFalse(fileContains(secondTxtFile.toFile(), replaceValue));
1035 | assertTrue(fileContains(secondTxtFile.toFile(), "test-"));
1036 | assertTrue(Files.exists(secondYmlFile));
1037 | assertFalse(fileContains(secondYmlFile.toFile(), replaceValue));
1038 | assertTrue(fileContains(secondYmlFile.toFile(), "test-"));
1039 |
1040 | // Assert second dir
1041 | assertTrue(Files.exists(secondDirRenamed));
1042 | assertTrue(Files.exists(thirdXmlFile));
1043 | assertTrue(fileContains(thirdXmlFile.toFile(), replaceValue));
1044 | assertTrue(fileContains(thirdXmlFile.toFile(), "test-"));
1045 | assertTrue(Files.exists(thirdTxtFile));
1046 | assertFalse(fileContains(thirdTxtFile.toFile(), replaceValue));
1047 | assertTrue(fileContains(thirdTxtFile.toFile(), "test-"));
1048 | assertTrue(Files.exists(thirdYmlFile));
1049 | assertFalse(fileContains(thirdYmlFile.toFile(), replaceValue));
1050 | assertTrue(fileContains(thirdYmlFile.toFile(), "test-"));
1051 |
1052 | }
1053 |
1054 | private void setFieldValue(Object obj, String fieldName, Object val)
1055 | throws NoSuchFieldException, IllegalAccessException {
1056 | Field field = obj.getClass().getDeclaredField(fieldName);
1057 | field.setAccessible(true);
1058 | field.set(obj, val);
1059 | }
1060 |
1061 | private void recursiveDelete(Path pathToBeDeleted) throws IOException {
1062 |
1063 | Files.walk(pathToBeDeleted)
1064 | .sorted(Comparator.reverseOrder())
1065 | .map(Path::toFile)
1066 | .forEach(File::delete);
1067 | }
1068 |
1069 | private boolean fileContains(File file, String findValue) throws IOException {
1070 | return fileContains(file, findValue, Charset.defaultCharset());
1071 | }
1072 |
1073 | private boolean fileContains(File file, String findValue, Charset charset) throws IOException {
1074 |
1075 | try (FileInputStream fis = new FileInputStream(file);
1076 | InputStreamReader isr = new InputStreamReader(fis, charset);
1077 | BufferedReader fileReader = new BufferedReader(isr)) {
1078 |
1079 | Stream lines = fileReader.lines();
1080 |
1081 | return lines.anyMatch(line -> line.contains(findValue));
1082 | }
1083 | }
1084 |
1085 | }
1086 |
--------------------------------------------------------------------------------
/src/test/resources/integration/non-utf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/floverfelt/find-and-replace-maven-plugin/430e33f63689e6e727f8ef865bac38821c711a8b/src/test/resources/integration/non-utf
--------------------------------------------------------------------------------
/src/test/resources/integration/test-file.txt:
--------------------------------------------------------------------------------
1 | Placeholder file(s) used during integration testing for altering file contents.
2 |
3 | asdf
4 |
5 | -test-
6 |
7 | asdf
--------------------------------------------------------------------------------
/src/test/resources/integration/test-file.xml:
--------------------------------------------------------------------------------
1 |
2 | 1.0-SNAPSHOT
3 | asdf
4 | -test-
5 | -test-
6 | asdf
7 |
--------------------------------------------------------------------------------
/src/test/resources/integration/test-file.yml:
--------------------------------------------------------------------------------
1 | some:
2 | value:
3 | in:
4 | yml: -test-
5 | other: asdf
6 | prop: asdf
--------------------------------------------------------------------------------