org.gradle.api.plugins.JavaPluginConvention Java Examples

The following examples show how to use org.gradle.api.plugins.JavaPluginConvention. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AspectJPlugin.java    From gradle-plugins with MIT License 7 votes vote down vote up
@Override
public void apply(Project project) {
    this.project = project;
    project.getPlugins().apply(AspectJBasePlugin.class);
    project.getPlugins().apply(JavaBasePlugin.class);

    JavaPluginConvention plugin = project.getConvention().getPlugin(JavaPluginConvention.class);

    plugin.getSourceSets().all(this::configureSourceSet);

    project.getPlugins().withType(JavaPlugin.class, javaPlugin -> {

        SourceSet main = plugin.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
        SourceSet test = plugin.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME);

        DefaultAspectjSourceSet mainAj = new DslObject(main).getConvention().getPlugin(DefaultAspectjSourceSet.class);
        DefaultAspectjSourceSet testAj = new DslObject(test).getConvention().getPlugin(DefaultAspectjSourceSet.class);

        Configuration aspectpath = project.getConfigurations().getByName(mainAj.getAspectConfigurationName());
        Configuration testAspectpath = project.getConfigurations().getByName(testAj.getAspectConfigurationName());

        testAspectpath.extendsFrom(aspectpath);

        testAj.setAspectPath(project.getObjects().fileCollection().from(main.getOutput(), testAspectpath));
    });
}
 
Example #2
Source File: SourcesJarPlugin.java    From gradle-plugins with MIT License 7 votes vote down vote up
@Override
public void apply(Project project) {

    project.getLogger().warn("io.freefair.sources-jar is deprecated. Use java.withSourcesJar() instead");

    project.getPluginManager().withPlugin("java", appliedPlugin -> {
        sourcesJar = project.getTasks().register("sourcesJar", Jar.class, sourcesJar -> {
            sourcesJar.setDescription("Assembles a jar archive containing the sources.");
            sourcesJar.getArchiveClassifier().set("sources");
            sourcesJar.setGroup(BasePlugin.BUILD_GROUP);

            sourcesJar.dependsOn(project.getTasks().named(JavaPlugin.CLASSES_TASK_NAME));

            JavaPluginConvention javaPluginConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
            DefaultSourceSet mainSourceSet = (DefaultSourceSet) javaPluginConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
            sourcesJar.from(mainSourceSet.getAllSource());
        });

        project.getArtifacts().add(Dependency.ARCHIVES_CONFIGURATION, sourcesJar);
    });
}
 
Example #3
Source File: SourcesJarPlugin.java    From gradle-plugins with MIT License 6 votes vote down vote up
@Override
public void apply(Project project) {

    project.getLogger().warn("io.freefair.sources-jar is deprecated. Use java.withSourcesJar() instead");

    project.getPluginManager().withPlugin("java", appliedPlugin -> {
        sourcesJar = project.getTasks().register("sourcesJar", Jar.class, sourcesJar -> {
            sourcesJar.setDescription("Assembles a jar archive containing the sources.");
            sourcesJar.getArchiveClassifier().set("sources");
            sourcesJar.setGroup(BasePlugin.BUILD_GROUP);

            sourcesJar.dependsOn(project.getTasks().named(JavaPlugin.CLASSES_TASK_NAME));

            JavaPluginConvention javaPluginConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
            DefaultSourceSet mainSourceSet = (DefaultSourceSet) javaPluginConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
            sourcesJar.from(mainSourceSet.getAllSource());
        });

        project.getArtifacts().add(Dependency.ARCHIVES_CONFIGURATION, sourcesJar);
    });
}
 
Example #4
Source File: CpdPluginTest.java    From gradle-cpd-plugin with Apache License 2.0 6 votes vote down vote up
@Test
void CpdPlugin_shouldSetCpdCheckSourceEqualsToMainAndTestSourceSetsIfJavaPluginIsApplied(Project project, TaskProvider<Cpd> cpd) {
    // Given:
    String mainFile = "src/main/java/Clazz.java";
    String testFile = "src/test/java/ClazzTest.java";

    // When:
    project.getPlugins().apply(JavaPlugin.class);
    createProjectFiles(project, mainFile, "src/resources/java/message.properties", testFile);

    project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME, sourceSet -> {
        sourceSet.getJava().srcDir(testFile(JAVA, "de/aaschmid/annotation"));
        sourceSet.getAllJava().srcDir(testFile(JAVA, "de/aaschmid/clazz"));
        sourceSet.getResources().srcDir(testFile(JAVA, "de/aaschmid/foo"));
    });
    project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME, sourceSet ->
            sourceSet.getJava().srcDir(testFile(JAVA, "de/aaschmid/test")));

    // Then:
    List<File> expected = testFilesRecurseIn(JAVA, "de/aaschmid/annotation", "de/aaschmid/clazz", "de/aaschmid/test");
    expected.add(project.file(mainFile));
    expected.add(project.file(testFile));

    assertThat(cpd.get().getSource()).containsExactlyInAnyOrderElementsOf(expected);
}
 
Example #5
Source File: OptionalDependenciesPlugin.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(Project project) {
	Configuration optional = project.getConfigurations().create(OPTIONAL_CONFIGURATION_NAME);
	optional.attributes((attributes) -> attributes.attribute(Usage.USAGE_ATTRIBUTE,
			project.getObjects().named(Usage.class, Usage.JAVA_RUNTIME)));
	project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> {
		SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class)
		                                       .getSourceSets();
		sourceSets.all((sourceSet) -> {
			sourceSet.setCompileClasspath(sourceSet.getCompileClasspath().plus(optional));
			sourceSet.setRuntimeClasspath(sourceSet.getRuntimeClasspath().plus(optional));
		});
		project.getTasks().withType(Javadoc.class)
		       .all((javadoc) -> javadoc.setClasspath(javadoc.getClasspath().plus(optional)));
	});
	project.getPlugins().withType(EclipsePlugin.class,
			(eclipsePlugin) -> project.getExtensions().getByType(EclipseModel.class)
			                          .classpath((classpath) -> classpath.getPlusConfigurations().add(optional)));
}
 
Example #6
Source File: ModuleName.java    From gradle-modules-plugin with MIT License 6 votes vote down vote up
Optional<String> findModuleName(Project project) {
    SourceSet main;
    try {
        JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
        main = javaConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
    } catch (IllegalStateException | UnknownDomainObjectException e) {
        LOGGER.warn("Cannot obtain JavaPluginConvention", e);
        return Optional.empty();
    }

    Optional<Path> moduleInfoJava = main.getAllJava()
            .getSourceDirectories()
            .getFiles()
            .stream()
            .map(sourceDir -> sourceDir.toPath().resolve("module-info.java"))
            .filter(Files::exists)
            .findAny();

    String projectPath = project.getPath();
    if (moduleInfoJava.isEmpty()) {
        LOGGER.lifecycle("Project {} => no module-info.java found", projectPath);
        return Optional.empty();
    }

    return findModuleName(moduleInfoJava.get(), projectPath);
}
 
Example #7
Source File: CodeServerBuilder.java    From putnami-gradle-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Collection<File> listProjectDepsSrcDirs(Project project) {
	ConfigurationContainer configs = project.getConfigurations();
	Configuration compileConf = configs.getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME);
	DependencySet depSet = compileConf.getAllDependencies();

	List<File> result = Lists.newArrayList();
	for (Dependency dep : depSet) {
		if (dep instanceof ProjectDependency) {
			Project projectDependency = ((ProjectDependency) dep).getDependencyProject();
			if (projectDependency.getPlugins().hasPlugin(PwtLibPlugin.class)) {
				JavaPluginConvention javaConvention = projectDependency.getConvention().getPlugin(JavaPluginConvention.class);
				SourceSet mainSourceSet = javaConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);

				result.addAll(mainSourceSet.getAllSource().getSrcDirs());
			}
		}
	}
	return result;
}
 
Example #8
Source File: JSassJavaPlugin.java    From gradle-plugins with MIT License 6 votes vote down vote up
@Override
public void apply(Project project) {
    project.getPlugins().apply(JSassWebjarsPlugin.class);

    project.getPlugins().apply(JavaPlugin.class);

    File baseDestinationDir = new File(project.getBuildDir(), "jsass");

    project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(sourceSet -> {
        String taskName = sourceSet.getCompileTaskName("Sass");

        TaskProvider<SassCompile> sassCompileTaskProvider = project.getTasks().register(taskName, SassCompile.class, sassCompile -> {
            sassCompile.setSource(sourceSet.getResources());
            sassCompile.getDestinationDir().set(new File(baseDestinationDir, sourceSet.getName()));
            sassCompile.setGroup(BasePlugin.BUILD_GROUP);
            sassCompile.setDescription("Compile sass and scss files for the " + sourceSet.getName() + " source set");
        });

        project.getTasks().named(sourceSet.getProcessResourcesTaskName(), ProcessResources.class)
                .configure(processResources -> processResources.from(sassCompileTaskProvider));
    });
}
 
Example #9
Source File: MavenProjectWrapper.java    From gradle-plugins with MIT License 6 votes vote down vote up
public MavenProjectWrapper(Project project, File pomFile) throws IOException, XmlPullParserException {
    this.project = project;
    this.pomFile = pomFile;

    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model = reader.read(new FileReader(pomFile));

    setModel(model);

    getBuild().setDirectory(project.getBuildDir().getAbsolutePath());

    SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();

    SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
    getBuild().setSourceDirectory(main.getJava().getSrcDirs().iterator().next().getAbsolutePath());
    getBuild().setOutputDirectory(main.getJava().getOutputDir().getAbsolutePath());

    SourceSet test = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME);
    getBuild().setTestSourceDirectory(test.getJava().getSrcDirs().iterator().next().getAbsolutePath());
    getBuild().setTestOutputDirectory(test.getJava().getOutputDir().getAbsolutePath());

    setArtifact(new ProjectArtifact(this));
}
 
Example #10
Source File: LombokPlugin.java    From gradle-plugins with MIT License 6 votes vote down vote up
private void configureForSpotbugs(JavaPluginConvention javaPluginConvention) {
    lombokBasePlugin.getLombokExtension().getConfig().put("lombok.extern.findbugs.addSuppressFBWarnings", "true");

    project.afterEvaluate(p -> {
        Object spotbugsExtension = project.getExtensions().getByName("spotbugs");

        String toolVersion;
        if (spotbugsExtension instanceof CodeQualityExtension) {
            toolVersion = ((CodeQualityExtension) spotbugsExtension).getToolVersion();
        }
        else {
            Property<String> toolVersionProperty = (Property<String>) new DslObject(spotbugsExtension).getAsDynamicObject().getProperty("toolVersion");
            toolVersion = toolVersionProperty.get();
        }

        javaPluginConvention.getSourceSets().all(sourceSet ->
                project.getDependencies().add(
                        sourceSet.getCompileOnlyConfigurationName(),
                        "com.github.spotbugs:spotbugs-annotations:" + toolVersion
                ));
    });
}
 
Example #11
Source File: SassCompileTask.java    From GradleSassPlugin with Apache License 2.0 6 votes vote down vote up
public void registerInSourceSets(String ...sourceSetNames) {
    if (sourceSetNames == null || sourceSetNames.length == 0) return;

    try {
        JavaPluginConvention javaPlugin = getProject().getConvention().getPlugin(JavaPluginConvention.class);
        if (javaPlugin == null) {
            throw new GradleException("You must apply the java plugin if you're using 'registerInSourceSets' functionality.");
        }

        for (String sourceSet : sourceSetNames) {
            javaPlugin.getSourceSets().getByName(sourceSet).getOutput().dir(
                    Collections.singletonMap("builtBy", this),
                    getOutDir()
            );
        }
    } catch (Exception e) {
        throw new GradleException("You must apply the java plugin if you're using 'registerInSourceSets' functionality.");
    }
}
 
Example #12
Source File: JSassJavaPlugin.java    From gradle-plugins with MIT License 6 votes vote down vote up
@Override
public void apply(Project project) {
    project.getPlugins().apply(JSassWebjarsPlugin.class);

    project.getPlugins().apply(JavaPlugin.class);

    File baseDestinationDir = new File(project.getBuildDir(), "jsass");

    project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(sourceSet -> {
        String taskName = sourceSet.getCompileTaskName("Sass");

        TaskProvider<SassCompile> sassCompileTaskProvider = project.getTasks().register(taskName, SassCompile.class, sassCompile -> {
            sassCompile.setSource(sourceSet.getResources());
            sassCompile.getDestinationDir().set(new File(baseDestinationDir, sourceSet.getName()));
            sassCompile.setGroup(BasePlugin.BUILD_GROUP);
            sassCompile.setDescription("Compile sass and scss files for the " + sourceSet.getName() + " source set");
        });

        project.getTasks().named(sourceSet.getProcessResourcesTaskName(), ProcessResources.class)
                .configure(processResources -> processResources.from(sassCompileTaskProvider));
    });
}
 
Example #13
Source File: MavenProjectWrapper.java    From gradle-plugins with MIT License 6 votes vote down vote up
public MavenProjectWrapper(Project project, File pomFile) throws IOException, XmlPullParserException {
    this.project = project;
    this.pomFile = pomFile;

    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model = reader.read(new FileReader(pomFile));

    setModel(model);

    getBuild().setDirectory(project.getBuildDir().getAbsolutePath());

    SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();

    SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
    getBuild().setSourceDirectory(main.getJava().getSrcDirs().iterator().next().getAbsolutePath());
    getBuild().setOutputDirectory(main.getJava().getOutputDir().getAbsolutePath());

    SourceSet test = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME);
    getBuild().setTestSourceDirectory(test.getJava().getSrcDirs().iterator().next().getAbsolutePath());
    getBuild().setTestOutputDirectory(test.getJava().getOutputDir().getAbsolutePath());

    setArtifact(new ProjectArtifact(this));
}
 
Example #14
Source File: LombokPlugin.java    From gradle-plugins with MIT License 6 votes vote down vote up
private void configureForSpotbugs(JavaPluginConvention javaPluginConvention) {
    lombokBasePlugin.getLombokExtension().getConfig().put("lombok.extern.findbugs.addSuppressFBWarnings", "true");

    project.afterEvaluate(p -> {
        Object spotbugsExtension = project.getExtensions().getByName("spotbugs");

        String toolVersion;
        if (spotbugsExtension instanceof CodeQualityExtension) {
            toolVersion = ((CodeQualityExtension) spotbugsExtension).getToolVersion();
        }
        else {
            Property<String> toolVersionProperty = (Property<String>) new DslObject(spotbugsExtension).getAsDynamicObject().getProperty("toolVersion");
            toolVersion = toolVersionProperty.get();
        }

        javaPluginConvention.getSourceSets().all(sourceSet ->
                project.getDependencies().add(
                        sourceSet.getCompileOnlyConfigurationName(),
                        "com.github.spotbugs:spotbugs-annotations:" + toolVersion
                ));
    });
}
 
Example #15
Source File: AspectJPlugin.java    From gradle-plugins with MIT License 6 votes vote down vote up
@Override
public void apply(Project project) {
    this.project = project;
    project.getPlugins().apply(AspectJBasePlugin.class);
    project.getPlugins().apply(JavaBasePlugin.class);

    JavaPluginConvention plugin = project.getConvention().getPlugin(JavaPluginConvention.class);

    plugin.getSourceSets().all(this::configureSourceSet);

    project.getPlugins().withType(JavaPlugin.class, javaPlugin -> {

        SourceSet main = plugin.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
        SourceSet test = plugin.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME);

        DefaultAspectjSourceSet mainAj = new DslObject(main).getConvention().getPlugin(DefaultAspectjSourceSet.class);
        DefaultAspectjSourceSet testAj = new DslObject(test).getConvention().getPlugin(DefaultAspectjSourceSet.class);

        Configuration aspectpath = project.getConfigurations().getByName(mainAj.getAspectConfigurationName());
        Configuration testAspectpath = project.getConfigurations().getByName(testAj.getAspectConfigurationName());

        testAspectpath.extendsFrom(aspectpath);

        testAj.setAspectPath(project.getObjects().fileCollection().from(main.getOutput(), testAspectpath));
    });
}
 
Example #16
Source File: AggregateJacocoReportPlugin.java    From gradle-plugins with MIT License 5 votes vote down vote up
@Override
public void apply(Project project) {
    project.getPlugins().apply(JacocoPlugin.class);

    project.getTasks().register("aggregateJacocoReport", JacocoReport.class, reportTask -> {

        reportTask.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
        reportTask.setDescription(String.format("Generates aggregated code coverage report for the %s project.", project.getPath()));

        project.allprojects(subproject -> {
            subproject.getPlugins().withType(JavaPlugin.class, javaPlugin -> {
                SourceSetContainer sourceSets = subproject.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
                SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
                reportTask.sourceSets(main);
            });

            subproject.getTasks()
                    .withType(Test.class)
                    .forEach(reportTask::executionData);
        });

        JacocoPluginExtension reportingExtension = project.getExtensions().getByType(JacocoPluginExtension.class);
        reportTask.getReports().getHtml().setEnabled(true);
        reportTask.getReports().all(report -> {
            if (report.getOutputType().equals(Report.OutputType.DIRECTORY)) {
                report.setDestination(project.provider(() -> new File(reportingExtension.getReportsDir(), reportTask.getName() + "/" + report.getName())));
            }
            else {
                report.setDestination(project.provider(() -> new File(reportingExtension.getReportsDir(), reportTask.getName() + "/" + reportTask.getName() + "." + report.getName())));
            }
        });
    });


}
 
Example #17
Source File: GwtCheckTask.java    From putnami-gradle-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void configure(final Project project, final PutnamiExtension extention) {
	final CompilerOption options = extention.getCompile();

	options.init(getProject());

	JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
	SourceSet mainSourceSet = javaConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
	final FileCollection sources = getProject()
		.files(project.files(mainSourceSet.getOutput().getResourcesDir()))
		.plus(project.files(mainSourceSet.getOutput().getClassesDirs()))
		.plus(getProject().files(mainSourceSet.getAllSource().getSrcDirs()));

	ConventionMapping mapping = ((IConventionAware) this).getConventionMapping();

	mapping.map("modules", new Callable<List<String>>() {
		@Override
		public List<String> call() {
			return extention.getModule();
		}
	});
	mapping.map("war", new Callable<File>() {
		@Override
		public File call()  {
			return new File(getProject().getBuildDir(), "out");
		}
	});
	mapping.map("src", new Callable<FileCollection>() {
		@Override
		public FileCollection call()  {
			return sources;
		}
	});
}
 
Example #18
Source File: MavenPluginPlugin.java    From gradle-plugins with MIT License 5 votes vote down vote up
@Override
public void apply(Project project) {
    project.getPlugins().apply(JavaPlugin.class);
    MavenPublishJavaPlugin mavenPublishJavaPlugin = project.getPlugins().apply(MavenPublishJavaPlugin.class);

    // https://github.com/gradle/gradle/issues/10555#issue-492150084
    if (project.getGradle().getGradleVersion().matches("5\\.6(\\.[12])?")) {
        mavenPublishJavaPlugin.getPublication().getPom().withXml(xmlProvider ->
                xmlProvider.asNode().appendNode("packaging", "maven-plugin")
        );
    }
    else {
        mavenPublishJavaPlugin.getPublication().getPom().setPackaging("maven-plugin");
    }

    TaskProvider<GenerateMavenPom> generateMavenPom = project.getTasks().named("generatePomFileForMavenJavaPublication", GenerateMavenPom.class);

    TaskProvider<DescriptorGeneratorTask> descriptorGeneratorTaskProvider = project.getTasks().register("generateMavenPluginDescriptor", DescriptorGeneratorTask.class, generateMavenPluginDescriptor -> {

        generateMavenPluginDescriptor.dependsOn(generateMavenPom);
        generateMavenPluginDescriptor.getPomFile().set(generateMavenPom.get().getDestination());

        generateMavenPluginDescriptor.getOutputDirectory().set(
                project.getLayout().getBuildDirectory().dir("maven-plugin")
        );

        SourceSet main = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName("main");
        generateMavenPluginDescriptor.getSourceDirectories().from(main.getAllJava().getSourceDirectories());
        JavaCompile javaCompile = (JavaCompile) project.getTasks().getByName(main.getCompileJavaTaskName());

        generateMavenPluginDescriptor.getClassesDirectories().from(javaCompile);
        generateMavenPluginDescriptor.getEncoding().convention(javaCompile.getOptions().getEncoding());
    });

    project.getTasks().named(JavaPlugin.PROCESS_RESOURCES_TASK_NAME, ProcessResources.class)
            .configure(processResources -> processResources.from(descriptorGeneratorTaskProvider));
}
 
Example #19
Source File: GwtCompileTask.java    From putnami-gradle-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void addSourceSet(FileCollection sources, Project project, String sourceSet) {
	JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
	SourceSet mainSourceSet = javaConvention.getSourceSets().getByName(sourceSet);
	sources.add(project.files(mainSourceSet.getOutput().getResourcesDir()))
		.plus(project.files(mainSourceSet.getOutput().getClassesDirs()))
		.plus(project.files(mainSourceSet.getAllSource().getSrcDirs()));
}
 
Example #20
Source File: CpdPlugin.java    From gradle-cpd-plugin with Apache License 2.0 5 votes vote down vote up
private void createTask(Project project) {
    TaskProvider<Cpd> taskProvider = project.getTasks().register(TASK_NAME_CPD_CHECK, Cpd.class, task -> {
        task.setDescription("Run CPD analysis for all sources");
        project.getAllprojects().forEach(p ->
                p.getPlugins().withType(JavaBasePlugin.class, plugin ->
                        p.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(sourceSet ->
                                sourceSet.getAllJava().getSrcDirs().forEach(task::source)
                        )
                )
        );
    });

    project.getPlugins().withType(LifecycleBasePlugin.class, plugin ->
            project.getTasks().findByName(LifecycleBasePlugin.CHECK_TASK_NAME).dependsOn(taskProvider));
}
 
Example #21
Source File: EntitasGradleProject.java    From Entitas-Java with MIT License 5 votes vote down vote up
public EntitasGradleProject(Project gradleProject) {
    this.project = gradleProject;
    //this.project.getPlugins().apply(JavaPlugin.class);
    javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
    extension = project.getExtensions().findByType(CodeGenerationPluginExtension.class);
    if (extension == null) {
        extension = new CodeGenerationPluginExtension();
    }


}
 
Example #22
Source File: ClasspathScanPlugin.java    From joinfaces with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Project project) {
	this.project = project;

	project.getPlugins().withType(JavaPlugin.class, javaPlugin ->
		project.getConvention()
			.getPlugin(JavaPluginConvention.class)
			.getSourceSets()
			.all(this::configureClasspathScan)
	);
}
 
Example #23
Source File: CpdPluginTest.java    From gradle-cpd-plugin with Apache License 2.0 5 votes vote down vote up
@Test
void CpdPlugin_shouldAddCpdCheckTaskAsDependencyOfCheckLifecycleTaskIfJavaPluginIsApplied(Project project, TaskProvider<Cpd> cpdCheck) {
    // When:
    project.getPlugins().apply(JavaBasePlugin.class);

    project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().create("tmp", (SourceSet sourceSet) ->
            sourceSet.getJava().srcDir(testFile(JAVA, ".")));

    Task checkTask = project.getTasks().getByName("check");

    // Then:
    assertThat((Set<Task>) checkTask.getTaskDependencies().getDependencies(checkTask)).contains(cpdCheck.get());
    assertThat(cpdCheck.get().getSource()).containsExactlyInAnyOrderElementsOf(testFilesRecurseIn(JAVA, "."));
}
 
Example #24
Source File: MavenPluginPlugin.java    From gradle-plugins with MIT License 5 votes vote down vote up
@Override
public void apply(Project project) {
    project.getPlugins().apply(JavaPlugin.class);
    MavenPublishJavaPlugin mavenPublishJavaPlugin = project.getPlugins().apply(MavenPublishJavaPlugin.class);

    // https://github.com/gradle/gradle/issues/10555#issue-492150084
    if (project.getGradle().getGradleVersion().matches("5\\.6(\\.[12])?")) {
        mavenPublishJavaPlugin.getPublication().getPom().withXml(xmlProvider ->
                xmlProvider.asNode().appendNode("packaging", "maven-plugin")
        );
    }
    else {
        mavenPublishJavaPlugin.getPublication().getPom().setPackaging("maven-plugin");
    }

    TaskProvider<GenerateMavenPom> generateMavenPom = project.getTasks().named("generatePomFileForMavenJavaPublication", GenerateMavenPom.class);

    TaskProvider<DescriptorGeneratorTask> descriptorGeneratorTaskProvider = project.getTasks().register("generateMavenPluginDescriptor", DescriptorGeneratorTask.class, generateMavenPluginDescriptor -> {

        generateMavenPluginDescriptor.dependsOn(generateMavenPom);
        generateMavenPluginDescriptor.getPomFile().set(generateMavenPom.get().getDestination());

        generateMavenPluginDescriptor.getOutputDirectory().set(
                project.getLayout().getBuildDirectory().dir("maven-plugin")
        );

        SourceSet main = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName("main");
        generateMavenPluginDescriptor.getSourceDirectories().from(main.getAllJava().getSourceDirectories());
        JavaCompile javaCompile = (JavaCompile) project.getTasks().getByName(main.getCompileJavaTaskName());

        generateMavenPluginDescriptor.getClassesDirectories().from(javaCompile);
        generateMavenPluginDescriptor.getEncoding().convention(javaCompile.getOptions().getEncoding());
    });

    project.getTasks().named(JavaPlugin.PROCESS_RESOURCES_TASK_NAME, ProcessResources.class)
            .configure(processResources -> processResources.from(descriptorGeneratorTaskProvider));
}
 
Example #25
Source File: AggregateJacocoReportPlugin.java    From gradle-plugins with MIT License 5 votes vote down vote up
@Override
public void apply(Project project) {
    project.getPlugins().apply(JacocoPlugin.class);

    project.getTasks().register("aggregateJacocoReport", JacocoReport.class, reportTask -> {

        reportTask.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
        reportTask.setDescription(String.format("Generates aggregated code coverage report for the %s project.", project.getPath()));

        project.allprojects(subproject -> {
            subproject.getPlugins().withType(JavaPlugin.class, javaPlugin -> {
                SourceSetContainer sourceSets = subproject.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
                SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
                reportTask.sourceSets(main);
            });

            subproject.getTasks()
                    .withType(Test.class)
                    .forEach(reportTask::executionData);
        });

        JacocoPluginExtension reportingExtension = project.getExtensions().getByType(JacocoPluginExtension.class);
        reportTask.getReports().getHtml().setEnabled(true);
        reportTask.getReports().all(report -> {
            if (report.getOutputType().equals(Report.OutputType.DIRECTORY)) {
                report.setDestination(project.provider(() -> new File(reportingExtension.getReportsDir(), reportTask.getName() + "/" + report.getName())));
            }
            else {
                report.setDestination(project.provider(() -> new File(reportingExtension.getReportsDir(), reportTask.getName() + "/" + reportTask.getName() + "." + report.getName())));
            }
        });
    });


}
 
Example #26
Source File: CpdPluginTest.java    From gradle-cpd-plugin with Apache License 2.0 5 votes vote down vote up
@Test
void CpdPlugin_shouldAddSourcesOfSubProjectsEvenIfAppliedOnlyOnParentProject(Project project, TaskProvider<Cpd> cpdCheck) {
    // When:
    Project subProject1 = ProjectBuilder.builder().withName("sub1").withParent(project).build();
    subProject1.getPlugins().apply(JavaPlugin.class);
    createProjectFiles(subProject1, "src/main/java/Clazz.java", "src/test/java/ClazzTest.java");

    Project subProject2 = ProjectBuilder.builder().withName("sub2").withParent(project).build();
    subProject2.getPlugins().apply(GroovyPlugin.class);
    createProjectFiles(subProject2, "src/main/groovy/Clazz.groovy", "src/main/resources/clazz.properties");

    subProject1.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME, sourceSet -> {
        sourceSet.getJava().srcDir(testFile(JAVA, "de/aaschmid/annotation"));
        sourceSet.getAllJava().srcDir(testFile(JAVA, "de/aaschmid/clazz"));
        sourceSet.getResources().srcDir(testFile(JAVA, "de/aaschmid/foo"));
    });
    subProject2.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME, sourceSet ->
            sourceSet.getJava().srcDir(testFile(JAVA, "de/aaschmid/test")));

    // Then:
    List<File> expected = testFilesRecurseIn(JAVA, "de/aaschmid/annotation", "de/aaschmid/clazz", "de/aaschmid/test");
    expected.add(subProject1.file("src/main/java/Clazz.java"));
    expected.add(subProject1.file("src/test/java/ClazzTest.java"));
    expected.add(subProject2.file("src/main/groovy/Clazz.groovy"));

    assertThat(cpdCheck.get().getSource()).containsExactlyInAnyOrderElementsOf(expected);

    assertThat(subProject1.getTasks().findByName("cpdCheck")).isNull();
    assertThat(subProject2.getTasks().findByName("cpdCheck")).isNull();
}
 
Example #27
Source File: GradleProject.java    From juniversal with MIT License 5 votes vote down vote up
public SourceSet getSourceSet(SourceType sourceType) {
    JavaPluginConvention javaPluginConvention;
    try {
        javaPluginConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
    } catch (IllegalStateException e) {
        throw new RuntimeException("Gradle project apparently isn't a Java project--it doesn't use the Java plugin");
    }

    SourceSetContainer sourceSets = javaPluginConvention.getSourceSets();
    return sourceSets.getByName(getSourceSetName(sourceType));
}
 
Example #28
Source File: AspectJPostCompileWeavingPlugin.java    From gradle-plugins with MIT License 5 votes vote down vote up
@Override
public void apply(Project project) {
    this.project = project;
    aspectjBasePlugin = project.getPlugins().apply(AspectJBasePlugin.class);

    project.getPlugins().apply(JavaBasePlugin.class);

    project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(this::configureSourceSet);
}
 
Example #29
Source File: Web3jPlugin.java    From web3j-gradle-plugin with Apache License 2.0 5 votes vote down vote up
public void apply(final Project target) {
    target.getPluginManager().apply(JavaPlugin.class);
    target.getPluginManager().apply(SolidityPlugin.class);
    target.getExtensions().create(Web3jExtension.NAME, Web3jExtension.class, target);
    target.getDependencies().add("implementation", "org.web3j:core:" + getProjectVersion());

    final SourceSetContainer sourceSets =
            target.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();

    target.afterEvaluate(p -> sourceSets.all(sourceSet -> configure(target, sourceSet)));
}
 
Example #30
Source File: CodeGeneratorPlugin.java    From gradle-plugins with MIT License 5 votes vote down vote up
@Override
public void apply(Project project) {
    CodeGeneratorConfiguration codeGenerator = project.getExtensions().create("codeGenerator", CodeGeneratorConfiguration.class, project.getObjects());
    Configuration codeGeneratorConfiguration = project.getConfigurations().create("codeGenerator");

    JavaPluginConvention plugin = project.getConvention().getPlugin(JavaPluginConvention.class);
    for (SourceSet sourceSet : plugin.getSourceSets()) {
        String outputDir = project.getBuildDir() + "/generated-src/generator/" + sourceSet.getName();
        File outputDirFile = new File(outputDir);
        project.getLogger().debug("Using output dir {}", outputDir);

        File inputDir = new File(project.getProjectDir() + "/src/code-generator/" + sourceSet.getName());
        sourceSet.getJava().srcDir(inputDir);
        sourceSet.getJava().srcDir(outputDirFile);

        project.getLogger().debug("Using input dir {}", inputDir);

        String taskName = sourceSet.getTaskName("generate", "Code");

        TaskProvider<GenerateCodeTask> generate = project.getTasks().register(taskName, GenerateCodeTask.class, s -> {
            s.setGroup("generate");
            s.getOutputDir().set(outputDirFile);
            if(inputDir.isDirectory()) {
                s.getInputDir().set(inputDir);
            }
            s.getSourceSet().set(sourceSet.getName());
            s.getCodeGeneratorClasspath().from(codeGeneratorConfiguration);
            s.getConfigurationValues().set(codeGenerator.getConfigurationValues());
            s.dependsOn(codeGeneratorConfiguration);
        });
        project.getTasks().named(sourceSet.getCompileJavaTaskName(), t -> t.dependsOn(generate));
    }
}