Search in sources :

Example 1 with XmlTransformer

use of org.gradle.internal.xml.XmlTransformer in project gradle by gradle.

the class IdeaPlugin method configureIdeaModule.

private void configureIdeaModule(final ProjectInternal project) {
    IdeaModuleIml iml = new IdeaModuleIml(new XmlTransformer(), project.getProjectDir());
    final IdeaModule module = instantiator.newInstance(IdeaModule.class, project, iml);
    final TaskProvider<GenerateIdeaModule> task = project.getTasks().register(IDEA_MODULE_TASK_NAME, GenerateIdeaModule.class, module);
    task.configure(new Action<GenerateIdeaModule>() {

        @Override
        public void execute(GenerateIdeaModule task) {
            task.setDescription("Generates IDEA module files (IML)");
        }
    });
    ideaModel.setModule(module);
    final String defaultModuleName = uniqueProjectNameProvider.getUniqueName(project);
    module.setName(defaultModuleName);
    ConventionMapping conventionMapping = ((IConventionAware) module).getConventionMapping();
    Set<File> sourceDirs = Sets.newLinkedHashSet();
    conventionMapping.map("sourceDirs", new Callable<Set<File>>() {

        @Override
        public Set<File> call() {
            return sourceDirs;
        }
    });
    conventionMapping.map("contentRoot", new Callable<File>() {

        @Override
        public File call() {
            return project.getProjectDir();
        }
    });
    Set<File> testSourceDirs = Sets.newLinkedHashSet();
    conventionMapping.map("testSourceDirs", new Callable<Set<File>>() {

        @Override
        public Set<File> call() {
            return testSourceDirs;
        }
    });
    Set<File> resourceDirs = Sets.newLinkedHashSet();
    conventionMapping.map("resourceDirs", new Callable<Set<File>>() {

        @Override
        public Set<File> call() throws Exception {
            return resourceDirs;
        }
    });
    Set<File> testResourceDirs = Sets.newLinkedHashSet();
    conventionMapping.map("testResourceDirs", new Callable<Set<File>>() {

        @Override
        public Set<File> call() throws Exception {
            return testResourceDirs;
        }
    });
    Set<File> excludeDirs = Sets.newLinkedHashSet();
    conventionMapping.map("excludeDirs", new Callable<Set<File>>() {

        @Override
        public Set<File> call() {
            excludeDirs.add(project.file(".gradle"));
            excludeDirs.add(project.getBuildDir());
            return excludeDirs;
        }
    });
    conventionMapping.map("pathFactory", new Callable<PathFactory>() {

        @Override
        public PathFactory call() {
            final PathFactory factory = new PathFactory();
            factory.addPathVariable("MODULE_DIR", task.get().getOutputFile().getParentFile());
            for (Map.Entry<String, File> entry : module.getPathVariables().entrySet()) {
                factory.addPathVariable(entry.getKey(), entry.getValue());
            }
            return factory;
        }
    });
    artifactRegistry.registerIdeProject(new IdeaModuleMetadata(module, task));
    addWorker(task, IDEA_MODULE_TASK_NAME);
}
Also used : Set(java.util.Set) IdeaModuleMetadata(org.gradle.plugins.ide.idea.internal.IdeaModuleMetadata) PathFactory(org.gradle.plugins.ide.idea.model.PathFactory) ConventionMapping(org.gradle.api.internal.ConventionMapping) XmlTransformer(org.gradle.internal.xml.XmlTransformer) IdeaModule(org.gradle.plugins.ide.idea.model.IdeaModule) IdeaModuleIml(org.gradle.plugins.ide.idea.model.IdeaModuleIml) IConventionAware(org.gradle.api.internal.IConventionAware) File(java.io.File)

Example 2 with XmlTransformer

use of org.gradle.internal.xml.XmlTransformer 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 3 with XmlTransformer

use of org.gradle.internal.xml.XmlTransformer in project gradle by gradle.

the class EclipsePlugin method configureEclipseClasspath.

private void configureEclipseClasspath(final Project project, final EclipseModel model) {
    model.setClasspath(project.getObjects().newInstance(EclipseClasspath.class, project));
    ((IConventionAware) model.getClasspath()).getConventionMapping().map("defaultOutputDir", new Callable<File>() {

        @Override
        public File call() {
            return new File(project.getProjectDir(), EclipsePluginConstants.DEFAULT_PROJECT_OUTPUT_PATH);
        }
    });
    model.getClasspath().getTestSourceSets().convention(testSourceSetsConvention);
    model.getClasspath().getTestConfigurations().convention(testConfigurationsConvention);
    project.getPlugins().withType(JavaBasePlugin.class, new Action<JavaBasePlugin>() {

        @Override
        public void execute(JavaBasePlugin javaBasePlugin) {
            final TaskProvider<GenerateEclipseClasspath> task = project.getTasks().register(ECLIPSE_CP_TASK_NAME, GenerateEclipseClasspath.class, model.getClasspath());
            task.configure(new Action<GenerateEclipseClasspath>() {

                @Override
                public void execute(final GenerateEclipseClasspath task) {
                    task.setDescription("Generates the Eclipse classpath file.");
                    task.setInputFile(project.file(".classpath"));
                    task.setOutputFile(project.file(".classpath"));
                }
            });
            addWorker(task, ECLIPSE_CP_TASK_NAME);
            XmlTransformer xmlTransformer = new XmlTransformer();
            xmlTransformer.setIndentation("\t");
            model.getClasspath().setFile(new XmlFileContentMerger(xmlTransformer));
            model.getClasspath().setSourceSets(project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets());
            AfterEvaluateHelper.afterEvaluateOrExecute(project, new Action<Project>() {

                @Override
                public void execute(Project p) {
                    // keep the ordering we had in earlier gradle versions
                    Set<String> containers = Sets.newLinkedHashSet();
                    containers.add("org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/" + model.getJdt().getJavaRuntimeName() + "/");
                    containers.addAll(model.getClasspath().getContainers());
                    model.getClasspath().setContainers(containers);
                }
            });
            configureScalaDependencies(project, model);
            configureJavaClasspath(project, task, model, testSourceSetsConvention, testConfigurationsConvention);
        }
    });
}
Also used : Action(org.gradle.api.Action) EclipseClasspath(org.gradle.plugins.ide.eclipse.model.EclipseClasspath) XmlFileContentMerger(org.gradle.plugins.ide.api.XmlFileContentMerger) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) XmlTransformer(org.gradle.internal.xml.XmlTransformer) EclipseProject(org.gradle.plugins.ide.eclipse.model.EclipseProject) Project(org.gradle.api.Project) JavaBasePlugin(org.gradle.api.plugins.JavaBasePlugin) File(java.io.File) TaskProvider(org.gradle.api.tasks.TaskProvider)

Example 4 with XmlTransformer

use of org.gradle.internal.xml.XmlTransformer in project gradle by gradle.

the class IdeaPlugin method configureIdeaProject.

private void configureIdeaProject(final Project project) {
    if (isRoot()) {
        XmlFileContentMerger ipr = new XmlFileContentMerger(new XmlTransformer());
        final IdeaProject ideaProject = instantiator.newInstance(IdeaProject.class, project, ipr);
        final TaskProvider<GenerateIdeaProject> projectTask = project.getTasks().register(IDEA_PROJECT_TASK_NAME, GenerateIdeaProject.class, ideaProject);
        projectTask.configure(new Action<GenerateIdeaProject>() {

            @Override
            public void execute(GenerateIdeaProject projectTask) {
                projectTask.setDescription("Generates IDEA project file (IPR)");
            }
        });
        ideaModel.setProject(ideaProject);
        ideaProject.setOutputFile(new File(project.getProjectDir(), project.getName() + ".ipr"));
        ConventionMapping conventionMapping = ((IConventionAware) ideaProject).getConventionMapping();
        conventionMapping.map("jdkName", new Callable<String>() {

            @Override
            public String call() {
                return JavaVersion.current().toString();
            }
        });
        conventionMapping.map("languageLevel", new Callable<IdeaLanguageLevel>() {

            @Override
            public IdeaLanguageLevel call() {
                JavaVersion maxSourceCompatibility = getMaxJavaModuleCompatibilityVersionFor(SOURCE_COMPATIBILITY);
                return new IdeaLanguageLevel(maxSourceCompatibility);
            }
        });
        conventionMapping.map("targetBytecodeVersion", new Callable<JavaVersion>() {

            @Override
            public JavaVersion call() {
                return getMaxJavaModuleCompatibilityVersionFor(TARGET_COMPATIBILITY);
            }
        });
        ideaProject.getWildcards().addAll(Arrays.asList("!?*.class", "!?*.scala", "!?*.groovy", "!?*.java"));
        conventionMapping.map("modules", new Callable<List<IdeaModule>>() {

            @Override
            public List<IdeaModule> call() {
                return Lists.newArrayList(Iterables.transform(Sets.filter(project.getRootProject().getAllprojects(), new Predicate<Project>() {

                    @Override
                    public boolean apply(Project p) {
                        return p.getPlugins().hasPlugin(IdeaPlugin.class);
                    }
                }), new Function<Project, IdeaModule>() {

                    @Override
                    public IdeaModule apply(Project p) {
                        return ideaModelFor(p).getModule();
                    }
                }));
            }
        });
        conventionMapping.map("pathFactory", new Callable<PathFactory>() {

            @Override
            public PathFactory call() {
                return new PathFactory().addPathVariable("PROJECT_DIR", projectTask.get().getOutputFile().getParentFile());
            }
        });
        addWorker(projectTask, IDEA_PROJECT_TASK_NAME);
        addWorkspace(ideaProject);
    }
}
Also used : IdeaProject(org.gradle.plugins.ide.idea.model.IdeaProject) PathFactory(org.gradle.plugins.ide.idea.model.PathFactory) XmlFileContentMerger(org.gradle.plugins.ide.api.XmlFileContentMerger) IdeaLanguageLevel(org.gradle.plugins.ide.idea.model.IdeaLanguageLevel) JavaVersion(org.gradle.api.JavaVersion) ConventionMapping(org.gradle.api.internal.ConventionMapping) Predicate(com.google.common.base.Predicate) XmlTransformer(org.gradle.internal.xml.XmlTransformer) Project(org.gradle.api.Project) IdeaProject(org.gradle.plugins.ide.idea.model.IdeaProject) IdeaModule(org.gradle.plugins.ide.idea.model.IdeaModule) List(java.util.List) File(java.io.File) IConventionAware(org.gradle.api.internal.IConventionAware)

Example 5 with XmlTransformer

use of org.gradle.internal.xml.XmlTransformer in project gradle by gradle.

the class IdeaPlugin method configureIdeaWorkspace.

private void configureIdeaWorkspace(final Project project) {
    if (isRoot()) {
        final IdeaWorkspace workspace = project.getObjects().newInstance(IdeaWorkspace.class);
        workspace.setIws(new XmlFileContentMerger(new XmlTransformer()));
        ideaModel.setWorkspace(workspace);
        final TaskProvider<GenerateIdeaWorkspace> task = project.getTasks().register(IDEA_WORKSPACE_TASK_NAME, GenerateIdeaWorkspace.class, workspace);
        task.configure(new Action<GenerateIdeaWorkspace>() {

            @Override
            public void execute(GenerateIdeaWorkspace task) {
                task.setDescription("Generates an IDEA workspace file (IWS)");
                task.setOutputFile(new File(project.getProjectDir(), project.getName() + ".iws"));
            }
        });
        addWorker(task, IDEA_WORKSPACE_TASK_NAME, false);
    }
}
Also used : XmlTransformer(org.gradle.internal.xml.XmlTransformer) IdeaWorkspace(org.gradle.plugins.ide.idea.model.IdeaWorkspace) XmlFileContentMerger(org.gradle.plugins.ide.api.XmlFileContentMerger) File(java.io.File)

Aggregations

XmlTransformer (org.gradle.internal.xml.XmlTransformer)8 XmlFileContentMerger (org.gradle.plugins.ide.api.XmlFileContentMerger)7 File (java.io.File)5 Project (org.gradle.api.Project)3 ConventionMapping (org.gradle.api.internal.ConventionMapping)3 IConventionAware (org.gradle.api.internal.IConventionAware)3 Set (java.util.Set)2 Action (org.gradle.api.Action)2 IdeaModule (org.gradle.plugins.ide.idea.model.IdeaModule)2 PathFactory (org.gradle.plugins.ide.idea.model.PathFactory)2 Predicate (com.google.common.base.Predicate)1 List (java.util.List)1 Callable (java.util.concurrent.Callable)1 JavaVersion (org.gradle.api.JavaVersion)1 JavaBasePlugin (org.gradle.api.plugins.JavaBasePlugin)1 JavaPlugin (org.gradle.api.plugins.JavaPlugin)1 JavaPluginExtension (org.gradle.api.plugins.JavaPluginExtension)1 WarPlugin (org.gradle.api.plugins.WarPlugin)1 TaskProvider (org.gradle.api.tasks.TaskProvider)1 War (org.gradle.api.tasks.bundling.War)1