Skip to content

Improved support for multi-module maven projects #210

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

Merged
merged 3 commits into from
Feb 26, 2018
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
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ VER_MOCKITO=2.13.0
VER_MAVEN_API=3.0
VER_ECLIPSE_AETHER=1.1.0
VER_MUSTACHE=0.9.5
VER_PLEXUS_RESOURCES=1.0.1
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Version 1.10.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/))

* Improved support for multi-module Maven projects ([#210](https://github.com/diffplug/spotless/pull/210)).

### Version 1.0.0.BETA2 - February 15th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.0.0.BETA2/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.0.0.BETA2))

* Fix build to ensure that published versions never have snapshot deps ([#205](https://github.com/diffplug/spotless/pull/205)).
Expand Down
5 changes: 4 additions & 1 deletion plugin-maven/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dependencies {
compileOnly "org.apache.maven.plugin-tools:maven-plugin-annotations:${VER_MAVEN_API}"
compileOnly "org.eclipse.aether:aether-api:${VER_ECLIPSE_AETHER}"
compileOnly "org.eclipse.aether:aether-util:${VER_ECLIPSE_AETHER}"
compileOnly "org.codehaus.plexus:plexus-resources:${VER_PLEXUS_RESOURCES}"

testCompile project(":testlib")
testCompile "junit:junit:${VER_JUNIT}"
Expand All @@ -76,6 +77,7 @@ dependencies {
testCompile "com.github.spullara.mustache.java:compiler:${VER_MUSTACHE}"
testCompile "org.apache.maven:maven-plugin-api:${VER_MAVEN_API}"
testCompile "org.eclipse.aether:aether-api:${VER_ECLIPSE_AETHER}"
testCompile "org.codehaus.plexus:plexus-resources:${VER_PLEXUS_RESOURCES}"
}

task cleanMavenProjectDir(type: Delete) { delete MAVEN_PROJECT_DIR }
Expand Down Expand Up @@ -141,6 +143,7 @@ task createPomXml(dependsOn: installLocalDependencies) {
mavenApiVersion : VER_MAVEN_API,
eclipseAetherVersion : VER_ECLIPSE_AETHER,
spotlessLibVersion : project.versionLib,
plexusResourcesVersion : VER_PLEXUS_RESOURCES,
additionalDependencies : additionalDependencies
]

Expand Down Expand Up @@ -182,6 +185,6 @@ test.dependsOn(jar)
test {
testLogging { exceptionFormat = 'full' }
// pass location of the local maven repository and plugin version to junit tests
systemProperty "localMavenRepositoryDir", "${LOCAL_MAVEN_REPO_DIR}"
systemProperty "localMavenRepositoryDir", LOCAL_MAVEN_REPO_DIR
systemProperty "spotlessMavenPluginVersion", project.versionMaven
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.FileResourceLoader;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
Expand All @@ -50,6 +52,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Component
private RepositorySystem repositorySystem;

@Component
private ResourceManager resourceManager;

@Parameter(defaultValue = "${repositorySystemSession}", required = true, readonly = true)
private RepositorySystemSession repositorySystemSession;

Expand Down Expand Up @@ -126,7 +131,15 @@ private FormatterConfig getFormatterConfig() {
ArtifactResolver resolver = new ArtifactResolver(repositorySystem, repositorySystemSession, repositories, getLog());
Provisioner provisioner = MavenProvisioner.create(resolver);
List<FormatterStepFactory> formatterStepFactories = getFormatterStepFactories();
return new FormatterConfig(baseDir, encoding, lineEndings, provisioner, formatterStepFactories);
FileLocator fileLocator = getFileLocator();
return new FormatterConfig(baseDir, encoding, lineEndings, provisioner, fileLocator, formatterStepFactories);
}

private FileLocator getFileLocator() {
resourceManager.addSearchPath(FileResourceLoader.ID, baseDir.getAbsolutePath());
resourceManager.addSearchPath("url", "");
resourceManager.setOutputDirectory(targetDir);
return new FileLocator(resourceManager);
}

private List<FormatterFactory> getFormatterFactories() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.spotless.maven;

import static com.diffplug.common.base.Strings.*;

import java.io.File;

import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.FileResourceCreationException;
import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
import org.codehaus.plexus.util.FileUtils;

public class FileLocator {

private final ResourceManager resourceManager;

public FileLocator(ResourceManager resourceManager) {
this.resourceManager = resourceManager;
}

public File locateFile(String path) {
if (isNullOrEmpty(path)) {
return null;
}

String outputFile = tmpOutputFileName(path);
try {
return resourceManager.getResourceAsFile(path, outputFile);
} catch (ResourceNotFoundException e) {
throw new RuntimeException("Unable to locate file with path: " + path, e);
} catch (FileResourceCreationException e) {
throw new RuntimeException("Unable to create temporaty file '" + outputFile + "' in the output directory", e);
}
}

private static String tmpOutputFileName(String path) {
String nameWithExtension = FileUtils.filename(path);
String extension = FileUtils.extension(path);
String name = nameWithExtension.replace('.' + extension, "");
return name + '-' + System.currentTimeMillis() + '.' + extension;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public class FormatterConfig {
private final String encoding;
private final LineEnding lineEndings;
private final Provisioner provisioner;
private final FileLocator fileLocator;
private final List<FormatterStepFactory> globalStepFactories;

public FormatterConfig(File baseDir, String encoding, LineEnding lineEndings, Provisioner provisioner,
List<FormatterStepFactory> globalStepFactories) {
FileLocator fileLocator, List<FormatterStepFactory> globalStepFactories) {
this.baseDir = baseDir;
this.encoding = encoding;
this.lineEndings = lineEndings;
this.provisioner = provisioner;
this.fileLocator = fileLocator;
this.globalStepFactories = globalStepFactories;
}

Expand All @@ -59,4 +61,8 @@ public Provisioner getProvisioner() {
public List<FormatterStepFactory> getGlobalStepFactories() {
return unmodifiableList(globalStepFactories);
}

public FileLocator getFileLocator() {
return fileLocator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private LineEnding lineEndings(FormatterConfig config) {
}

private FormatterStepConfig stepConfig(Charset encoding, FormatterConfig config) {
return new FormatterStepConfig(encoding, licenseHeaderDelimiter(), config.getProvisioner());
return new FormatterStepConfig(encoding, licenseHeaderDelimiter(), config.getProvisioner(), config.getFileLocator());
}

private static List<FormatterStepFactory> gatherStepFactories(List<FormatterStepFactory> allGlobal, List<FormatterStepFactory> allConfigured) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public class FormatterStepConfig {
private final Charset encoding;
private final String licenseHeaderDelimiter;
private final Provisioner provisioner;
private final FileLocator fileLocator;

public FormatterStepConfig(Charset encoding, String licenseHeaderDelimiter, Provisioner provisioner) {
public FormatterStepConfig(Charset encoding, String licenseHeaderDelimiter, Provisioner provisioner, FileLocator fileLocator) {
this.encoding = encoding;
this.licenseHeaderDelimiter = licenseHeaderDelimiter;
this.provisioner = provisioner;
this.fileLocator = fileLocator;
}

public Charset getEncoding() {
Expand All @@ -42,4 +44,8 @@ public String getLicenseHeaderDelimiter() {
public Provisioner getProvisioner() {
return provisioner;
}

public FileLocator getFileLocator() {
return fileLocator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class LicenseHeader implements FormatterStepFactory {
@Parameter
private File file;
private String file;

@Parameter
private String content;
Expand All @@ -43,7 +43,8 @@ public final FormatterStep newFormatterStep(FormatterStepConfig config) {

if (file != null ^ content != null) {
if (file != null) {
return LicenseHeaderStep.createFromFile(file, config.getEncoding(), delimiterString);
File licenseHeaderFile = config.getFileLocator().locateFile(file);
return LicenseHeaderStep.createFromFile(licenseHeaderFile, config.getEncoding(), delimiterString);
} else {
return LicenseHeaderStep.createFromHeader(content, delimiterString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static java.util.Collections.singleton;

import java.io.File;
import java.util.Set;

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

Expand All @@ -31,15 +30,15 @@
public class Eclipse implements FormatterStepFactory {

@Parameter(required = true)
private File file;
private String file;

@Parameter
private String version;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
String formatterVersion = version == null ? defaultVersion() : version;
Set<File> settingsFiles = singleton(file);
return EclipseFormatterStep.create(formatterVersion, settingsFiles, config.getProvisioner());
File settingsFile = config.getFileLocator().locateFile(file);
return EclipseFormatterStep.create(formatterVersion, singleton(settingsFile), config.getProvisioner());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class ImportOrder implements FormatterStepFactory {
@Parameter
private File file;
private String file;

@Parameter
private String order;
Expand All @@ -35,7 +35,8 @@ public class ImportOrder implements FormatterStepFactory {
public FormatterStep newFormatterStep(FormatterStepConfig config) {
if (file != null ^ order != null) {
if (file != null) {
return ImportOrderStep.createFromFile(file);
File importsFile = config.getFileLocator().locateFile(file);
return ImportOrderStep.createFromFile(importsFile);
} else {
return ImportOrderStep.createFromOrder(order.split(","));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
public class Scalafmt implements FormatterStepFactory {

@Parameter
private File file;
private String file;

@Parameter
private String version;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
String scalafmtVersion = version != null ? version : ScalaFmtStep.defaultVersion();
return ScalaFmtStep.create(scalafmtVersion, config.getProvisioner(), file);
File configFile = config.getFileLocator().locateFile(file);
return ScalaFmtStep.create(scalafmtVersion, config.getProvisioner(), configFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.spotless.maven;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

import java.io.File;

import org.codehaus.plexus.resource.ResourceManager;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

public class FileLocatorTest {

private final ResourceManager resourceManager = mock(ResourceManager.class);
private final FileLocator fileLocator = new FileLocator(resourceManager);

@Test
public void locateEmptyString() {
assertNull(fileLocator.locateFile(""));
}

@Test
public void locateNull() {
assertNull(fileLocator.locateFile(null));
}

@Test
public void locateValidFile() throws Exception {
File file = new File("test-config.xml");
when(resourceManager.getResourceAsFile(any(), any())).thenReturn(file);

File locatedFile = fileLocator.locateFile("/tmp/configs/my-config.xml");

assertEquals(file, locatedFile);

ArgumentCaptor<String> argCaptor = ArgumentCaptor.forClass(String.class);
verify(resourceManager).getResourceAsFile(eq("/tmp/configs/my-config.xml"), argCaptor.capture());
assertThat(argCaptor.getValue()).startsWith("my-config").endsWith(".xml");
}
}
Loading