Skip to content

Commit aa48b8d

Browse files
committed
Allow replacement to be null for ReplaceRegex of plugin maven
1 parent 7667a84 commit aa48b8d

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,10 +35,12 @@ public class Replace implements FormatterStepFactory {
3535

3636
@Override
3737
public FormatterStep newFormatterStep(FormatterStepConfig config) {
38-
if (name == null || search == null || replacement == null) {
39-
throw new IllegalArgumentException("Must specify 'name', 'search' and 'replacement'.");
38+
if (name == null || search == null) {
39+
throw new IllegalArgumentException("Must specify 'name' and 'search'.");
4040
}
41-
42-
return ReplaceStep.create(name, search, replacement);
41+
// Use empty string if replacement is not provided. In pom.xml there is no way to specify
42+
// an empty string as a property value as maven will always trim the value and if it is
43+
// empty, maven will consider the property as not provided.
44+
return ReplaceStep.create(name, search, replacement != null ? replacement : "");
4345
}
4446
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,10 +35,12 @@ public class ReplaceRegex implements FormatterStepFactory {
3535

3636
@Override
3737
public FormatterStep newFormatterStep(FormatterStepConfig config) {
38-
if (name == null || searchRegex == null || replacement == null) {
39-
throw new IllegalArgumentException("Must specify 'name', 'searchRegex' and 'replacement'.");
38+
if (name == null || searchRegex == null) {
39+
throw new IllegalArgumentException("Must specify 'name' and 'searchRegex'.");
4040
}
41-
42-
return ReplaceRegexStep.create(name, searchRegex, replacement);
41+
// Use empty string if replacement is not provided. In pom.xml there is no way to specify
42+
// an empty string as a property value as maven will always trim the value and if it is
43+
// empty, maven will consider the property as not provided.
44+
return ReplaceRegexStep.create(name, searchRegex, replacement != null ? replacement : "");
4345
}
4446
}

0 commit comments

Comments
 (0)