Search in sources :

Example 1 with ClasspathBuilder

use of com.thomasjensen.checkstyle.addons.build.ClasspathBuilder in project checkstyle-addons by checkstyle-addons.

the class JarSonarqubeConfigAction method configureTaskFor.

@Override
protected void configureTaskFor(@Nonnull ShadowJar pJarTask, @Nullable DependencyConfig pDepConfig) {
    Objects.requireNonNull(pDepConfig, "required dependency config not present");
    pJarTask.setGroup(TaskCreator.ARTIFACTS_GROUP_NAME);
    pJarTask.setDescription("Assembles the SonarQube plugin for dependency configuration '" + pDepConfig.getName() + "'");
    final BuildConfigExtension buildConfig = buildUtil.getBuildConfig();
    final TaskContainer tasks = project.getTasks();
    // Inputs for up-to-date checking
    final TaskInputs inputs = pJarTask.getInputs();
    inputs.property(BuildUtil.GROUP_ID, project.getGroup());
    inputs.property(BuildUtil.VERSION, project.getVersion());
    inputs.property("name", buildConfig.getLongName());
    inputs.property("description", project.getDescription());
    inputs.property("authorName", buildConfig.getAuthorName());
    inputs.property("sqPluginKey", buildConfig.getSqPluginKey());
    inputs.property("sqPackage", buildConfig.getSqPackage());
    inputs.property("gitHash", buildConfig.getGitHash());
    inputs.property("orgUrl", buildConfig.getOrgUrl());
    inputs.property("issueTrackerUrl", buildConfig.getIssueTrackerUrl());
    inputs.property("website", buildConfig.getWebsite());
    // archive name
    pJarTask.getArchiveFileName().set("sonar-" + inputs.getProperties().get("sqPluginKey") + "-" + inputs.getProperties().get(BuildUtil.VERSION) + (pDepConfig.isDefaultConfig() ? "" : ("-csp" + pDepConfig.getSonarQubeMinCsPluginVersion())) + ".jar");
    // Task Dependencies
    final TaskProvider<Task> sqClassesTaskProvider = buildUtil.getTaskProvider(TaskNames.sonarqubeClasses, Task.class, pDepConfig);
    pJarTask.dependsOn(sqClassesTaskProvider);
    final VersionFileTask versionTask = (VersionFileTask) tasks.getByName(VersionFileTask.TASK_NAME);
    pJarTask.dependsOn(versionTask);
    final JavaCompile compileMainTask = buildUtil.getTask(TaskNames.compileJava, JavaCompile.class, pDepConfig);
    final SourceSet mainSourceSet = buildUtil.getSourceSet(SourceSet.MAIN_SOURCE_SET_NAME);
    final Copy resourcesMainTask = (Copy) tasks.getByName(mainSourceSet.getProcessResourcesTaskName());
    pJarTask.dependsOn(compileMainTask, resourcesMainTask);
    // Add dependencies as shadowed packages (in cooperation with jarSonarqubeRelocate task)
    final Configuration shadowCfg = new ClasspathBuilder(project).buildMainRuntimeConfiguration(pDepConfig, false);
    pJarTask.getConfigurations().add(shadowCfg);
    // Configuration of JAR file contents
    pJarTask.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE);
    pJarTask.into("META-INF", copySpec -> copySpec.from("LICENSE"));
    final JavaCompile compileSqTask = buildUtil.getTask(TaskNames.compileSonarqubeJava, JavaCompile.class, pDepConfig);
    pJarTask.from(compileSqTask.getDestinationDirectory());
    pJarTask.from(compileMainTask.getDestinationDirectory());
    pJarTask.from(resourcesMainTask.getDestinationDir(), copySpec -> {
        copySpec.include("checkstyle_packages.xml");
        copySpec.include("**/messages*.properties");
        copySpec.include("**/*.json");
    });
    pJarTask.into(inputs.getProperties().get("sqPackage"), copySpec -> copySpec.from(versionTask.getVersionFile()));
    // Manifest
    setManifestAttributes(pJarTask, pDepConfig);
}
Also used : SourceSet(org.gradle.api.tasks.SourceSet) Task(org.gradle.api.Task) TaskContainer(org.gradle.api.tasks.TaskContainer) TaskInputs(org.gradle.api.tasks.TaskInputs) Configuration(org.gradle.api.artifacts.Configuration) Copy(org.gradle.api.tasks.Copy) ClasspathBuilder(com.thomasjensen.checkstyle.addons.build.ClasspathBuilder) BuildConfigExtension(com.thomasjensen.checkstyle.addons.build.BuildConfigExtension) JavaCompile(org.gradle.api.tasks.compile.JavaCompile)

Example 2 with ClasspathBuilder

use of com.thomasjensen.checkstyle.addons.build.ClasspathBuilder in project checkstyle-addons by checkstyle-addons.

the class JarEclipseConfigAction method getPublishedDependencyLibs.

/**
 * Scan the dependencies of the specified configurations and return a list of File objects for each dependency.
 * Resolves the configurations if they are still unresolved.
 *
 * @param pTask the calling task
 * @param pDepConfig the current dependency configuration
 * @return list of files
 */
@Nonnull
private Set<File> getPublishedDependencyLibs(@Nonnull final Task pTask, @Nonnull final DependencyConfig pDepConfig) {
    Set<File> result = new HashSet<>();
    Configuration cfg = new ClasspathBuilder(pTask.getProject()).buildMainRuntimeConfiguration(pDepConfig);
    for (ResolvedDependency dep : cfg.getResolvedConfiguration().getFirstLevelModuleDependencies()) {
        if (!isCheckstyle(dep)) {
            for (ResolvedArtifact artifact : dep.getAllModuleArtifacts()) {
                result.add(artifact.getFile());
            }
        }
    }
    return result;
}
Also used : ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) Configuration(org.gradle.api.artifacts.Configuration) ClasspathBuilder(com.thomasjensen.checkstyle.addons.build.ClasspathBuilder) ResolvedDependency(org.gradle.api.artifacts.ResolvedDependency) File(java.io.File) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Example 3 with ClasspathBuilder

use of com.thomasjensen.checkstyle.addons.build.ClasspathBuilder in project checkstyle-addons by checkstyle-addons.

the class TestTaskConfigAction method configureTaskFor.

@Override
protected void configureTaskFor(@Nonnull Test pTestTask, @Nullable DependencyConfig pDepConfig) {
    Objects.requireNonNull(pDepConfig, "required dependency config not present");
    final String csVersion = getCsVersion();
    final String baseCsVersion = pDepConfig.getCheckstyleBaseVersion();
    final TaskContainer tasks = project.getTasks();
    final JavaVersion javaLevel = pDepConfig.getJavaLevel();
    pTestTask.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
    // Only produce JUnit XMLs for the default test task in order to minimize noise.
    pTestTask.getReports().getJunitXml().getRequired().set(Boolean.FALSE);
    if (baseCsVersion.equals(csVersion)) {
        pTestTask.setDescription("Run the unit tests using dependency configuration '" + pDepConfig.getName() + "' (Checkstyle " + baseCsVersion + ", Java level: " + javaLevel + ")");
    } else {
        pTestTask.setDescription("Run the unit tests compiled for Checkstyle " + baseCsVersion + " against a Checkstyle " + csVersion + " runtime (Java level: " + javaLevel + ")");
    }
    final SourceSet testSourceSet = buildUtil.getSourceSet(SourceSet.TEST_SOURCE_SET_NAME);
    final SourceSet mainSourceSet = buildUtil.getSourceSet(SourceSet.MAIN_SOURCE_SET_NAME);
    final SourceSet sqSourceSet = buildUtil.getSourceSet(BuildUtil.SONARQUBE_SOURCE_SET_NAME);
    pTestTask.dependsOn(buildUtil.getTaskProvider(TaskNames.compileTestJava, Task.class, pDepConfig), tasks.named(testSourceSet.getProcessResourcesTaskName()), buildUtil.getTaskProvider(TaskNames.compileJava, Task.class, pDepConfig), tasks.named(mainSourceSet.getProcessResourcesTaskName()), buildUtil.getTaskProvider(TaskNames.compileSonarqubeJava, Task.class, pDepConfig), tasks.named(sqSourceSet.getProcessResourcesTaskName()));
    new com.thomasjensen.checkstyle.addons.build.TestConfigAction().execute(pTestTask);
    final TaskProvider<JavaCompile> compileTaskProvider = buildUtil.getTaskProvider(TaskNames.compileTestJava, JavaCompile.class, pDepConfig);
    Provider<Directory> destDir = compileTaskProvider.flatMap(AbstractCompile::getDestinationDirectory);
    pTestTask.setTestClassesDirs(project.files(destDir));
    final JavaPluginExtension javaExt = project.getExtensions().getByType(JavaPluginExtension.class);
    pTestTask.getReports().getHtml().getOutputLocation().fileValue(new File(javaExt.getTestReportDir().getAsFile().get(), pTestTask.getName()));
    final JacocoTaskExtension jacoco = (JacocoTaskExtension) pTestTask.getExtensions().getByName(JacocoPluginExtension.TASK_EXTENSION_NAME);
    jacoco.setEnabled(false);
    pTestTask.setClasspath(new ClasspathBuilder(project).buildTestExecutionClasspath(pDepConfig, csVersion));
    final JavaLevelUtil javaLevelUtil = new JavaLevelUtil(project);
    if (javaLevelUtil.isOlderSupportedJava(javaLevel)) {
        pTestTask.setExecutable(javaLevelUtil.getJvmExecutable(javaLevel));
    }
    // Make the Checkstyle version available to the test cases via a system property.
    pTestTask.systemProperty(CSVERSION_SYSPROP_NAME, csVersion);
}
Also used : Task(org.gradle.api.Task) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) JavaVersion(org.gradle.api.JavaVersion) JacocoTaskExtension(org.gradle.testing.jacoco.plugins.JacocoTaskExtension) JavaLevelUtil(com.thomasjensen.checkstyle.addons.build.JavaLevelUtil) SourceSet(org.gradle.api.tasks.SourceSet) TaskContainer(org.gradle.api.tasks.TaskContainer) AbstractCompile(org.gradle.api.tasks.compile.AbstractCompile) ClasspathBuilder(com.thomasjensen.checkstyle.addons.build.ClasspathBuilder) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) File(java.io.File) Directory(org.gradle.api.file.Directory)

Example 4 with ClasspathBuilder

use of com.thomasjensen.checkstyle.addons.build.ClasspathBuilder in project checkstyle-addons by checkstyle-addons.

the class CompileConfigAction method configureTaskFor.

@Override
protected void configureTaskFor(@Nonnull JavaCompile pCompileTask, @Nullable DependencyConfig pDepConfig) {
    Objects.requireNonNull(pDepConfig, "required dependency config not present");
    final SourceSet sourceSetToCompile = getSourceSetToCompile();
    final JavaVersion javaLevel = pDepConfig.getJavaLevel();
    pCompileTask.setGroup(BasePlugin.BUILD_GROUP);
    pCompileTask.setDescription("Compile sources from '" + sourceSetToCompile.getName() + "' source set using dependency configuration '" + pDepConfig.getName() + "' (Java level: " + javaLevel + ")");
    // Additional Task Input: the dependency configuration file
    pCompileTask.getInputs().file(pDepConfig.getConfigFile());
    final CompileOptions options = pCompileTask.getOptions();
    options.setEncoding(StandardCharsets.UTF_8.toString());
    // show deprecation warnings in compiler output
    options.setDeprecation(true);
    final JavaLevelUtil javaLevelUtil = new JavaLevelUtil(project);
    if (javaLevelUtil.isOlderSupportedJava(javaLevel)) {
        options.setFork(true);
        options.getForkOptions().setExecutable(javaLevelUtil.getCompilerExecutable(javaLevel));
    }
    final File destDir = calculateDestDirFromSourceSet(sourceSetToCompile, pDepConfig.getName());
    pCompileTask.setSource(sourceSetToCompile.getAllJava());
    pCompileTask.getDestinationDirectory().set(destDir);
    pCompileTask.setSourceCompatibility(javaLevel.toString());
    pCompileTask.setTargetCompatibility(javaLevel.toString());
    FileCollection cp = new ClasspathBuilder(project).buildCompileClasspath(pDepConfig, sourceSetToCompile.getName());
    pCompileTask.setClasspath(cp);
    if (isTest(sourceSetToCompile)) {
        TaskProvider<Task> mainCompile = buildUtil.getTaskProvider(TaskNames.compileJava, Task.class, pDepConfig);
        TaskProvider<Task> sonarqubeCompile = buildUtil.getTaskProvider(TaskNames.compileSonarqubeJava, Task.class, pDepConfig);
        pCompileTask.dependsOn(mainCompile, sonarqubeCompile);
    }
}
Also used : SourceSet(org.gradle.api.tasks.SourceSet) Task(org.gradle.api.Task) ClasspathBuilder(com.thomasjensen.checkstyle.addons.build.ClasspathBuilder) CompileOptions(org.gradle.api.tasks.compile.CompileOptions) JavaVersion(org.gradle.api.JavaVersion) JavaLevelUtil(com.thomasjensen.checkstyle.addons.build.JavaLevelUtil) FileCollection(org.gradle.api.file.FileCollection) File(java.io.File)

Example 5 with ClasspathBuilder

use of com.thomasjensen.checkstyle.addons.build.ClasspathBuilder in project checkstyle-addons by checkstyle-addons.

the class FatJarConfigAction method configureTaskFor.

@Override
protected void configureTaskFor(@Nonnull ShadowJar pFatJarTask, @Nullable DependencyConfig pDepConfig) {
    Objects.requireNonNull(pDepConfig, "required dependency config not present");
    pFatJarTask.setGroup(TaskCreator.ARTIFACTS_GROUP_NAME);
    pFatJarTask.getArchiveClassifier().set("all");
    // set appendix for archive name
    final String appendix = pDepConfig.getName();
    if (!pDepConfig.isDefaultConfig()) {
        pFatJarTask.getArchiveAppendix().set(appendix);
    }
    // dependency on the corresponding (thin) Jar task
    final Jar thinJarTask = buildUtil.getTask(TaskNames.jar, Jar.class, pDepConfig);
    pFatJarTask.dependsOn(thinJarTask);
    pFatJarTask.setDescription("Create a combined JAR of project and runtime dependencies of '" + SourceSet.MAIN_SOURCE_SET_NAME + "' for dependency configuration '" + pDepConfig.getName() + "'");
    buildUtil.inheritManifest(pFatJarTask, pDepConfig);
    pFatJarTask.from(thinJarTask.getArchiveFile());
    Configuration cfg = new ClasspathBuilder(project).buildMainRuntimeConfiguration(pDepConfig);
    pFatJarTask.setConfigurations(Collections.singletonList(cfg));
    pFatJarTask.exclude("META-INF/INDEX.LIST", "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA", "META-INF/maven/**/*", "pom.xml");
    final Set<String> deps = getNonCheckstyleDeps(cfg);
    if (deps.size() > 0) {
        // append all licenses found in dependent Jars into one
        pFatJarTask.append("META-INF/LICENSE");
        pFatJarTask.dependencies((final DependencyFilter pDependencyFilter) -> {
            for (String dep : deps) {
                pDependencyFilter.include(pDependencyFilter.dependency(dep));
            }
        });
    }
}
Also used : Configuration(org.gradle.api.artifacts.Configuration) ClasspathBuilder(com.thomasjensen.checkstyle.addons.build.ClasspathBuilder) ShadowJar(com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) Jar(org.gradle.api.tasks.bundling.Jar) DependencyFilter(com.github.jengelman.gradle.plugins.shadow.internal.DependencyFilter)

Aggregations

ClasspathBuilder (com.thomasjensen.checkstyle.addons.build.ClasspathBuilder)8 File (java.io.File)4 Task (org.gradle.api.Task)4 Configuration (org.gradle.api.artifacts.Configuration)4 SourceSet (org.gradle.api.tasks.SourceSet)4 TaskContainer (org.gradle.api.tasks.TaskContainer)3 BuildConfigExtension (com.thomasjensen.checkstyle.addons.build.BuildConfigExtension)2 BuildUtil (com.thomasjensen.checkstyle.addons.build.BuildUtil)2 JavaLevelUtil (com.thomasjensen.checkstyle.addons.build.JavaLevelUtil)2 Nonnull (javax.annotation.Nonnull)2 JavaVersion (org.gradle.api.JavaVersion)2 Project (org.gradle.api.Project)2 Jar (org.gradle.api.tasks.bundling.Jar)2 JavaCompile (org.gradle.api.tasks.compile.JavaCompile)2 DependencyFilter (com.github.jengelman.gradle.plugins.shadow.internal.DependencyFilter)1 ShadowJar (com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar)1 DependencyConfig (com.thomasjensen.checkstyle.addons.build.DependencyConfig)1 TaskCreator (com.thomasjensen.checkstyle.addons.build.TaskCreator)1 TaskNames (com.thomasjensen.checkstyle.addons.build.TaskNames)1 Node (groovy.util.Node)1