Search in sources :

Example 76 with MavenProject

use of org.jetbrains.idea.maven.project.MavenProject in project intellij-community by JetBrains.

the class MavenProjectModelModifier method addDependency.

private Promise<Void> addDependency(@NotNull Collection<Module> fromModules, @NotNull final MavenId mavenId, @Nullable String minVersion, @Nullable String maxVersion, @NotNull final DependencyScope scope) {
    final List<Trinity<MavenDomProjectModel, MavenId, String>> models = new ArrayList<>(fromModules.size());
    List<XmlFile> files = new ArrayList<>(fromModules.size());
    List<MavenProject> projectToUpdate = new ArrayList<>(fromModules.size());
    final String mavenScope = getMavenScope(scope);
    for (Module from : fromModules) {
        if (!myProjectsManager.isMavenizedModule(from))
            return null;
        MavenProject fromProject = myProjectsManager.findProject(from);
        if (fromProject == null)
            return null;
        final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(myProject, fromProject.getFile());
        if (model == null)
            return null;
        String scopeToSet = null;
        String version = null;
        if (mavenId.getGroupId() != null && mavenId.getArtifactId() != null) {
            MavenDomDependency managedDependency = MavenDependencyCompletionUtil.findManagedDependency(model, myProject, mavenId.getGroupId(), mavenId.getArtifactId());
            if (managedDependency != null) {
                String managedScope = StringUtil.nullize(managedDependency.getScope().getStringValue(), true);
                scopeToSet = (managedScope == null && MavenConstants.SCOPE_COMPILE.equals(mavenScope)) || StringUtil.equals(managedScope, mavenScope) ? null : mavenScope;
            }
            if (managedDependency == null || StringUtil.isEmpty(managedDependency.getVersion().getStringValue())) {
                version = selectVersion(mavenId, minVersion, maxVersion);
            }
        }
        models.add(Trinity.create(model, new MavenId(mavenId.getGroupId(), mavenId.getArtifactId(), version), scopeToSet));
        files.add(DomUtil.getFile(model));
        projectToUpdate.add(fromProject);
    }
    new WriteCommandAction(myProject, "Add Maven Dependency", PsiUtilCore.toPsiFileArray(files)) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            for (Trinity<MavenDomProjectModel, MavenId, String> trinity : models) {
                final MavenDomProjectModel model = trinity.first;
                MavenDomDependency dependency = MavenDomUtil.createDomDependency(model, null, trinity.second);
                String mavenScope = trinity.third;
                if (mavenScope != null) {
                    dependency.getScope().setStringValue(mavenScope);
                }
                Document document = PsiDocumentManager.getInstance(myProject).getDocument(DomUtil.getFile(model));
                if (document != null) {
                    FileDocumentManager.getInstance().saveDocument(document);
                }
            }
        }
    }.execute();
    return myProjectsManager.forceUpdateProjects(projectToUpdate);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Trinity(com.intellij.openapi.util.Trinity) XmlFile(com.intellij.psi.xml.XmlFile) Document(com.intellij.openapi.editor.Document) Result(com.intellij.openapi.application.Result) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenProject(org.jetbrains.idea.maven.project.MavenProject) Module(com.intellij.openapi.module.Module)

Example 77 with MavenProject

use of org.jetbrains.idea.maven.project.MavenProject in project intellij-community by JetBrains.

the class MavenProjectModelModifier method changeLanguageLevel.

@Override
public Promise<Void> changeLanguageLevel(@NotNull Module module, @NotNull final LanguageLevel level) {
    if (!myProjectsManager.isMavenizedModule(module))
        return null;
    MavenProject mavenProject = myProjectsManager.findProject(module);
    if (mavenProject == null)
        return null;
    final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(myProject, mavenProject.getFile());
    if (model == null)
        return null;
    new WriteCommandAction(myProject, "Add Maven Dependency", DomUtil.getFile(model)) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            MavenDomConfiguration configuration = getCompilerPlugin(model).getConfiguration();
            XmlTag tag = configuration.ensureTagExists();
            setChildTagValue(tag, "source", level.getCompilerComplianceDefaultOption());
            setChildTagValue(tag, "target", level.getCompilerComplianceDefaultOption());
            Document document = PsiDocumentManager.getInstance(myProject).getDocument(DomUtil.getFile(model));
            if (document != null) {
                FileDocumentManager.getInstance().saveDocument(document);
            }
        }
    }.execute();
    return myProjectsManager.forceUpdateProjects(Collections.singleton(mavenProject));
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) MavenProject(org.jetbrains.idea.maven.project.MavenProject) Document(com.intellij.openapi.editor.Document) Result(com.intellij.openapi.application.Result) XmlTag(com.intellij.psi.xml.XmlTag)

Example 78 with MavenProject

use of org.jetbrains.idea.maven.project.MavenProject in project intellij-community by JetBrains.

the class MavenModuleNameMapper method resolveModuleNames.

private static void resolveModuleNames(Collection<MavenProject> projects, Map<MavenProject, Module> mavenProjectToModule, Map<MavenProject, String> mavenProjectToModuleName) {
    NameItem[] names = new NameItem[projects.size()];
    int i = 0;
    for (MavenProject each : projects) {
        names[i++] = new NameItem(each, mavenProjectToModule.get(each));
    }
    Arrays.sort(names);
    Map<String, Integer> nameCounters = new HashMap<>();
    for (i = 0; i < names.length; i++) {
        if (names[i].hasDuplicatedGroup)
            continue;
        for (int k = i + 1; k < names.length; k++) {
            if (names[i].originalName.equals(names[k].originalName)) {
                nameCounters.put(names[i].originalName, 0);
                if (names[i].groupId.equals(names[k].groupId)) {
                    names[i].hasDuplicatedGroup = true;
                    names[k].hasDuplicatedGroup = true;
                }
            }
        }
    }
    Set<String> existingNames = new HashSet<>();
    for (NameItem name : names) {
        if (name.module != null) {
            boolean wasAdded = existingNames.add(name.getResultName());
        //assert wasAdded : name.getResultName();
        }
    }
    for (NameItem nameItem : names) {
        if (nameItem.module == null) {
            Integer c = nameCounters.get(nameItem.originalName);
            if (c != null) {
                nameItem.number = c;
                nameCounters.put(nameItem.originalName, c + 1);
            }
            do {
                String name = nameItem.getResultName();
                if (existingNames.add(name))
                    break;
                nameItem.number++;
                nameCounters.put(nameItem.originalName, nameItem.number + 1);
            } while (true);
        }
    }
    for (NameItem each : names) {
        mavenProjectToModuleName.put(each.project, each.getResultName());
    }
//assert new HashSet<String>(mavenProjectToModuleName.values()).size() == mavenProjectToModuleName.size() : new HashMap<MavenProject, String>(mavenProjectToModuleName);
}
Also used : MavenProject(org.jetbrains.idea.maven.project.MavenProject)

Example 79 with MavenProject

use of org.jetbrains.idea.maven.project.MavenProject in project intellij-community by JetBrains.

the class MavenModuleNameMapper method resolveModulePaths.

private static void resolveModulePaths(Collection<MavenProject> projects, Map<MavenProject, Module> mavenProjectToModule, Map<MavenProject, String> mavenProjectToModuleName, Map<MavenProject, String> mavenProjectToModulePath, String dedicatedModuleDir) {
    for (MavenProject each : projects) {
        Module module = mavenProjectToModule.get(each);
        String path = module != null ? module.getModuleFilePath() : generateModulePath(each, mavenProjectToModuleName, dedicatedModuleDir);
        mavenProjectToModulePath.put(each, path);
    }
}
Also used : MavenProject(org.jetbrains.idea.maven.project.MavenProject) Module(com.intellij.openapi.module.Module)

Example 80 with MavenProject

use of org.jetbrains.idea.maven.project.MavenProject in project intellij-community by JetBrains.

the class ToggleBeforeRunTaskAction method doIsSelected.

@Override
protected boolean doIsSelected(AnActionEvent e) {
    final DataContext context = e.getDataContext();
    final Pair<MavenProject, String> desc = getTaskDesc(context);
    if (desc != null) {
        final RunManagerEx runManager = getRunManager(context);
        if (runManager == null)
            return false;
        for (MavenBeforeRunTask each : runManager.getBeforeRunTasks(MavenBeforeRunTasksProvider.ID)) {
            if (each.isFor(desc.first, desc.second))
                return true;
        }
    }
    return false;
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) MavenProject(org.jetbrains.idea.maven.project.MavenProject) RunManagerEx(com.intellij.execution.RunManagerEx) MavenBeforeRunTask(org.jetbrains.idea.maven.tasks.MavenBeforeRunTask)

Aggregations

MavenProject (org.jetbrains.idea.maven.project.MavenProject)132 VirtualFile (com.intellij.openapi.vfs.VirtualFile)40 MavenProjectsManager (org.jetbrains.idea.maven.project.MavenProjectsManager)20 MavenId (org.jetbrains.idea.maven.model.MavenId)16 Project (com.intellij.openapi.project.Project)10 Nullable (org.jetbrains.annotations.Nullable)9 Bombed (com.intellij.idea.Bombed)8 Module (com.intellij.openapi.module.Module)6 DataContext (com.intellij.openapi.actionSystem.DataContext)5 File (java.io.File)5 ArrayList (java.util.ArrayList)5 Result (com.intellij.openapi.application.Result)4 MavenArtifact (org.jetbrains.idea.maven.model.MavenArtifact)4 MavenEmbeddersManager (org.jetbrains.idea.maven.project.MavenEmbeddersManager)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 TextRange (com.intellij.openapi.util.TextRange)3 NotNull (org.jetbrains.annotations.NotNull)3 MavenRunnerParameters (org.jetbrains.idea.maven.execution.MavenRunnerParameters)3 MavenExplicitProfiles (org.jetbrains.idea.maven.model.MavenExplicitProfiles)3 MavenProjectsTree (org.jetbrains.idea.maven.project.MavenProjectsTree)3