-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved config handling in maven plugin
Exposed `encoding` and `lineEndings` in the plugin xml configuration. Renamed `eclipseFormatFile` to `eclipseConfigFile` and moved it inside the `<java>` tag. Maven plugins can handle nested configs using Plexus dependency injection and POJO objects. Thus introduced a dedicated class for all java related configuration called `Java`. Plugin xml configuration can now look like: ``` <plugin> <groupId>com.diffplug.spotless</groupId> <artifactId>spotless-maven-plugin</artifactId> <version>${spotless.version}</version> <configuration> <encoding>UTF-8</encoding> <lineEndings>UNIX</lineEndings> <java> <eclipseConfigFile>${basedir}/eclipse-fmt.xml</eclipseConfigFile> </java> </configuration> </plugin> ``` Extracted an abstract `AbstractSpotlessMojo` to hold all injected dependencies. It can be potentially used in future to implement the `check` goal. Changed name of `SpotlessMojo` from "spotless" to "apply" so that it can be invoked with `mvn spotless:apply`.
- Loading branch information
Showing
3 changed files
with
156 additions
and
51 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
plugin-maven/src/main/java/com/diffplug/gradle/spotless/AbstractSpotlessMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.gradle.spotless; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.io.File; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.apache.maven.plugins.annotations.Component; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.apache.maven.project.MavenProject; | ||
import org.eclipse.aether.RepositorySystem; | ||
import org.eclipse.aether.RepositorySystemSession; | ||
import org.eclipse.aether.repository.RemoteRepository; | ||
|
||
import com.diffplug.spotless.LineEnding; | ||
|
||
public abstract class AbstractSpotlessMojo extends AbstractMojo { | ||
|
||
private static final String DEFAULT_ENCODING = "UTF-8"; | ||
private static final String DEFAULT_LINE_ENDINGS = "GIT_ATTRIBUTES"; | ||
|
||
@Component | ||
private RepositorySystem repositorySystem; | ||
|
||
@Parameter(defaultValue = "${repositorySystemSession}", required = true, readonly = true) | ||
private RepositorySystemSession repositorySystemSession; | ||
|
||
@Parameter(defaultValue = "${project.remotePluginRepositories}", required = true, readonly = true) | ||
private List<RemoteRepository> repositories; | ||
|
||
@Parameter(defaultValue = "${project}", required = true, readonly = true) | ||
private MavenProject project; | ||
|
||
@Parameter(defaultValue = DEFAULT_ENCODING) | ||
private String encoding; | ||
|
||
@Parameter(defaultValue = DEFAULT_LINE_ENDINGS) | ||
private LineEnding lineEndings; | ||
|
||
@Parameter(required = true) | ||
private Java java; | ||
|
||
protected ArtifactResolver createArtifactResolver() { | ||
return new ArtifactResolver(repositorySystem, repositorySystemSession, repositories); | ||
} | ||
|
||
protected List<Path> getAllSourceRoots() { | ||
Stream<String> compileSourceRoots = project.getCompileSourceRoots().stream(); | ||
Stream<String> testCompileSourceRoots = project.getTestCompileSourceRoots().stream(); | ||
return Stream.concat(compileSourceRoots, testCompileSourceRoots) | ||
.map(Paths::get) | ||
.filter(Files::isDirectory) | ||
.collect(toList()); | ||
} | ||
|
||
protected Charset getEncoding() { | ||
return Charset.forName(encoding); | ||
} | ||
|
||
protected LineEnding.Policy getLineEndingsPolicy(List<File> filesToFormat) { | ||
return lineEndings.createPolicy(getRootDir(), () -> filesToFormat); | ||
} | ||
|
||
protected File getRootDir() { | ||
return project.getBasedir(); | ||
} | ||
|
||
protected Java getJavaConfig() { | ||
return java; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
plugin-maven/src/main/java/com/diffplug/gradle/spotless/Java.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.gradle.spotless; | ||
|
||
import java.io.File; | ||
|
||
public class Java { | ||
private File eclipseConfigFile; | ||
|
||
public File getEclipseConfigFile() { | ||
return eclipseConfigFile; | ||
} | ||
|
||
public void setEclipseConfigFile(File eclipseConfigFile) { | ||
this.eclipseConfigFile = eclipseConfigFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters