Search in sources :

Example 11 with JavaPlugin

use of org.gradle.api.plugins.JavaPlugin in project gradle-consistent-versions by palantir.

the class FixLegacyJavaConfigurationsPlugin method apply.

@Override
public final void apply(Project project) {
    // ConsistentVersionsPlugin should ensure that we only get applied onto java projects
    if (!project.getPlugins().hasPlugin(JavaPlugin.class)) {
        throw new GradleException("FixLegacyJavaConfigurationsPlugin must be applied after 'java' / JavaPlugin");
    }
    if (VersionsLockPlugin.isIgnoreLockFile(project)) {
        return;
    }
    Configuration unifiedClasspath = project.getRootProject().getConfigurations().findByName(VersionsLockPlugin.UNIFIED_CLASSPATH_CONFIGURATION_NAME);
    Preconditions.checkNotNull(unifiedClasspath, "FixLegacyJavaConfigurationsPlugin must be applied after VersionsLockPlugin");
    fixLegacyResolvableJavaConfigurations(project, unifiedClasspath);
}
Also used : Configuration(org.gradle.api.artifacts.Configuration) GradleException(org.gradle.api.GradleException) JavaPlugin(org.gradle.api.plugins.JavaPlugin)

Example 12 with JavaPlugin

use of org.gradle.api.plugins.JavaPlugin in project spring-boot by spring-projects.

the class MavenRepositoryPlugin method setUpProjectRepository.

private void setUpProjectRepository(Project project, Task publishTask, File repositoryLocation) {
    publishTask.doFirst(new CleanAction(repositoryLocation));
    Configuration projectRepository = project.getConfigurations().create(MAVEN_REPOSITORY_CONFIGURATION_NAME);
    project.getArtifacts().add(projectRepository.getName(), repositoryLocation, (artifact) -> artifact.builtBy(publishTask));
    DependencySet target = projectRepository.getDependencies();
    project.getPlugins().withType(JavaPlugin.class).all((javaPlugin) -> addMavenRepositoryDependencies(project, JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME, target));
    project.getPlugins().withType(JavaLibraryPlugin.class).all((javaLibraryPlugin) -> addMavenRepositoryDependencies(project, JavaPlugin.API_CONFIGURATION_NAME, target));
    project.getPlugins().withType(JavaPlatformPlugin.class).all((javaPlugin) -> addMavenRepositoryDependencies(project, JavaPlatformPlugin.API_CONFIGURATION_NAME, target));
}
Also used : Configuration(org.gradle.api.artifacts.Configuration) DependencySet(org.gradle.api.artifacts.DependencySet) JavaPlugin(org.gradle.api.plugins.JavaPlugin) JavaLibraryPlugin(org.gradle.api.plugins.JavaLibraryPlugin) JavaPlatformPlugin(org.gradle.api.plugins.JavaPlatformPlugin)

Example 13 with JavaPlugin

use of org.gradle.api.plugins.JavaPlugin in project cayenne by apache.

the class CgenTask method getDestDirFile.

@OutputDirectory
protected File getDestDirFile() {
    final Reference<File> javaSourceDir = new Reference<>(null);
    if (destDir != null) {
        javaSourceDir.set(destDir);
    } else if (destDirName != null) {
        javaSourceDir.set(getProject().file(destDirName));
    } else {
        getProject().getPlugins().withType(JavaPlugin.class, new Action<JavaPlugin>() {

            @Override
            public void execute(final JavaPlugin plugin) {
                SourceSetContainer sourceSets = (SourceSetContainer) getProject().getProperties().get("sourceSets");
                Set<File> sourceDirs = sourceSets.getByName("main").getJava().getSrcDirs();
                if (sourceDirs != null && !sourceDirs.isEmpty()) {
                    // find java directory, if there is no such dir, take first
                    for (File dir : sourceDirs) {
                        if (dir.getName().endsWith("java")) {
                            javaSourceDir.set(dir);
                            break;
                        }
                    }
                    if (javaSourceDir.get() == null) {
                        javaSourceDir.set(sourceDirs.iterator().next());
                    }
                }
            }
        });
    }
    if (javaSourceDir.get() == null) {
        throw new InvalidUserDataException("cgen.destDir is not set and there is no Java source sets found.");
    }
    if (!javaSourceDir.get().exists()) {
        javaSourceDir.get().mkdirs();
    }
    return javaSourceDir.get();
}
Also used : Action(org.gradle.api.Action) ClientClassGenerationAction(org.apache.cayenne.gen.ClientClassGenerationAction) ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction) Reference(groovy.lang.Reference) InvalidUserDataException(org.gradle.api.InvalidUserDataException) JavaPlugin(org.gradle.api.plugins.JavaPlugin) File(java.io.File)

Example 14 with JavaPlugin

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

the class EclipseWtpPlugin method configureEclipseWtpComponent.

private void configureEclipseWtpComponent(final Project project, final EclipseModel model) {
    XmlTransformer xmlTransformer = new XmlTransformer();
    xmlTransformer.setIndentation("\t");
    final EclipseWtpComponent component = project.getObjects().newInstance(EclipseWtpComponent.class, project, new XmlFileContentMerger(xmlTransformer));
    model.getWtp().setComponent(component);
    TaskProvider<GenerateEclipseWtpComponent> task = project.getTasks().register(ECLIPSE_WTP_COMPONENT_TASK_NAME, GenerateEclipseWtpComponent.class, component);
    task.configure(new Action<GenerateEclipseWtpComponent>() {

        @Override
        public void execute(final GenerateEclipseWtpComponent task) {
            task.setDescription("Generates the Eclipse WTP component settings file.");
            task.setInputFile(project.file(".settings/org.eclipse.wst.common.component"));
            task.setOutputFile(project.file(".settings/org.eclipse.wst.common.component"));
        }
    });
    addWorker(task, ECLIPSE_WTP_COMPONENT_TASK_NAME);
    ((IConventionAware) component).getConventionMapping().map("deployName", new Callable<String>() {

        @Override
        public String call() throws Exception {
            return model.getProject().getName();
        }
    });
    project.getPlugins().withType(JavaPlugin.class, new Action<JavaPlugin>() {

        @Override
        public void execute(JavaPlugin javaPlugin) {
            if (hasWarOrEarPlugin(project)) {
                return;
            }
            Set<Configuration> libConfigurations = component.getLibConfigurations();
            libConfigurations.add(project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME));
            component.setClassesDeployPath("/");
            ((IConventionAware) component).getConventionMapping().map("libDeployPath", new Callable<String>() {

                @Override
                public String call() throws Exception {
                    return "../";
                }
            });
            ((IConventionAware) component).getConventionMapping().map("sourceDirs", new Callable<Set<File>>() {

                @Override
                public Set<File> call() throws Exception {
                    return getMainSourceDirs(project);
                }
            });
        }
    });
    project.getPlugins().withType(WarPlugin.class, new Action<WarPlugin>() {

        @Override
        public void execute(WarPlugin warPlugin) {
            Set<Configuration> libConfigurations = component.getLibConfigurations();
            Set<Configuration> minusConfigurations = component.getMinusConfigurations();
            libConfigurations.add(project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME));
            minusConfigurations.add(project.getConfigurations().getByName("providedRuntime"));
            component.setClassesDeployPath("/WEB-INF/classes");
            ConventionMapping convention = ((IConventionAware) component).getConventionMapping();
            convention.map("libDeployPath", new Callable<String>() {

                @Override
                public String call() throws Exception {
                    return "/WEB-INF/lib";
                }
            });
            convention.map("contextPath", new Callable<String>() {

                @Override
                public String call() throws Exception {
                    return ((War) project.getTasks().getByName("war")).getArchiveBaseName().getOrNull();
                }
            });
            convention.map("resources", new Callable<List<WbResource>>() {

                @Override
                public List<WbResource> call() throws Exception {
                    File projectDir = project.getProjectDir();
                    File webAppDir = ((War) project.getTasks().getByName("war")).getWebAppDirectory().get().getAsFile();
                    String webAppDirName = RelativePathUtil.relativePath(projectDir, webAppDir);
                    return Lists.newArrayList(new WbResource("/", webAppDirName));
                }
            });
            convention.map("sourceDirs", new Callable<Set<File>>() {

                @Override
                public Set<File> call() throws Exception {
                    return getMainSourceDirs(project);
                }
            });
        }
    });
    project.getPlugins().withType(EarPlugin.class, new Action<EarPlugin>() {

        @Override
        public void execute(EarPlugin earPlugin) {
            Set<Configuration> libConfigurations = component.getLibConfigurations();
            Set<Configuration> rootConfigurations = component.getRootConfigurations();
            rootConfigurations.clear();
            rootConfigurations.add(project.getConfigurations().getByName("deploy"));
            libConfigurations.clear();
            libConfigurations.add(project.getConfigurations().getByName("earlib"));
            component.setClassesDeployPath("/");
            final ConventionMapping convention = ((IConventionAware) component).getConventionMapping();
            convention.map("libDeployPath", new Callable<String>() {

                @Override
                public String call() throws Exception {
                    String deployPath = ((Ear) project.getTasks().findByName(EarPlugin.EAR_TASK_NAME)).getLibDirName();
                    if (!deployPath.startsWith("/")) {
                        deployPath = "/" + deployPath;
                    }
                    return deployPath;
                }
            });
            convention.map("sourceDirs", new Callable<Set<File>>() {

                @Override
                public Set<File> call() throws Exception {
                    return WrapUtil.toSet(((Ear) project.getTasks().findByName(EarPlugin.EAR_TASK_NAME)).getAppDirectory().get().getAsFile());
                }
            });
            project.getPlugins().withType(JavaPlugin.class, new Action<JavaPlugin>() {

                @Override
                public void execute(JavaPlugin javaPlugin) {
                    convention.map("sourceDirs", new Callable<Set<File>>() {

                        @Override
                        public Set<File> call() throws Exception {
                            return getMainSourceDirs(project);
                        }
                    });
                }
            });
        }
    });
}
Also used : Action(org.gradle.api.Action) Set(java.util.Set) JavaPlugin(org.gradle.api.plugins.JavaPlugin) War(org.gradle.api.tasks.bundling.War) Callable(java.util.concurrent.Callable) XmlTransformer(org.gradle.internal.xml.XmlTransformer) EclipseWtpComponent(org.gradle.plugins.ide.eclipse.model.EclipseWtpComponent) WarPlugin(org.gradle.api.plugins.WarPlugin) XmlFileContentMerger(org.gradle.plugins.ide.api.XmlFileContentMerger) ConventionMapping(org.gradle.api.internal.ConventionMapping) WbResource(org.gradle.plugins.ide.eclipse.model.WbResource) Ear(org.gradle.plugins.ear.Ear) IConventionAware(org.gradle.api.internal.IConventionAware) File(java.io.File) EarPlugin(org.gradle.plugins.ear.EarPlugin)

Example 15 with JavaPlugin

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

the class EclipsePlugin method configureJavaClasspath.

private static void configureJavaClasspath(final Project project, final TaskProvider<GenerateEclipseClasspath> task, final EclipseModel model, Collection<SourceSet> testSourceSetsConvention, Collection<Configuration> testConfigurationsConvention) {
    project.getPlugins().withType(JavaPlugin.class, new Action<JavaPlugin>() {

        @Override
        public void execute(JavaPlugin javaPlugin) {
            ((IConventionAware) model.getClasspath()).getConventionMapping().map("plusConfigurations", new Callable<Collection<Configuration>>() {

                @Override
                public Collection<Configuration> call() {
                    SourceSetContainer sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
                    List<Configuration> sourceSetsConfigurations = Lists.newArrayListWithCapacity(sourceSets.size() * 2);
                    ConfigurationContainer configurations = project.getConfigurations();
                    for (SourceSet sourceSet : sourceSets) {
                        sourceSetsConfigurations.add(configurations.getByName(sourceSet.getCompileClasspathConfigurationName()));
                        sourceSetsConfigurations.add(configurations.getByName(sourceSet.getRuntimeClasspathConfigurationName()));
                    }
                    return sourceSetsConfigurations;
                }
            }).cache();
            ((IConventionAware) model.getClasspath()).getConventionMapping().map("classFolders", new Callable<List<File>>() {

                @Override
                public List<File> call() {
                    List<File> result = Lists.newArrayList();
                    for (SourceSet sourceSet : project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets()) {
                        result.addAll(sourceSet.getOutput().getDirs().getFiles());
                    }
                    return result;
                }
            });
            task.configure(new Action<GenerateEclipseClasspath>() {

                @Override
                public void execute(GenerateEclipseClasspath task) {
                    for (SourceSet sourceSet : project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets()) {
                        task.dependsOn(sourceSet.getOutput().getDirs());
                    }
                }
            });
            SourceSetContainer sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
            sourceSets.configureEach(new Action<SourceSet>() {

                @Override
                public void execute(SourceSet sourceSet) {
                    if (sourceSet.getName().toLowerCase(Locale.ROOT).contains("test")) {
                        // source sets with 'test' in their name are marked as test on the Eclipse classpath
                        testSourceSetsConvention.add(sourceSet);
                        // resolved dependencies from the source sets with 'test' in their name are marked as test on the Eclipse classpath
                        testConfigurationsConvention.add(project.getConfigurations().findByName(sourceSet.getCompileClasspathConfigurationName()));
                        testConfigurationsConvention.add(project.getConfigurations().findByName(sourceSet.getRuntimeClasspathConfigurationName()));
                    }
                }
            });
            project.getConfigurations().all(new Action<Configuration>() {

                @Override
                public void execute(Configuration configuration) {
                    if (configuration.isCanBeResolved() && configuration.getName().toLowerCase(Locale.ROOT).contains("test")) {
                        // resolved dependencies from custom configurations with 'test' in their name are marked as test on the Eclipse classpath
                        testConfigurationsConvention.add(configuration);
                    }
                }
            });
        }
    });
    project.getPlugins().withType(JavaTestFixturesPlugin.class, new Action<JavaTestFixturesPlugin>() {

        @Override
        public void execute(JavaTestFixturesPlugin javaTestFixturesPlugin) {
            model.getClasspath().getContainsTestFixtures().convention(true);
            project.getPluginManager().withPlugin("java", new Action<AppliedPlugin>() {

                @Override
                public void execute(AppliedPlugin appliedPlugin) {
                    SourceSetContainer sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
                    SourceSet sourceSet = sourceSets.getByName(TestFixturesSupport.TEST_FIXTURE_SOURCESET_NAME);
                    // the testFixtures source set is marked as test on the Eclipse classpath
                    testSourceSetsConvention.add(sourceSet);
                    // resolved dependencies from the testFixtures source set are marked as test on the Eclipse classpath
                    testConfigurationsConvention.add(project.getConfigurations().findByName(sourceSet.getCompileClasspathConfigurationName()));
                    testConfigurationsConvention.add(project.getConfigurations().findByName(sourceSet.getRuntimeClasspathConfigurationName()));
                }
            });
        }
    });
    project.getPlugins().withType(TestSuiteBasePlugin.class, testSuiteBasePlugin -> {
        TestingExtension testing = project.getExtensions().getByType(TestingExtension.class);
        ExtensiblePolymorphicDomainObjectContainer<TestSuite> suites = testing.getSuites();
        suites.withType(JvmTestSuite.class).configureEach(jvmTestSuite -> {
            // jvm test suite source sets are marked as test on the Eclipse classpath
            testSourceSetsConvention.add(jvmTestSuite.getSources());
            // resolved dependencies from jvm test suites are marked as test on the Eclipse classpath
            testConfigurationsConvention.add(project.getConfigurations().findByName(jvmTestSuite.getSources().getCompileClasspathConfigurationName()));
            testConfigurationsConvention.add(project.getConfigurations().findByName(jvmTestSuite.getSources().getRuntimeClasspathConfigurationName()));
        });
    });
}
Also used : Action(org.gradle.api.Action) Configuration(org.gradle.api.artifacts.Configuration) TestingExtension(org.gradle.testing.base.TestingExtension) JavaPlugin(org.gradle.api.plugins.JavaPlugin) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) JavaTestFixturesPlugin(org.gradle.api.plugins.JavaTestFixturesPlugin) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) Callable(java.util.concurrent.Callable) SourceSet(org.gradle.api.tasks.SourceSet) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) TestSuite(org.gradle.testing.base.TestSuite) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) Collection(java.util.Collection) List(java.util.List) AppliedPlugin(org.gradle.api.plugins.AppliedPlugin) IConventionAware(org.gradle.api.internal.IConventionAware) File(java.io.File) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite)

Aggregations

JavaPlugin (org.gradle.api.plugins.JavaPlugin)21 Configuration (org.gradle.api.artifacts.Configuration)9 Project (org.gradle.api.Project)8 JavaPluginExtension (org.gradle.api.plugins.JavaPluginExtension)8 Plugin (org.gradle.api.Plugin)7 PublishingExtension (org.gradle.api.publish.PublishingExtension)7 MavenPublication (org.gradle.api.publish.maven.MavenPublication)7 PluginContainer (org.gradle.api.plugins.PluginContainer)6 MavenPublishPlugin (org.gradle.api.publish.maven.plugins.MavenPublishPlugin)6 File (java.io.File)5 Callable (java.util.concurrent.Callable)4 Action (org.gradle.api.Action)4 ConfigurationContainer (org.gradle.api.artifacts.ConfigurationContainer)4 JavaTestFixturesPlugin (org.gradle.api.plugins.JavaTestFixturesPlugin)4 SourceSet (org.gradle.api.tasks.SourceSet)4 PropDepsPlugin (org.springframework.gradle.propdeps.PropDepsPlugin)3 SpotlessExtension (com.diffplug.gradle.spotless.SpotlessExtension)2 SpotlessPlugin (com.diffplug.gradle.spotless.SpotlessPlugin)2 VersionsPlugin (com.github.benmanes.gradle.versions.VersionsPlugin)2 ImmutableList (com.google.common.collect.ImmutableList)2