diff --git a/build.gradle.kts b/build.gradle.kts index 6eb549e2..b653d552 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,25 +2,25 @@ import org.infernus.idea.checkstyle.build.CheckstyleVersions import org.jetbrains.intellij.platform.gradle.TestFrameworkType import org.jetbrains.intellij.platform.gradle.tasks.VerifyPluginTask +repositories { + mavenCentral() + + intellijPlatform { + defaultRepositories() + } +} + plugins { id("java") id("jacoco") id("idea") id("org.jetbrains.intellij.platform") version "2.1.0" - id("com.dorongold.task-tree") version "2.1.1" + id("org.infernus.idea.checkstyle.build") } version = "5.98.0" -repositories { - mavenCentral() - - intellijPlatform { - defaultRepositories() - } -} - intellijPlatform { pluginConfiguration { id = "CheckStyle-IDEA" @@ -59,6 +59,7 @@ tasks { withType { setForkEvery(1) + jvmArgs("-Xshare:off") } withType { diff --git a/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/CsaccessTestTask.java b/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/CsaccessTestTask.java index 11c39c4f..afc0e5b7 100644 --- a/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/CsaccessTestTask.java +++ b/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/CsaccessTestTask.java @@ -1,7 +1,6 @@ package org.infernus.idea.checkstyle.build; import java.io.File; -import java.util.Set; import groovy.lang.Closure; import org.gradle.api.Project; @@ -29,7 +28,8 @@ public class CsaccessTestTask extends Test { public static final String CSVERSION_SYSPROP_NAME = "org.infernus.idea.checkstyle.version"; - private String csVersion = null; + private FileCollection effectiveClassPath = null; + private Property dryRun; public CsaccessTestTask() { @@ -43,17 +43,16 @@ public CsaccessTestTask() { GradlePluginMain.configureTestTask(this); setTestClassesDirs(csaccessTestSourceSet.getOutput().getClassesDirs()); setClasspath(csaccessTestSourceSet.getRuntimeClasspath() - .plus(csaccessTestSourceSet.getCompileClasspath())); + .plus(csaccessTestSourceSet.getCompileClasspath())); // TODO delete? } public static String getTaskName(final String pCheckstyleVersion) { return "xtest_" + CheckstyleVersions.toGradleVersion(pCheckstyleVersion); } - public void setCheckstyleVersion(final String pCheckstyleVersion, final boolean isBaseVersion) { - csVersion = pCheckstyleVersion; + public void setCheckstyleVersion(final String checkstyleVersion, final boolean isBaseVersion) { setDescription("Runs the '" + CustomSourceSetCreator.CSACCESSTEST_SOURCESET_NAME + "' unit tests against a " - + "Checkstyle " + pCheckstyleVersion + " runtime."); + + "Checkstyle " + checkstyleVersion + " runtime."); getReports().getJunitXml().getRequired().set(false); if (isBaseVersion) { setGroup(LifecycleBasePlugin.VERIFICATION_GROUP); @@ -67,12 +66,44 @@ public void setCheckstyleVersion(final String pCheckstyleVersion, final boolean configure(new Closure(this) { @Override public Void call() { - systemProperty(CSVERSION_SYSPROP_NAME, pCheckstyleVersion); + systemProperty(CSVERSION_SYSPROP_NAME, checkstyleVersion); return null; } }); + + effectiveClassPath = setClassPathForVersion(checkstyleVersion, getProject()); } + private @NotNull FileCollection setClassPathForVersion(final String checkstyleVersion, final Project project) { + final JavaPluginExtension jpc = project.getExtensions().getByType(JavaPluginExtension.class); + final Dependency csDep = CheckstyleVersions.createCheckstyleDependency(project, checkstyleVersion); + final ConfigurationContainer configurations = project.getConfigurations(); + final Configuration detachedConfiguration = configurations.detachedConfiguration(csDep); + // workaround for Checkstyle#14123 + detachedConfiguration + .getResolutionStrategy() + .getCapabilitiesResolution() + .withCapability("com.google.collections", "google-collections", resolutionDetails -> resolutionDetails.select("com.google.guava:guava:0")); + + final SourceSetContainer sourceSets = jpc.getSourceSets(); + final SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); + final SourceSet testSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME); + final SourceSet csaccessSourceSet = sourceSets.getByName(CustomSourceSetCreator.CSACCESS_SOURCESET_NAME); + final SourceSet csaccessTestSourceSet = jpc.getSourceSets().getByName(CustomSourceSetCreator.CSACCESSTEST_SOURCESET_NAME); + + return project.files( + csaccessTestSourceSet.getOutput().getResourcesDir(), + csaccessSourceSet.getOutput().getResourcesDir(), + mainSourceSet.getOutput().getResourcesDir()) + .plus(csaccessTestSourceSet.getOutput().getClassesDirs()) + .plus(csaccessSourceSet.getOutput().getClassesDirs()) + .plus(mainSourceSet.getOutput().getClassesDirs()) + .plus(project.files(detachedConfiguration.getFiles())) + .plus(csaccessTestSourceSet.getRuntimeClasspath()) + .plus(csaccessTestSourceSet.getCompileClasspath()) + .minus(testSourceSet.getOutput().getClassesDirs()) + .minus(project.files(testSourceSet.getOutput().getResourcesDir())); + } /** * Overriding getClasspath() in order to set the final classpath is an unusual solution, but it was the only @@ -83,47 +114,18 @@ public Void call() { */ @Override public @NotNull FileCollection getClasspath() { - final FileCollection originalClasspath = super.getClasspath(); - - final Project project = getProject(); - final JavaPluginExtension jpc = project.getExtensions().getByType(JavaPluginExtension.class); - final SourceSetContainer sourceSets = jpc.getSourceSets(); - final SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); - final SourceSet testSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME); - final SourceSet csaccessSourceSet = sourceSets.getByName(CustomSourceSetCreator.CSACCESS_SOURCESET_NAME); - final SourceSet csaccessTestSrcSet = sourceSets.getByName(CustomSourceSetCreator - .CSACCESSTEST_SOURCESET_NAME); - - final Dependency csDep = CheckstyleVersions.createCheckstyleDependency(project, csVersion); - final ConfigurationContainer configurations = project.getConfigurations(); - final Configuration detachedConfiguration = configurations.detachedConfiguration(csDep); - // workaround for Checkstyle#14123 - detachedConfiguration - .getResolutionStrategy() - .getCapabilitiesResolution() - .withCapability("com.google.collections", "google-collections", resolutionDetails -> resolutionDetails.select("com.google.guava:guava:0")); - final Set csJars = detachedConfiguration.getFiles(); - - FileCollection effectiveClasspath = project.files( - csaccessTestSrcSet.getOutput().getResourcesDir(), - csaccessSourceSet.getOutput().getResourcesDir(), - mainSourceSet.getOutput().getResourcesDir()) - .plus(csaccessTestSrcSet.getOutput().getClassesDirs()) - .plus(csaccessSourceSet.getOutput().getClassesDirs()) - .plus(mainSourceSet.getOutput().getClassesDirs()) - .plus(project.files(csJars)) - .plus(originalClasspath) - .minus(testSourceSet.getOutput().getClassesDirs()) - .minus(project.files(testSourceSet.getOutput().getResourcesDir())); + if (effectiveClassPath == null) { + throw new IllegalStateException("setCheckstyleVersion has not been called"); + } if (getLogger().isDebugEnabled()) { getLogger().debug("--------------------------------------------------------------------------"); getLogger().debug("Effective classpath of " + getName() + ":"); - for (File f : effectiveClasspath) { + for (File f : effectiveClassPath) { getLogger().debug("\t- " + f.getAbsolutePath()); } } - return effectiveClasspath; + return effectiveClassPath; } @Override diff --git a/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/GatherCheckstyleArtifactsTask.java b/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/GatherCheckstyleArtifactsTask.java index 3c511b61..4448f80d 100644 --- a/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/GatherCheckstyleArtifactsTask.java +++ b/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/GatherCheckstyleArtifactsTask.java @@ -28,6 +28,7 @@ public class GatherCheckstyleArtifactsTask extends DefaultTask { public static final String NAME = "gatherCheckstyleArtifacts"; + private final Map> rawVersionsToDependencies = new HashMap<>(); private final CheckstyleVersions csVersions; @OutputDirectory @@ -36,7 +37,6 @@ public class GatherCheckstyleArtifactsTask @OutputFile private final File classPathsInfoFile; - public GatherCheckstyleArtifactsTask() { super(); setGroup(LifecycleBasePlugin.BUILD_GROUP); @@ -50,19 +50,21 @@ public GatherCheckstyleArtifactsTask() { // Task Outputs: the directory full of JARs, and the classpath info file bundledJarsDir = getTemporaryDir(); classPathsInfoFile = new File(project.getLayout().getBuildDirectory().getAsFile().get(), "resources-generated/checkstyle-classpaths.properties"); - } + for (final String csVersion : csVersions.getVersions()) { + final Set dependencies = resolveDependencies(project, csVersion); + rawVersionsToDependencies.put(csVersion, dependencies); + } + } @TaskAction public void runTask() { final Set bundledFiles = new TreeSet<>(); final Properties classPaths = new SortedProperties(); - final Map> rawVersionsToDependencies = new HashMap<>(); final Set availableFileNames = new HashSet<>(); for (final String csVersion : csVersions.getVersions()) { - final Set dependencies = resolveDependencies(getProject(), csVersion); - rawVersionsToDependencies.put(csVersion, dependencies); + Set dependencies = rawVersionsToDependencies.get(csVersion); availableFileNames.addAll(dependencies.stream().map(File::getName).collect(toSet())); } @@ -109,7 +111,6 @@ private String convertToClassPath(final Collection resolvedDependencies) return sb.toString(); } - private void copyFiles(final Set bundledJars) { for (final File bundledJar : bundledJars) { try { @@ -120,7 +121,6 @@ private void copyFiles(final Set bundledJars) { } } - private void createClassPathsFile(final Properties classPaths) { //noinspection ResultOfMethodCallIgnored classPathsInfoFile.getParentFile().mkdir(); @@ -133,7 +133,6 @@ private void createClassPathsFile(final Properties classPaths) { } } - public File getBundledJarsDir() { return bundledJarsDir; } diff --git a/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/GradlePluginMain.java b/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/GradlePluginMain.java index a3396a0c..3d5f5022 100644 --- a/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/GradlePluginMain.java +++ b/buildSrc/src/main/java/org/infernus/idea/checkstyle/build/GradlePluginMain.java @@ -21,68 +21,64 @@ import org.gradle.language.base.plugins.LifecycleBasePlugin; import org.jetbrains.annotations.NotNull; +import static org.codehaus.groovy.runtime.DefaultGroovyMethods.println; + /** * The main plugin class. The action starts here. */ -public class GradlePluginMain - implements Plugin { +public class GradlePluginMain implements Plugin { public static final String CSLIB_TARGET_SUBFOLDER = "checkstyle/lib"; + private static final String CSCLASSES_TARGET_SUBFOLDER = "checkstyle/classes"; private CheckstyleVersions supportedCsVersions = null; - @Override - @SuppressWarnings("NullableProblems") - public void apply(final Project pProject) { - establishSourceSets(pProject); - readSupportedCheckstyleVersions(pProject); - configureTestTask((Test) pProject.getTasks().getByName(JavaPlugin.TEST_TASK_NAME)); - createCsAccessTestTask(pProject); - createCrossCheckTasks(pProject); - new CustomSourceSetCreator(pProject).setupCoverageVerification(); - createCheckstyleArtifactTasks(pProject); - wireIntellijPluginTasks(pProject); + public void apply(final Project project) { + Test testTask = (Test) project.getTasks().getByName(JavaPlugin.TEST_TASK_NAME); + configureTestTask(testTask); + + establishSourceSets(project); + readSupportedCheckstyleVersions(project); + createCsAccessTestTask(project); + createCrossCheckTasks(project); + createCheckstyleArtifactTasks(project); + new CustomSourceSetCreator(project).setupCoverageVerification(); + wireIntellijPluginTasks(project); } - - private void establishSourceSets(final Project pProject) { - final CustomSourceSetCreator sourceSetFactory = new CustomSourceSetCreator(pProject); + private void establishSourceSets(final Project project) { + final CustomSourceSetCreator sourceSetFactory = new CustomSourceSetCreator(project); sourceSetFactory.establishCsAccessSourceSet(); sourceSetFactory.establishCsAccessTestSourceSet(); } - - private void readSupportedCheckstyleVersions(final Project pProject) { - supportedCsVersions = new CheckstyleVersions(pProject); - pProject.getExtensions().getExtraProperties().set("supportedCsVersions", supportedCsVersions); + private void readSupportedCheckstyleVersions(final Project project) { + supportedCsVersions = new CheckstyleVersions(project); + project.getExtensions().getExtraProperties().set("supportedCsVersions", supportedCsVersions); } - - static void configureTestTask(final Test pTestTask) { - pTestTask.testLogging((TestLoggingContainer tlc) -> { + static void configureTestTask(final Test testTask) { + testTask.testLogging((TestLoggingContainer tlc) -> { tlc.setEvents(Collections.singleton(TestLogEvent.FAILED)); tlc.setShowStackTraces(true); tlc.setShowExceptions(true); tlc.setShowCauses(true); - //tlc.setShowStandardStreams(true); tlc.setExceptionFormat(TestExceptionFormat.FULL); }); - pTestTask.addTestListener(new TestSuiteStatsReporter(pTestTask.getLogger())); + testTask.addTestListener(new TestSuiteStatsReporter(testTask.getLogger())); } - - private void createCsAccessTestTask(final Project pProject) { - TaskProvider provider = pProject.getTasks().register(CsaccessTestTask.NAME, + private void createCsAccessTestTask(final Project project) { + TaskProvider provider = project.getTasks().register(CsaccessTestTask.NAME, CsaccessTestTask.class); provider.configure((CsaccessTestTask rct) -> { rct.setCheckstyleVersion(supportedCsVersions.getBaseVersion(), true); - pProject.getTasks().getByName(JavaPlugin.TEST_TASK_NAME).dependsOn(rct); + project.getTasks().getByName(JavaPlugin.TEST_TASK_NAME).dependsOn(rct); }); } - - private void createCrossCheckTasks(final Project pProject) { - final TaskContainer tasks = pProject.getTasks(); + private void createCrossCheckTasks(final Project project) { + final TaskContainer tasks = project.getTasks(); TaskProvider provider = tasks.register(CsaccessTestTask.XTEST_TASK_NAME); provider.configure((Task xtestTask) -> { @@ -104,27 +100,25 @@ private void createCrossCheckTasks(final Project pProject) { }); } - - private void createCheckstyleArtifactTasks(final Project pProject) { + private void createCheckstyleArtifactTasks(final Project project) { TaskProvider taskProvider = - pProject.getTasks().register(GatherCheckstyleArtifactsTask.NAME, GatherCheckstyleArtifactsTask.class); + project.getTasks().register(GatherCheckstyleArtifactsTask.NAME, GatherCheckstyleArtifactsTask.class); taskProvider.configure((GatherCheckstyleArtifactsTask task) -> { - pProject.getTasks().getByName(JavaPlugin.PROCESS_RESOURCES_TASK_NAME).dependsOn(task); + project.getTasks().getByName(JavaPlugin.PROCESS_RESOURCES_TASK_NAME).dependsOn(task); // Add generated classpath info file to resources - SourceSetContainer sourceSets = (SourceSetContainer) pProject.getProperties().get("sourceSets"); + SourceSetContainer sourceSets = (SourceSetContainer) project.getProperties().get("sourceSets"); SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); mainSourceSet.getResources().srcDir(task.getClassPathsInfoFile().getParentFile()); }); - createCopyCheckstyleArtifactsToSandboxTask(pProject, false); - createCopyCheckstyleArtifactsToSandboxTask(pProject, true); + createCopyCheckstyleArtifactsToSandboxTask(project, false); + createCopyCheckstyleArtifactsToSandboxTask(project, true); - createCopyClassesToSandboxTask(pProject, false); - createCopyClassesToSandboxTask(pProject, true); + createCopyClassesToSandboxTask(project, false); + createCopyClassesToSandboxTask(project, true); } - private void createCopyCheckstyleArtifactsToSandboxTask(final Project project, final boolean test) { final TaskContainer tasks = project.getTasks(); final String taskName = test ? "copyCheckstyleArtifactsToTestSandbox" : "copyCheckstyleArtifactsToSandbox"; @@ -163,11 +157,11 @@ private void forEachXTest(final TaskContainer tasks, final Consumer taskCo * This task makes the compiled classes and resources from the 'csaccess' sourceset available to the plugin by * copying it to the sandbox. Test code from csaccessTest sourceset not affected. * - * @param pProject the Gradle project + * @param project the Gradle project * @param test {@code true} if the target is the test sandbox, {@code false} for the main sandbox */ - private void createCopyClassesToSandboxTask(final Project pProject, final boolean test) { - final TaskContainer tasks = pProject.getTasks(); + private void createCopyClassesToSandboxTask(final Project project, final boolean test) { + final TaskContainer tasks = project.getTasks(); final String taskName = test ? "copyClassesToTestSandbox" : "copyClassesToSandbox"; final TaskProvider taskProvider = tasks.register(taskName, Copy.class); taskProvider.configure((Copy copyTask) -> { @@ -175,20 +169,21 @@ private void createCopyClassesToSandboxTask(final Project pProject, final boolea copyTask.setDescription("Copy classes from '" + CustomSourceSetCreator.CSACCESS_SOURCESET_NAME + "' sourceset into the prepared " + (test ? "test " : "") + "sandbox"); - final JavaPluginExtension jpc = pProject.getExtensions().getByType(JavaPluginExtension.class); + final JavaPluginExtension jpc = project.getExtensions().getByType(JavaPluginExtension.class); SourceSet csaccessSourceSet = jpc.getSourceSets().getByName(CustomSourceSetCreator.CSACCESS_SOURCESET_NAME); copyTask.dependsOn(tasks.getByName(csaccessSourceSet.getClassesTaskName())); if (test) { tasks.getByName(JavaPlugin.TEST_TASK_NAME).dependsOn(copyTask); tasks.getByName(CsaccessTestTask.NAME).dependsOn(copyTask); forEachXTest(tasks, xTask -> xTask.dependsOn(copyTask)); + copyTask.mustRunAfter(tasks.getByName("copyCheckstyleArtifactsToTestSandbox")); } else { tasks.getByName("buildSearchableOptions").dependsOn(copyTask); + copyTask.mustRunAfter(tasks.getByName("copyCheckstyleArtifactsToSandbox")); } - final String targetSubfolder = "checkstyle/classes"; copyTask.from(csaccessSourceSet.getOutput()); - copyTask.into(new File(pProject.getLayout().getBuildDirectory().getAsFile().get(), pluginSandboxDir(test, targetSubfolder))); + copyTask.into(new File(project.getLayout().getBuildDirectory().getAsFile().get(), pluginSandboxDir(test, CSCLASSES_TARGET_SUBFOLDER))); }); } @@ -202,10 +197,10 @@ private void createCopyClassesToSandboxTask(final Project pProject, final boolea /** * Defer some of the wiring until after the intellij plugin's tasks have been created. * - * @param pProject the Gradle project + * @param project the Gradle project */ - private void wireIntellijPluginTasks(final Project pProject) { - final TaskContainer tasks = pProject.getTasks(); + private void wireIntellijPluginTasks(final Project project) { + final TaskContainer tasks = project.getTasks(); tasks.all((Task task) -> { if ("buildPlugin".equals(task.getName()) || "runIdea".equals(task.getName()) || "runIde".equals(task.getName())) { task.dependsOn(tasks.getByName("copyCheckstyleArtifactsToSandbox")); diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 00000000..5ad69748 --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +org.gradle.configuration-cache=true diff --git a/settings.gradle.kts b/settings.gradle.kts index 5a5d5c7b..efdc394f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1 +1,7 @@ rootProject.name = "checkstyle-idea" + +dependencyResolutionManagement { + repositories { + mavenCentral() + } +}