diff --git a/gradle.properties b/gradle.properties index b0debfda66..4810d7155c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 0c4180e7ef..607df5cc1b 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -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)). diff --git a/plugin-maven/build.gradle b/plugin-maven/build.gradle index 0e4e6f0a57..d9c953c0cc 100644 --- a/plugin-maven/build.gradle +++ b/plugin-maven/build.gradle @@ -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}" @@ -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 } @@ -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 ] @@ -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 } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index 33bf1a7061..4a9dbd3af4 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -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; @@ -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; @@ -126,7 +131,15 @@ private FormatterConfig getFormatterConfig() { ArtifactResolver resolver = new ArtifactResolver(repositorySystem, repositorySystemSession, repositories, getLog()); Provisioner provisioner = MavenProvisioner.create(resolver); List 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 getFormatterFactories() { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java new file mode 100644 index 0000000000..237d815ed0 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java @@ -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; + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java index 5656ddb8f9..096771c92d 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java @@ -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 globalStepFactories; public FormatterConfig(File baseDir, String encoding, LineEnding lineEndings, Provisioner provisioner, - List globalStepFactories) { + FileLocator fileLocator, List globalStepFactories) { this.baseDir = baseDir; this.encoding = encoding; this.lineEndings = lineEndings; this.provisioner = provisioner; + this.fileLocator = fileLocator; this.globalStepFactories = globalStepFactories; } @@ -59,4 +61,8 @@ public Provisioner getProvisioner() { public List getGlobalStepFactories() { return unmodifiableList(globalStepFactories); } + + public FileLocator getFileLocator() { + return fileLocator; + } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java index 47b692a8c2..42b04e33f9 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java @@ -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 gatherStepFactories(List allGlobal, List allConfigured) { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java index 0a2fbde018..dfe038a4c0 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java @@ -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() { @@ -42,4 +44,8 @@ public String getLicenseHeaderDelimiter() { public Provisioner getProvisioner() { return provisioner; } + + public FileLocator getFileLocator() { + return fileLocator; + } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java index aa9317616e..028ebc46cd 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java @@ -26,7 +26,7 @@ public class LicenseHeader implements FormatterStepFactory { @Parameter - private File file; + private String file; @Parameter private String content; @@ -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); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Eclipse.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Eclipse.java index 7a8dbeb331..3694228045 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Eclipse.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Eclipse.java @@ -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; @@ -31,7 +30,7 @@ public class Eclipse implements FormatterStepFactory { @Parameter(required = true) - private File file; + private String file; @Parameter private String version; @@ -39,7 +38,7 @@ public class Eclipse implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig config) { String formatterVersion = version == null ? defaultVersion() : version; - Set settingsFiles = singleton(file); - return EclipseFormatterStep.create(formatterVersion, settingsFiles, config.getProvisioner()); + File settingsFile = config.getFileLocator().locateFile(file); + return EclipseFormatterStep.create(formatterVersion, singleton(settingsFile), config.getProvisioner()); } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/ImportOrder.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/ImportOrder.java index 3be6897679..73c2d98a80 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/ImportOrder.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/ImportOrder.java @@ -26,7 +26,7 @@ public class ImportOrder implements FormatterStepFactory { @Parameter - private File file; + private String file; @Parameter private String order; @@ -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(",")); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scalafmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scalafmt.java index ce28bd4813..470a5ddb72 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scalafmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scalafmt.java @@ -27,7 +27,7 @@ public class Scalafmt implements FormatterStepFactory { @Parameter - private File file; + private String file; @Parameter private String version; @@ -35,6 +35,7 @@ public class Scalafmt implements FormatterStepFactory { @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); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java new file mode 100644 index 0000000000..4ddcb16768 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java @@ -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 argCaptor = ArgumentCaptor.forClass(String.class); + verify(resourceManager).getResourceAsFile(eq("/tmp/configs/my-config.xml"), argCaptor.capture()); + assertThat(argCaptor.getValue()).startsWith("my-config").endsWith(".xml"); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java index 9547113794..c5d8e75620 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java @@ -16,6 +16,11 @@ package com.diffplug.spotless.maven; import static com.diffplug.common.base.Strings.isNullOrEmpty; +import static java.util.Arrays.asList; +import static java.util.Arrays.stream; +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; +import static java.util.stream.Collectors.toList; import static org.junit.Assert.fail; import java.io.BufferedReader; @@ -26,8 +31,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import org.junit.Before; @@ -44,6 +48,9 @@ public class MavenIntegrationTest extends ResourceHarness { private static final String SPOTLESS_MAVEN_PLUGIN_VERSION = "spotlessMavenPluginVersion"; private static final String CONFIGURATION = "configuration"; private static final String EXECUTIONS = "executions"; + private static final String MODULES = "modules"; + private static final String MODULE_NAME = "name"; + private static final String CHILD_ID = "childId"; private final MustacheFactory mustacheFactory = new DefaultMustacheFactory(); @@ -106,20 +113,27 @@ protected MavenRunner mavenRunner() throws IOException { .withLocalRepository(new File(getSystemProperty(LOCAL_MAVEN_REPOSITORY_DIR))); } + protected MultiModuleProjectCreator multiModuleProject() { + return new MultiModuleProjectCreator(); + } + private String createPomXmlContent(String[] executions, String[] configuration) throws IOException { - URL url = MavenIntegrationTest.class.getResource("/pom-test.xml.mustache"); + Map params = buildPomXmlParams(executions, configuration, null); + return createPomXmlContent("/pom-test.xml.mustache", params); + } + + private String createPomXmlContent(String pomTemplate, Map params) throws IOException { + URL url = MavenIntegrationTest.class.getResource(pomTemplate); try (BufferedReader reader = Resources.asCharSource(url, StandardCharsets.UTF_8).openBufferedStream()) { Mustache mustache = mustacheFactory.compile(reader, "pom"); StringWriter writer = new StringWriter(); - Map params = buildPomXmlParams(executions, configuration); mustache.execute(writer, params); return writer.toString(); } } - private static Map buildPomXmlParams(String[] executions, String[] configuration) { - Map params = new HashMap<>(); - params.put(LOCAL_MAVEN_REPOSITORY_DIR, getSystemProperty(LOCAL_MAVEN_REPOSITORY_DIR)); + private static Map buildPomXmlParams(String[] executions, String[] configuration, String[] modules) { + Map params = new HashMap<>(); params.put(SPOTLESS_MAVEN_PLUGIN_VERSION, getSystemProperty(SPOTLESS_MAVEN_PLUGIN_VERSION)); if (configuration != null) { @@ -130,6 +144,11 @@ private static Map buildPomXmlParams(String[] executions, String params.put(EXECUTIONS, String.join("\n", executions)); } + if (modules != null) { + List> moduleNames = stream(modules).map(name -> singletonMap(MODULE_NAME, name)).collect(toList()); + params.put(MODULES, moduleNames); + } + return params; } @@ -161,4 +180,86 @@ private static String[] including(String... include) { result[result.length - 1] = ""; return result; } + + protected class MultiModuleProjectCreator { + + private String configSubProject; + private SubProjectFile[] configSubProjectFiles; + private String[] configuration; + private final Map> subProjects = new LinkedHashMap<>(); + + protected MultiModuleProjectCreator withConfigSubProject(String name, SubProjectFile... files) { + configSubProject = name; + configSubProjectFiles = files; + return this; + } + + protected MultiModuleProjectCreator withConfiguration(String... lines) { + configuration = lines; + return this; + } + + protected MultiModuleProjectCreator addSubProject(String name, SubProjectFile... files) { + subProjects.put(name, asList(files)); + return this; + } + + protected void create() throws IOException { + createRootPom(); + createConfigSubProject(); + createSubProjects(); + } + + private void createRootPom() throws IOException { + List modulesList = new ArrayList<>(); + modulesList.add(configSubProject); + modulesList.addAll(subProjects.keySet()); + String[] modules = modulesList.toArray(new String[0]); + + Map rootPomParams = buildPomXmlParams(null, configuration, modules); + setFile("pom.xml").toContent(createPomXmlContent("/multi-module/pom-parent.xml.mustache", rootPomParams)); + } + + private void createConfigSubProject() throws IOException { + if (configSubProject != null) { + String content = createPomXmlContent("/multi-module/pom-config.xml.mustache", emptyMap()); + setFile(configSubProject + "/pom.xml").toContent(content); + + createSubProjectFiles(configSubProject, asList(configSubProjectFiles)); + } + } + + private void createSubProjects() throws IOException { + for (Map.Entry> entry : subProjects.entrySet()) { + String subProjectName = entry.getKey(); + List subProjectFiles = entry.getValue(); + + String content = createPomXmlContent("/multi-module/pom-child.xml.mustache", singletonMap(CHILD_ID, subProjectName)); + setFile(subProjectName + "/pom.xml").toContent(content); + + createSubProjectFiles(subProjectName, subProjectFiles); + } + } + + private void createSubProjectFiles(String subProjectName, List subProjectFiles) throws IOException { + for (SubProjectFile file : subProjectFiles) { + setFile(subProjectName + '/' + file.to).toResource(file.from); + } + } + } + + protected static class SubProjectFile { + + private final String from; + private final String to; + + private SubProjectFile(String from, String to) { + this.from = from; + this.to = to; + } + + protected static SubProjectFile file(String from, String to) { + return new SubProjectFile(from, to); + } + } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java new file mode 100644 index 0000000000..2a5f5653cb --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java @@ -0,0 +1,99 @@ +/* + * 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.spotless.maven.MavenIntegrationTest.SubProjectFile.file; + +import org.junit.Test; + +public class MultiModuleProjectTest extends MavenIntegrationTest { + + @Test + public void testConfigurationDependency() throws Exception { + /* + create a multi-module project with the following stucture: + + /junit-tmp-dir + ├── config + │   ├── pom.xml + │   └── src/main/resources/configs + │   ├── eclipse-formatter.xml + │   └── scalafmt.conf + ├── mvnw + ├── mvnw.cmd + ├── one + │   ├── pom.xml + │   └── src + │   ├── main/java/test1.java + │   └── test/java/test2.java + ├── two + │   ├── pom.xml + │   └── src + │   ├── main/java/test1.java + │   └── test/java/test2.java + ├── three + │   ├── pom.xml + │   └── src + │   ├── main/scala/test1.scala + │   └── test/scala/test2.scala + ├── pom.xml + ├── .mvn + ├── mvnw + └── mvnw.cmd + */ + multiModuleProject() + .withConfigSubProject("config", + file("java/eclipse/formatter.xml", "src/main/resources/configs/eclipse-formatter.xml"), + file("scala/scalafmt/scalafmt.conf", "src/main/resources/configs/scalafmt.conf")) + .withConfiguration( + "", + " ", + " configs/eclipse-formatter.xml", + " 4.7.1", + " ", + "", + "", + " ", + " configs/scalafmt.conf", + " ", + "") + .addSubProject("one", + file("java/eclipse/JavaCodeUnformatted.test", "src/main/java/test1.java"), + file("java/eclipse/JavaCodeUnformatted.test", "src/test/java/test2.java")) + .addSubProject("two", + file("java/eclipse/JavaCodeUnformatted.test", "src/main/java/test1.java"), + file("java/eclipse/JavaCodeUnformatted.test", "src/test/java/test2.java")) + .addSubProject("three", + file("scala/scalafmt/basic.dirty", "src/main/scala/test1.scala"), + file("scala/scalafmt/basic.dirty", "src/test/scala/test2.scala")) + .create(); + + // build config module that contains eclipse and scalafmt configuration files + mavenRunner().withArguments("-f", "config", "clean", "install").runNoError(); + + // format all files in the multi-module project + mavenRunner().withArguments("spotless:apply").runNoError(); + + assertFile("one/src/main/java/test1.java").sameAsResource("java/eclipse/JavaCodeFormatted.test"); + assertFile("one/src/test/java/test2.java").sameAsResource("java/eclipse/JavaCodeFormatted.test"); + + assertFile("two/src/main/java/test1.java").sameAsResource("java/eclipse/JavaCodeFormatted.test"); + assertFile("two/src/test/java/test2.java").sameAsResource("java/eclipse/JavaCodeFormatted.test"); + + assertFile("three/src/main/scala/test1.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf"); + assertFile("three/src/test/scala/test2.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf"); + } +} diff --git a/plugin-maven/src/test/resources/multi-module/pom-child.xml.mustache b/plugin-maven/src/test/resources/multi-module/pom-child.xml.mustache new file mode 100644 index 0000000000..8d2af11ae0 --- /dev/null +++ b/plugin-maven/src/test/resources/multi-module/pom-child.xml.mustache @@ -0,0 +1,26 @@ + + 4.0.0 + + + com.diffplug.spotless + spotless-maven-plugin-tests-parent + 1.0.0-SNAPSHOT + + + com.diffplug.spotless + spotless-maven-plugin-tests-child-{{{childId}}} + 1.0.0-SNAPSHOT + + Spotless Maven Plugin Tests Child {{{childId}}} + + + + + com.diffplug.spotless + spotless-maven-plugin + + + + + diff --git a/plugin-maven/src/test/resources/multi-module/pom-config.xml.mustache b/plugin-maven/src/test/resources/multi-module/pom-config.xml.mustache new file mode 100644 index 0000000000..e054d76c65 --- /dev/null +++ b/plugin-maven/src/test/resources/multi-module/pom-config.xml.mustache @@ -0,0 +1,17 @@ + + 4.0.0 + + + com.diffplug.spotless + spotless-maven-plugin-tests-parent + 1.0.0-SNAPSHOT + + + com.diffplug.spotless + spotless-maven-plugin-tests-config + 1.0.0-SNAPSHOT + + Spotless Maven Plugin Tests Config + + diff --git a/plugin-maven/src/test/resources/multi-module/pom-parent.xml.mustache b/plugin-maven/src/test/resources/multi-module/pom-parent.xml.mustache new file mode 100644 index 0000000000..7e23167446 --- /dev/null +++ b/plugin-maven/src/test/resources/multi-module/pom-parent.xml.mustache @@ -0,0 +1,46 @@ + + 4.0.0 + + com.diffplug.spotless + spotless-maven-plugin-tests-parent + 1.0.0-SNAPSHOT + pom + + Spotless Maven Plugin Tests Parent + + + UTF-8 + 1.8 + 1.8 + + + + {{#modules}} + {{{name}}} + {{/modules}} + + + + + + + com.diffplug.spotless + spotless-maven-plugin + {{spotlessMavenPluginVersion}} + + {{{configuration}}} + + + + com.diffplug.spotless + spotless-maven-plugin-tests-config + ${project.version} + + + + + + + + diff --git a/plugin-maven/src/test/resources/pom-build.xml.mustache b/plugin-maven/src/test/resources/pom-build.xml.mustache index 7fbd6fc08e..1fad90d874 100644 --- a/plugin-maven/src/test/resources/pom-build.xml.mustache +++ b/plugin-maven/src/test/resources/pom-build.xml.mustache @@ -18,6 +18,7 @@ {{mavenApiVersion}} {{eclipseAetherVersion}} {{spotlessLibVersion}} + {{plexusResourcesVersion}} @@ -45,6 +46,11 @@ ${eclipse.aether.version} provided + + org.codehaus.plexus + plexus-resources + ${plexus.resources.version} + com.diffplug.spotless