private void configureExtensionRule() { final ConventionMapping extensionMapping = conventionMappingOf(extension); extensionMapping.map("sourceSets", Callables.returning(new ArrayList())); extensionMapping.map("reportsDir", new Callable<File>() { @Override public File call() { return project.getExtensions().getByType(ReportingExtension.class).file(getReportName()); } }); withBasePlugin(new Action<Plugin>() { @Override public void execute(Plugin plugin) { extensionMapping.map("sourceSets", new Callable<SourceSetContainer>() { @Override public SourceSetContainer call() { return getJavaPluginConvention().getSourceSets(); } }); } }); }
private void configureCompileTestJavaTask(final Project project) { final JavaCompile compileTestJava = (JavaCompile) project.getTasks() .findByName(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME); final SourceSet test = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("test"); final JavaModule module = (JavaModule) project.getExtensions().getByName(EXTENSION_NAME); compileTestJava.getInputs().property("moduleName", module.geName()); compileTestJava.doFirst(new Action<Task>() { @Override public void execute(Task task) { List<String> args = new ArrayList<>(); args.add("--module-path"); args.add(compileTestJava.getClasspath().getAsPath()); args.add("--add-modules"); args.add("junit"); args.add("--add-reads"); args.add(module.geName() + "=junit"); args.add("--patch-module"); args.add(module.geName() + "=" + test.getJava().getSourceDirectories().getAsPath()); compileTestJava.getOptions().setCompilerArgs(args); compileTestJava.setClasspath(project.files()); } }); }
private void configureTestTask(final Project project) { final Test testTask = (Test) project.getTasks().findByName(JavaPlugin.TEST_TASK_NAME); final SourceSet test = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("test"); final JavaModule module = (JavaModule) project.getExtensions().getByName(EXTENSION_NAME); testTask.getInputs().property("moduleName", module.geName()); testTask.doFirst(new Action<Task>() { @Override public void execute(Task task) { List<String> args = new ArrayList<>(); args.add("--module-path"); args.add(testTask.getClasspath().getAsPath()); args.add("--add-modules"); args.add("ALL-MODULE-PATH"); args.add("--add-reads"); args.add(module.geName() + "=junit"); args.add("--patch-module"); args.add(module.geName() + "=" + test.getJava().getOutputDir()); testTask.setJvmArgs(args); testTask.setClasspath(project.files()); } }); }
@Override public void apply(Project project) { ParsecPluginExtension pluginExtension = project.getExtensions().create("parsec", ParsecPluginExtension.class); PathUtils pathUtils = new PathUtils(project, pluginExtension); TaskContainer tasks = project.getTasks(); // Create tasks (when applied as a plugin) ParsecInitTask initTask = tasks.create("parsec-init", ParsecInitTask.class); ParsecGenerateTask generateTask = tasks.create("parsec-generate", ParsecGenerateTask.class); // Make generate trigger init. generateTask.dependsOn(initTask); project.getPlugins().withType(JavaPlugin.class, plugin -> { SourceSet sourceSet = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("main"); // Add ${buildDir}/generated-sources/java to sources sourceSet.getJava().srcDir(pathUtils.getGeneratedSourcesPath()); // Add ${buildDir}/generated-resources/parsec to resources sourceSet.getResources().srcDir(pathUtils.getGeneratedResourcesPath()); // Make compileJava trigger generate tasks.getByName(JavaPlugin.COMPILE_JAVA_TASK_NAME).dependsOn(generateTask); }); }
@Override public void apply(final Project project) { Logger logger = project.getLogger(); logger.info("applying jsweet plugin"); if (!project.getPlugins().hasPlugin(JavaPlugin.class) && !project.getPlugins().hasPlugin(WarPlugin.class)) { logger.error("No java or war plugin detected. Enable java or war plugin."); throw new IllegalStateException("No java or war plugin detected. Enable java or war plugin."); } JSweetPluginExtension extension = project.getExtensions().create("jsweet", JSweetPluginExtension.class); JavaPluginConvention javaPluginConvention = project.getConvention().getPlugin(JavaPluginConvention.class); SourceSetContainer sourceSets = javaPluginConvention.getSourceSets(); SourceSet mainSources = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); JSweetTranspileTask task = project.getTasks().create("jsweet", JSweetTranspileTask.class); task.setGroup("generate"); task.dependsOn(JavaPlugin.COMPILE_JAVA_TASK_NAME); task.setConfiguration(extension); task.setSources(mainSources.getAllJava()); task.setClasspath(mainSources.getCompileClasspath()); JSweetCleanTask cleanTask = project.getTasks().create("jsweetClean", JSweetCleanTask.class); cleanTask.setConfiguration(extension); }
public void establishSonarQubeSourceSet() { final JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class); final SourceSetContainer sourceSets = javaConvention.getSourceSets(); final ConfigurationContainer configs = project.getConfigurations(); final SourceSet testSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME); final SourceSet sqSourceSet = sourceSets.create(BuildUtil.SONARQUBE_SOURCE_SET_NAME); configs.getByName(testSourceSet.getImplementationConfigurationName()).extendsFrom( configs.getByName(sqSourceSet.getImplementationConfigurationName())); configs.getByName(testSourceSet.getRuntimeOnlyConfigurationName()).extendsFrom( configs.getByName(sqSourceSet.getRuntimeOnlyConfigurationName())); final TaskContainer tasks = project.getTasks(); tasks.getByName(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME).dependsOn( tasks.getByName(sqSourceSet.getClassesTaskName())); final FileCollection sqOutputs = sqSourceSet.getOutput().getClassesDirs().plus( project.files(sqSourceSet.getOutput().getResourcesDir())); testSourceSet.setCompileClasspath(testSourceSet.getCompileClasspath().plus(sqOutputs)); testSourceSet.setRuntimeClasspath(testSourceSet.getRuntimeClasspath().plus(sqOutputs)); }
private void configureForSourceSets(SourceSetContainer sourceSets) { sourceSets.all(new Action<SourceSet>() { @Override public void execute(SourceSet sourceSet) { Task task = project.getTasks().create(sourceSet.getTaskName(getTaskBaseName(), null), getCastedTaskType()); configureForSourceSet(sourceSet, (T)task); } }); }
public Set<File> getProjectLibraries() { Set<File> classpath = new HashSet<>(); SourceSetContainer sourceSets = (SourceSetContainer) project.getProperties().get("sourceSets"); if (sourceSets != null) { SortedSet<String> availableSourceSetNames = sourceSets.getNames(); for (String sourceSetName : Arrays.asList("main", "test", "integrationTest")) { if (availableSourceSetNames.contains(sourceSetName)) { SourceSet sourceSet = sourceSets.getByName(sourceSetName); classpath.add(sourceSet.getOutput().getClassesDir()); } } } // add dependencies from configured gradle configuration to url (usually test or integrationTest) TSGeneratorConfig generatorConfiguration = project.getExtensions().getByType(TSGeneratorConfig.class); String configurationName = generatorConfiguration.getRuntime().getConfiguration(); ConfigurationContainer configurations = project.getConfigurations(); Configuration runtimeConfiguration = configurations.findByName(configurationName + "Runtime"); if (runtimeConfiguration == null) { runtimeConfiguration = configurations.getByName(configurationName); } classpath.addAll(runtimeConfiguration.getFiles()); for (File file : classpath) { LOGGER.debug("classpath entry: {}", file); } return classpath; }
@Override public void apply(Project project) { GeneratePluginExtension generatePluginExtension = project.getExtensions().create(EXTENSION_NAME, GeneratePluginExtension.class, project); GenerateTask generateTask = project.getTasks().create(TASK_NAME, GenerateTask.class); generateTask.setGroup(BasePlugin.BUILD_GROUP); generateTask.setDescription("Generates DDL scripts based on JPA model."); generateTask.setExtension(generatePluginExtension); generateTask.dependsOn(JavaBasePlugin.BUILD_TASK_NAME); project.afterEvaluate(evaluatedProject -> { fillDefaults(evaluatedProject, generatePluginExtension); SourceSetContainer sourceSets = (SourceSetContainer) project.getProperties().get("sourceSets"); Set<File> paths; if (sourceSets != null) { UnionFileCollection mainClasspath = (UnionFileCollection) sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath(); paths = mainClasspath.getSources() .stream() .filter(fileCollection -> fileCollection instanceof DefaultSourceSetOutput) .map(DefaultSourceSetOutput.class::cast) .flatMap(fileCollection -> fileCollection.getClassesDirs().getFiles().stream()) .collect(Collectors.toSet()); } else { paths = new HashSet<>(); } generateTask.setOutputClassesDirs(paths); }); }
@TaskAction public void modernizer() throws Exception { final ModernizerPluginExtension extension = getExtension(); if (extension.getJavaVersion() == null || extension.getJavaVersion().trim().isEmpty()) { extension.setJavaVersion(getJavaVersion()); } final SourceSetContainer sourceSets = (SourceSetContainer) getProject().getProperties().get("sourceSets"); executeForSourceSet(extension, sourceSets.getByName("main")); if(extension.isIncludeTestClasses()) { executeForSourceSet(extension, sourceSets.getByName("test")); } }
@Override public List<String> getSrcDirs() { SourceSetContainer sourceSets = javaConvention.getSourceSets(); SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); return mainSourceSet.getAllSource().getSrcDirs().stream().map(f -> { try { return f.getCanonicalPath(); } catch (IOException e) { return ""; } }).collect(Collectors.toList()); }
@Override public List<String> getSrcDirs() { SourceSetContainer sourceSets = javaConvention.getSourceSets(); SourceSet mainSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME); return mainSourceSet.getAllSource().getSrcDirs().stream().map(f -> { try { return f.getCanonicalPath(); } catch (IOException e) { return ""; } }).collect(Collectors.toList()); }
@Override public void apply(final Project project) { project.getPlugins().apply(JavaPlugin.class); // final JNAeratorPluginExtension extension = project.getExtensions().create(JNAeratorPluginExtension.NAME, JNAeratorPluginExtension.class); // extension.setOutputDir(new File(project.getBuildDir(), "generated-sources/jnaerator")); JNAeratorTask task = project.getTasks().create("jnaerator", JNAeratorTask.class); task.setOutputDir(new File(project.getBuildDir(), "generated-sources/jnaerator")); project.getTasks().getByName("compileJava").dependsOn(task); SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets(); final SourceSet mainSourceSet = sourceSets.getByName("main"); mainSourceSet.getJava().srcDir(task.getOutputDir()); }
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)); }
public void establishGeneralCompileOnlyCfg() { final JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class); final SourceSetContainer sourceSets = javaConvention.getSourceSets(); final ConfigurationContainer configs = project.getConfigurations(); final Configuration generalCompileOnly = configs.create(GENERAL_COMPILE_ONLY_CONFIG_NAME); Arrays.asList(SourceSet.MAIN_SOURCE_SET_NAME, BuildUtil.SONARQUBE_SOURCE_SET_NAME, SourceSet.TEST_SOURCE_SET_NAME).forEach((@Nonnull final String pSourceSetName) -> { final SourceSet sourceSet = sourceSets.getByName(pSourceSetName); configs.getByName(sourceSet.getCompileOnlyConfigurationName()).extendsFrom(generalCompileOnly); }); }
/** * The source sets container. */ public SourceSetContainer getSourceSets() { return sourceSets; }
private SourceSetHelper getSourceSet(String name) { SourceSetContainer sourceSets = (SourceSetContainer) this.project.getProperties().get("sourceSets"); SourceSet sourceSet = sourceSets.getByName(name); return new SourceSetHelper(sourceSet); }
@Override public void apply(Project project) { project.getPlugins().apply(StdModulePlugin.class); final StdPluginExtension extension = project.getExtensions().create("stdplugin", StdPluginExtension.class, project); // Gradle project.getPlugins().apply(JavaGradlePluginPlugin.class); project.getRepositories().add(project.getRepositories().jcenter()); // project.getDependencies().add(JavaPlugin.COMPILE_CONFIGURATION_NAME, project.getDependencies().gradleApi()); // project.getDependencies().add(JavaPlugin.TEST_COMPILE_CONFIGURATION_NAME, project.getDependencies().gradleTestKit()); // Maven project.getPlugins().apply(MavenPublishPlugin.class); // Bintray /* project.getPlugins().apply(BintrayPlugin.class); BintrayExtension bintray = project.getExtensions().getByType(BintrayExtension.class); String bintrayUsername = StdProjectPlugin.getExtraPropertyOrNull(project.getRootProject(), "bintrayUsername"); if (bintrayUsername != null) bintray.setUser(bintrayUsername); String bintrayApiKey = StdProjectPlugin.getExtraPropertyOrNull(project.getRootProject(), "bintrayApiKey"); if (bintrayApiKey != null) bintray.setKey(bintrayApiKey); bintray.getPkg().setRepo("gradle-plugins"); */ // Plugin final File pluginDescriptorDir = new File(project.getBuildDir(), "generated-resources/gradle-plugin"); Task generatePluginDescriptors = project.getTasks().create("generatePluginDescriptors", GeneratePluginDescriptors.class, new Action<GeneratePluginDescriptors>() { @Override public void execute(GeneratePluginDescriptors t) { // t.setGroup("Build"); t.setDescription("Generates gradle plugin descriptors."); t.setDestinationDir(pluginDescriptorDir); t.conventionMapping("pluginImplementations", new Callable<Map<String, String>>() { @Override public Map<String, String> call() throws Exception { return extension.implementations; } }); } }); SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets(); sourceSets.getByName("main").getOutput().dir(Collections.<String, Object>singletonMap("builtBy", generatePluginDescriptors), pluginDescriptorDir); }
/** * Constructor. */ public SiteTask() { super(); final Project project = getProject(); buildUtil = new BuildUtil(project); setDescription(buildUtil.getLongName() + ": Package documentation for publication on the website"); final TaskContainer tasks = project.getTasks(); for (final String predecTaskName : new String[]{// "processResources", "siteCopyAllChecks", "siteCopyJavadoc", "siteCopyDownloadGuide"}) { dependsOn(tasks.getByName(predecTaskName)); } // Task Inputs: The markdown description files of our checks final SourceSetContainer sourceSets = (SourceSetContainer) project.getProperties().get("sourceSets"); final SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); final FileTree tree = project.fileTree(mainSourceSet.getResources().getSrcDirs().iterator().next(), new Closure<Void>(this) { @Override public Void call(final Object... pArgs) { PatternFilterable ft = (PatternFilterable) getDelegate(); ft.include("**/*.md"); return null; } }); getInputs().files(tree.getFiles()); // Task Outputs: the contents of the 'site' directory final File siteDir = new File(project.getBuildDir(), "site"); getOutputs().dir(siteDir); // perform the collection of files in execution phase doLast(new Closure<Void>(this) { @Override public Void call() { try { collect(tree, siteDir); } catch (IOException e) { throw new GradleException("error executing 'site' task", e); } return null; } }); }
private static SourceSetContainer getSourceSets(Project project) { return project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets(); }