|
1 | 1 | /*
|
2 |
| - * Copyright 2016-2022 DiffPlug |
| 2 | + * Copyright 2016-2023 DiffPlug |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package com.diffplug.spotless.maven.java;
|
17 | 17 |
|
| 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; |
18 | 23 | 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; |
19 | 28 |
|
20 |
| -import com.diffplug.common.collect.ImmutableSet; |
21 | 29 | import com.diffplug.spotless.maven.FormatterFactory;
|
22 | 30 | import com.diffplug.spotless.maven.generic.LicenseHeader;
|
23 | 31 |
|
|
29 | 37 | */
|
30 | 38 | public class Java extends FormatterFactory {
|
31 | 39 |
|
32 |
| - private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("src/main/java/**/*.java", "src/test/java/**/*.java"); |
33 | 40 | private static final String LICENSE_HEADER_DELIMITER = "package ";
|
34 | 41 |
|
35 | 42 | @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()); |
38 | 51 | }
|
39 | 52 |
|
40 | 53 | @Override
|
@@ -65,4 +78,12 @@ public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) {
|
65 | 78 | public void addFormatAnnotations(FormatAnnotations formatAnnotations) {
|
66 | 79 | addStepFactory(formatAnnotations);
|
67 | 80 | }
|
| 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 | + } |
68 | 89 | }
|
0 commit comments