Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect sourceDirectory & testSourceDirectory configs for Java formatters #1553

Merged
merged 1 commit into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* A synthesis log with the number of considered files is added after each formatter execution ([#1507](https://github.com/diffplug/spotless/pull/1507))
### Fixed
* Respect `sourceDirectory` and `testSourceDirectory` POM configurations for Java formatters ([#1553](https://github.com/diffplug/spotless/pull/1553))
### Changes
* **POTENTIALLY BREAKING** `sortByKeys` for JSON formatting now takes into account objects inside arrays ([#1546](https://github.com/diffplug/spotless/pull/1546))
* Any commit of the Spotless maven plugin now available via JitPack ([#1547](https://github.com/diffplug/spotless/pull/1547))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private static String withTrailingSeparator(String path) {

private Set<String> getIncludes(FormatterFactory formatterFactory) {
Set<String> configuredIncludes = formatterFactory.includes();
Set<String> includes = configuredIncludes.isEmpty() ? formatterFactory.defaultIncludes() : configuredIncludes;
Set<String> includes = configuredIncludes.isEmpty() ? formatterFactory.defaultIncludes(project) : configuredIncludes;
if (includes.isEmpty()) {
throw new PluginException("You must specify some files to include, such as '<includes><include>src/**/*.blah</include></includes>'");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.stream.Collectors;

import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import com.diffplug.common.collect.Sets;
import com.diffplug.spotless.FormatExceptionPolicyStrict;
Expand Down Expand Up @@ -71,7 +72,7 @@ public abstract class FormatterFactory {

private ToggleOffOn toggle;

public abstract Set<String> defaultIncludes();
public abstract Set<String> defaultIncludes(MavenProject project);

public abstract String licenseHeaderDelimiter();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@

import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.antlr4.Antlr4Defaults;
import com.diffplug.spotless.maven.FormatterFactory;
Expand All @@ -30,7 +32,7 @@
*/
public class Antlr4 extends FormatterFactory {
@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return ImmutableSet.of(Antlr4Defaults.includes());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@
import java.util.Collections;
import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.spotless.cpp.CppDefaults;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;
Expand All @@ -30,7 +32,7 @@
*/
public class Cpp extends FormatterFactory {
@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return Collections.emptySet();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@
import java.util.Collections;
import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.spotless.maven.FormatterFactory;

/**
Expand All @@ -29,7 +31,7 @@
public class Format extends FormatterFactory {

@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return Collections.emptySet();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 DiffPlug
* Copyright 2020-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@

import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;
Expand All @@ -33,7 +35,7 @@ public class Groovy extends FormatterFactory {
private static final String LICENSE_HEADER_DELIMITER = "package ";

@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return DEFAULT_INCLUDES;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,17 @@
*/
package com.diffplug.spotless.maven.java;

import static java.util.stream.Collectors.toSet;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.stream.Stream;

import org.apache.maven.model.Build;
import org.apache.maven.project.MavenProject;

import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;

Expand All @@ -29,12 +37,17 @@
*/
public class Java extends FormatterFactory {

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

@Override
public Set<String> defaultIncludes() {
return DEFAULT_INCLUDES;
public Set<String> defaultIncludes(MavenProject project) {
Path projectDir = project.getBasedir().toPath();
Build build = project.getBuild();
return Stream.of(build.getSourceDirectory(), build.getTestSourceDirectory())
.map(Paths::get)
.map(projectDir::relativize)
.map(Java::fileMask)
.collect(toSet());
}

@Override
Expand Down Expand Up @@ -65,4 +78,12 @@ public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) {
public void addFormatAnnotations(FormatAnnotations formatAnnotations) {
addStepFactory(formatAnnotations);
}

private static String fileMask(Path path) {
String dir = path.toString();
if (!dir.endsWith(File.separator)) {
dir += File.separator;
}
return dir + "**" + File.separator + "*.java";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Collections;
import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.spotless.maven.FormatterFactory;

/**
Expand All @@ -27,7 +29,7 @@
*/
public class Javascript extends FormatterFactory {
@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return Collections.emptySet();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Collections;
import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.spotless.maven.FormatterFactory;

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

@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return Collections.emptySet();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,8 @@

import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;

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

@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return DEFAULT_INCLUDES;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@
import java.util.Collections;
import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;

Expand All @@ -29,7 +31,7 @@
*/
public class Markdown extends FormatterFactory {
@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return Collections.emptySet();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@

import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;
Expand All @@ -29,7 +31,7 @@
*/
public class Pom extends FormatterFactory {
@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return ImmutableSet.of("pom.xml");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@
import java.util.Collections;
import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;

Expand All @@ -30,7 +32,7 @@
public class Python extends FormatterFactory {

@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return Collections.emptySet();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@

import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;
Expand All @@ -34,7 +36,7 @@ public class Scala extends FormatterFactory {
private static final String LICENSE_HEADER_DELIMITER = "package ";

@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return DEFAULT_INCLUDES;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 DiffPlug
* Copyright 2020-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@
import java.util.Collections;
import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.spotless.maven.FormatterFactory;

/**
Expand All @@ -27,7 +29,7 @@
*/
public class Sql extends FormatterFactory {
@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return Collections.emptySet();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Collections;
import java.util.Set;

import org.apache.maven.project.MavenProject;

import com.diffplug.spotless.maven.FormatterFactory;

/**
Expand All @@ -27,7 +29,7 @@
*/
public class Typescript extends FormatterFactory {
@Override
public Set<String> defaultIncludes() {
public Set<String> defaultIncludes(MavenProject project) {
return Collections.emptySet();
}

Expand Down
Loading