Skip to content

Commit 1774837

Browse files
authored
Respect sourceDirectory & testSourceDirectory configs for Java formatters (#1553 fixes #1214)
2 parents fd40aa7 + dc9878b commit 1774837

File tree

22 files changed

+250
-35
lines changed

22 files changed

+250
-35
lines changed

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* A synthesis log with the number of considered files is added after each formatter execution ([#1507](https://github.com/diffplug/spotless/pull/1507))
8+
### Fixed
9+
* Respect `sourceDirectory` and `testSourceDirectory` POM configurations for Java formatters ([#1553](https://github.com/diffplug/spotless/pull/1553))
810
### Changes
911
* **POTENTIALLY BREAKING** `sortByKeys` for JSON formatting now takes into account objects inside arrays ([#1546](https://github.com/diffplug/spotless/pull/1546))
1012
* Any commit of the Spotless maven plugin now available via JitPack ([#1547](https://github.com/diffplug/spotless/pull/1547))

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ private static String withTrailingSeparator(String path) {
320320

321321
private Set<String> getIncludes(FormatterFactory formatterFactory) {
322322
Set<String> configuredIncludes = formatterFactory.includes();
323-
Set<String> includes = configuredIncludes.isEmpty() ? formatterFactory.defaultIncludes() : configuredIncludes;
323+
Set<String> includes = configuredIncludes.isEmpty() ? formatterFactory.defaultIncludes(project) : configuredIncludes;
324324
if (includes.isEmpty()) {
325325
throw new PluginException("You must specify some files to include, such as '<includes><include>src/**/*.blah</include></includes>'");
326326
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.stream.Collectors;
3030

3131
import org.apache.maven.plugins.annotations.Parameter;
32+
import org.apache.maven.project.MavenProject;
3233

3334
import com.diffplug.common.collect.Sets;
3435
import com.diffplug.spotless.FormatExceptionPolicyStrict;
@@ -71,7 +72,7 @@ public abstract class FormatterFactory {
7172

7273
private ToggleOffOn toggle;
7374

74-
public abstract Set<String> defaultIncludes();
75+
public abstract Set<String> defaultIncludes(MavenProject project);
7576

7677
public abstract String licenseHeaderDelimiter();
7778

plugin-maven/src/main/java/com/diffplug/spotless/maven/antlr4/Antlr4.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -17,6 +17,8 @@
1717

1818
import java.util.Set;
1919

20+
import org.apache.maven.project.MavenProject;
21+
2022
import com.diffplug.common.collect.ImmutableSet;
2123
import com.diffplug.spotless.antlr4.Antlr4Defaults;
2224
import com.diffplug.spotless.maven.FormatterFactory;
@@ -30,7 +32,7 @@
3032
*/
3133
public class Antlr4 extends FormatterFactory {
3234
@Override
33-
public Set<String> defaultIncludes() {
35+
public Set<String> defaultIncludes(MavenProject project) {
3436
return ImmutableSet.of(Antlr4Defaults.includes());
3537
}
3638

plugin-maven/src/main/java/com/diffplug/spotless/maven/cpp/Cpp.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -18,6 +18,8 @@
1818
import java.util.Collections;
1919
import java.util.Set;
2020

21+
import org.apache.maven.project.MavenProject;
22+
2123
import com.diffplug.spotless.cpp.CppDefaults;
2224
import com.diffplug.spotless.maven.FormatterFactory;
2325
import com.diffplug.spotless.maven.generic.LicenseHeader;
@@ -30,7 +32,7 @@
3032
*/
3133
public class Cpp extends FormatterFactory {
3234
@Override
33-
public Set<String> defaultIncludes() {
35+
public Set<String> defaultIncludes(MavenProject project) {
3436
return Collections.emptySet();
3537
}
3638

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -18,6 +18,8 @@
1818
import java.util.Collections;
1919
import java.util.Set;
2020

21+
import org.apache.maven.project.MavenProject;
22+
2123
import com.diffplug.spotless.maven.FormatterFactory;
2224

2325
/**
@@ -29,7 +31,7 @@
2931
public class Format extends FormatterFactory {
3032

3133
@Override
32-
public Set<String> defaultIncludes() {
34+
public Set<String> defaultIncludes(MavenProject project) {
3335
return Collections.emptySet();
3436
}
3537

plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 DiffPlug
2+
* Copyright 2020-2023 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.
@@ -17,6 +17,8 @@
1717

1818
import java.util.Set;
1919

20+
import org.apache.maven.project.MavenProject;
21+
2022
import com.diffplug.common.collect.ImmutableSet;
2123
import com.diffplug.spotless.maven.FormatterFactory;
2224
import com.diffplug.spotless.maven.generic.LicenseHeader;
@@ -33,7 +35,7 @@ public class Groovy extends FormatterFactory {
3335
private static final String LICENSE_HEADER_DELIMITER = "package ";
3436

3537
@Override
36-
public Set<String> defaultIncludes() {
38+
public Set<String> defaultIncludes(MavenProject project) {
3739
return DEFAULT_INCLUDES;
3840
}
3941

plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -15,9 +15,17 @@
1515
*/
1616
package com.diffplug.spotless.maven.java;
1717

18+
import static java.util.stream.Collectors.toSet;
19+
20+
import java.io.File;
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
1823
import java.util.Set;
24+
import java.util.stream.Stream;
25+
26+
import org.apache.maven.model.Build;
27+
import org.apache.maven.project.MavenProject;
1928

20-
import com.diffplug.common.collect.ImmutableSet;
2129
import com.diffplug.spotless.maven.FormatterFactory;
2230
import com.diffplug.spotless.maven.generic.LicenseHeader;
2331

@@ -29,12 +37,17 @@
2937
*/
3038
public class Java extends FormatterFactory {
3139

32-
private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("src/main/java/**/*.java", "src/test/java/**/*.java");
3340
private static final String LICENSE_HEADER_DELIMITER = "package ";
3441

3542
@Override
36-
public Set<String> defaultIncludes() {
37-
return DEFAULT_INCLUDES;
43+
public Set<String> defaultIncludes(MavenProject project) {
44+
Path projectDir = project.getBasedir().toPath();
45+
Build build = project.getBuild();
46+
return Stream.of(build.getSourceDirectory(), build.getTestSourceDirectory())
47+
.map(Paths::get)
48+
.map(projectDir::relativize)
49+
.map(Java::fileMask)
50+
.collect(toSet());
3851
}
3952

4053
@Override
@@ -65,4 +78,12 @@ public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) {
6578
public void addFormatAnnotations(FormatAnnotations formatAnnotations) {
6679
addStepFactory(formatAnnotations);
6780
}
81+
82+
private static String fileMask(Path path) {
83+
String dir = path.toString();
84+
if (!dir.endsWith(File.separator)) {
85+
dir += File.separator;
86+
}
87+
return dir + "**" + File.separator + "*.java";
88+
}
6889
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/Javascript.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Collections;
1919
import java.util.Set;
2020

21+
import org.apache.maven.project.MavenProject;
22+
2123
import com.diffplug.spotless.maven.FormatterFactory;
2224

2325
/**
@@ -27,7 +29,7 @@
2729
*/
2830
public class Javascript extends FormatterFactory {
2931
@Override
30-
public Set<String> defaultIncludes() {
32+
public Set<String> defaultIncludes(MavenProject project) {
3133
return Collections.emptySet();
3234
}
3335

plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Json.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Collections;
1919
import java.util.Set;
2020

21+
import org.apache.maven.project.MavenProject;
22+
2123
import com.diffplug.spotless.maven.FormatterFactory;
2224

2325
/**
@@ -27,7 +29,7 @@ public class Json extends FormatterFactory {
2729
public static final int DEFAULT_INDENTATION = 4;
2830

2931
@Override
30-
public Set<String> defaultIncludes() {
32+
public Set<String> defaultIncludes(MavenProject project) {
3133
return Collections.emptySet();
3234
}
3335

plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Kotlin.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -19,6 +19,8 @@
1919

2020
import java.util.Set;
2121

22+
import org.apache.maven.project.MavenProject;
23+
2224
import com.diffplug.common.collect.ImmutableSet;
2325
import com.diffplug.spotless.maven.FormatterFactory;
2426

@@ -27,7 +29,7 @@ public class Kotlin extends FormatterFactory {
2729
private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("src/main/kotlin/**/*.kt", "src/test/kotlin/**/*.kt");
2830

2931
@Override
30-
public Set<String> defaultIncludes() {
32+
public Set<String> defaultIncludes(MavenProject project) {
3133
return DEFAULT_INCLUDES;
3234
}
3335

plugin-maven/src/main/java/com/diffplug/spotless/maven/markdown/Markdown.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 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.
@@ -18,6 +18,8 @@
1818
import java.util.Collections;
1919
import java.util.Set;
2020

21+
import org.apache.maven.project.MavenProject;
22+
2123
import com.diffplug.spotless.maven.FormatterFactory;
2224
import com.diffplug.spotless.maven.generic.LicenseHeader;
2325

@@ -29,7 +31,7 @@
2931
*/
3032
public class Markdown extends FormatterFactory {
3133
@Override
32-
public Set<String> defaultIncludes() {
34+
public Set<String> defaultIncludes(MavenProject project) {
3335
return Collections.emptySet();
3436
}
3537

plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/Pom.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 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.
@@ -17,6 +17,8 @@
1717

1818
import java.util.Set;
1919

20+
import org.apache.maven.project.MavenProject;
21+
2022
import com.diffplug.common.collect.ImmutableSet;
2123
import com.diffplug.spotless.maven.FormatterFactory;
2224
import com.diffplug.spotless.maven.generic.LicenseHeader;
@@ -29,7 +31,7 @@
2931
*/
3032
public class Pom extends FormatterFactory {
3133
@Override
32-
public Set<String> defaultIncludes() {
34+
public Set<String> defaultIncludes(MavenProject project) {
3335
return ImmutableSet.of("pom.xml");
3436
}
3537

plugin-maven/src/main/java/com/diffplug/spotless/maven/python/Python.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 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.
@@ -18,6 +18,8 @@
1818
import java.util.Collections;
1919
import java.util.Set;
2020

21+
import org.apache.maven.project.MavenProject;
22+
2123
import com.diffplug.spotless.maven.FormatterFactory;
2224
import com.diffplug.spotless.maven.generic.LicenseHeader;
2325

@@ -30,7 +32,7 @@
3032
public class Python extends FormatterFactory {
3133

3234
@Override
33-
public Set<String> defaultIncludes() {
35+
public Set<String> defaultIncludes(MavenProject project) {
3436
return Collections.emptySet();
3537
}
3638

plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -17,6 +17,8 @@
1717

1818
import java.util.Set;
1919

20+
import org.apache.maven.project.MavenProject;
21+
2022
import com.diffplug.common.collect.ImmutableSet;
2123
import com.diffplug.spotless.maven.FormatterFactory;
2224
import com.diffplug.spotless.maven.generic.LicenseHeader;
@@ -34,7 +36,7 @@ public class Scala extends FormatterFactory {
3436
private static final String LICENSE_HEADER_DELIMITER = "package ";
3537

3638
@Override
37-
public Set<String> defaultIncludes() {
39+
public Set<String> defaultIncludes(MavenProject project) {
3840
return DEFAULT_INCLUDES;
3941
}
4042

plugin-maven/src/main/java/com/diffplug/spotless/maven/sql/Sql.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 DiffPlug
2+
* Copyright 2020-2023 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.
@@ -18,6 +18,8 @@
1818
import java.util.Collections;
1919
import java.util.Set;
2020

21+
import org.apache.maven.project.MavenProject;
22+
2123
import com.diffplug.spotless.maven.FormatterFactory;
2224

2325
/**
@@ -27,7 +29,7 @@
2729
*/
2830
public class Sql extends FormatterFactory {
2931
@Override
30-
public Set<String> defaultIncludes() {
32+
public Set<String> defaultIncludes(MavenProject project) {
3133
return Collections.emptySet();
3234
}
3335

plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Collections;
1919
import java.util.Set;
2020

21+
import org.apache.maven.project.MavenProject;
22+
2123
import com.diffplug.spotless.maven.FormatterFactory;
2224

2325
/**
@@ -27,7 +29,7 @@
2729
*/
2830
public class Typescript extends FormatterFactory {
2931
@Override
30-
public Set<String> defaultIncludes() {
32+
public Set<String> defaultIncludes(MavenProject project) {
3133
return Collections.emptySet();
3234
}
3335

0 commit comments

Comments
 (0)