Search in sources :

Example 16 with Convention

use of org.gradle.api.plugins.Convention in project gradle by gradle.

the class ExtensibleDynamicObjectTest method canGetAndSetPropertyDefinedByConventionObject.

@Test
public void canGetAndSetPropertyDefinedByConventionObject() {
    Bean bean = new Bean();
    Convention convention = bean.extensibleDynamicObject.getConvention();
    ConventionBean conventionBean = new ConventionBean();
    convention.getPlugins().put("test", conventionBean);
    conventionBean.setConventionProperty("value");
    assertThat(bean.getProperty("conventionProperty"), equalTo((Object) "value"));
    bean.setProperty("conventionProperty", "new value");
    assertThat(bean.getProperty("conventionProperty"), equalTo((Object) "new value"));
    assertThat(conventionBean.getConventionProperty(), equalTo((Object) "new value"));
}
Also used : Convention(org.gradle.api.plugins.Convention) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Test(org.junit.Test)

Example 17 with Convention

use of org.gradle.api.plugins.Convention in project gradle by gradle.

the class ScalaBasePlugin method configureScalaCompile.

private static void configureScalaCompile(final Project project, JavaBasePlugin javaPlugin, final SourceSet sourceSet) {
    String taskName = sourceSet.getCompileTaskName("scala");
    final ScalaCompile scalaCompile = project.getTasks().create(taskName, ScalaCompile.class);
    scalaCompile.dependsOn(sourceSet.getCompileJavaTaskName());
    javaPlugin.configureForSourceSet(sourceSet, scalaCompile);
    Convention scalaConvention = (Convention) InvokerHelper.getProperty(sourceSet, "convention");
    ScalaSourceSet scalaSourceSet = scalaConvention.findPlugin(ScalaSourceSet.class);
    scalaCompile.setDescription("Compiles the " + scalaSourceSet.getScala() + ".");
    scalaCompile.setSource(scalaSourceSet.getScala());
    project.getTasks().getByName(sourceSet.getClassesTaskName()).dependsOn(taskName);
    // cannot use convention mapping because the resulting object won't be serializable
    // cannot compute at task execution time because we need association with source set
    project.getGradle().addBuildListener(new BuildAdapter() {

        @Override
        public void projectsEvaluated(Gradle gradle) {
            IncrementalCompileOptions incrementalOptions = scalaCompile.getScalaCompileOptions().getIncrementalOptions();
            if (incrementalOptions.getAnalysisFile() == null) {
                String analysisFilePath = project.getBuildDir().getPath() + "/tmp/scala/compilerAnalysis/" + scalaCompile.getName() + ".analysis";
                incrementalOptions.setAnalysisFile(new File(analysisFilePath));
            }
            if (incrementalOptions.getPublishedCode() == null) {
                Jar jarTask = (Jar) project.getTasks().findByName(sourceSet.getJarTaskName());
                incrementalOptions.setPublishedCode(jarTask == null ? null : jarTask.getArchivePath());
            }
        }
    });
}
Also used : IncrementalCompileOptions(org.gradle.api.tasks.scala.IncrementalCompileOptions) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) Convention(org.gradle.api.plugins.Convention) ScalaCompile(org.gradle.api.tasks.scala.ScalaCompile) ScalaSourceSet(org.gradle.api.tasks.ScalaSourceSet) DefaultScalaSourceSet(org.gradle.api.internal.tasks.DefaultScalaSourceSet) BuildAdapter(org.gradle.BuildAdapter) Jar(org.gradle.jvm.tasks.Jar) Gradle(org.gradle.api.invocation.Gradle) File(java.io.File)

Example 18 with Convention

use of org.gradle.api.plugins.Convention in project gradle by gradle.

the class ScalaBasePlugin method configureSourceSetDefaults.

private static void configureSourceSetDefaults(final Project project, final SourceDirectorySetFactory sourceDirectorySetFactory) {
    final JavaBasePlugin javaPlugin = project.getPlugins().getPlugin(JavaBasePlugin.class);
    project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(new Action<SourceSet>() {

        @Override
        public void execute(final SourceSet sourceSet) {
            String displayName = (String) InvokerHelper.invokeMethod(sourceSet, "getDisplayName", null);
            Convention sourceSetConvention = (Convention) InvokerHelper.getProperty(sourceSet, "convention");
            DefaultScalaSourceSet scalaSourceSet = new DefaultScalaSourceSet(displayName, sourceDirectorySetFactory);
            sourceSetConvention.getPlugins().put("scala", scalaSourceSet);
            final SourceDirectorySet scalaDirectorySet = scalaSourceSet.getScala();
            scalaDirectorySet.srcDir(new Callable<File>() {

                @Override
                public File call() throws Exception {
                    return project.file("src/" + sourceSet.getName() + "/scala");
                }
            });
            sourceSet.getAllJava().source(scalaDirectorySet);
            sourceSet.getAllSource().source(scalaDirectorySet);
            sourceSet.getResources().getFilter().exclude(new Spec<FileTreeElement>() {

                @Override
                public boolean isSatisfiedBy(FileTreeElement element) {
                    return scalaDirectorySet.contains(element.getFile());
                }
            });
            configureScalaCompile(project, javaPlugin, sourceSet);
        }
    });
}
Also used : SourceSet(org.gradle.api.tasks.SourceSet) ScalaSourceSet(org.gradle.api.tasks.ScalaSourceSet) DefaultScalaSourceSet(org.gradle.api.internal.tasks.DefaultScalaSourceSet) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) Convention(org.gradle.api.plugins.Convention) FileTreeElement(org.gradle.api.file.FileTreeElement) JavaBasePlugin(org.gradle.api.plugins.JavaBasePlugin) DefaultScalaSourceSet(org.gradle.api.internal.tasks.DefaultScalaSourceSet) SourceDirectorySet(org.gradle.api.file.SourceDirectorySet) Spec(org.gradle.api.specs.Spec) Callable(java.util.concurrent.Callable)

Aggregations

Convention (org.gradle.api.plugins.Convention)18 Test (org.junit.Test)14 BeanDynamicObject (org.gradle.internal.metaobject.BeanDynamicObject)10 DynamicObject (org.gradle.internal.metaobject.DynamicObject)10 JavaPluginConvention (org.gradle.api.plugins.JavaPluginConvention)4 DefaultScalaSourceSet (org.gradle.api.internal.tasks.DefaultScalaSourceSet)3 ScalaSourceSet (org.gradle.api.tasks.ScalaSourceSet)3 File (java.io.File)2 BuildAdapter (org.gradle.BuildAdapter)2 Gradle (org.gradle.api.invocation.Gradle)2 IncrementalCompileOptions (org.gradle.api.tasks.scala.IncrementalCompileOptions)2 ScalaCompile (org.gradle.api.tasks.scala.ScalaCompile)2 Jar (org.gradle.jvm.tasks.Jar)2 Callable (java.util.concurrent.Callable)1 FileTreeElement (org.gradle.api.file.FileTreeElement)1 SourceDirectorySet (org.gradle.api.file.SourceDirectorySet)1 JavaBasePlugin (org.gradle.api.plugins.JavaBasePlugin)1 WarPluginConvention (org.gradle.api.plugins.WarPluginConvention)1 Spec (org.gradle.api.specs.Spec)1 SourceSet (org.gradle.api.tasks.SourceSet)1