Search in sources :

Example 46 with MavenId

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

the class MavenIndex method doUpdateIndexData.

private void doUpdateIndexData(IndexData data, MavenProgressIndicator progress) throws IOException, MavenServerIndexerException {
    final Map<String, Set<String>> groupToArtifactMap = new THashMap<>();
    final Map<String, Set<String>> groupWithArtifactToVersionMap = new THashMap<>();
    final StringBuilder builder = new StringBuilder();
    progress.pushState();
    progress.setIndeterminate(true);
    try {
        myIndexer.processArtifacts(data.indexId, new MavenIndicesProcessor() {

            @Override
            public void processArtifacts(Collection<MavenId> artifacts) {
                for (MavenId each : artifacts) {
                    String groupId = each.getGroupId();
                    String artifactId = each.getArtifactId();
                    String version = each.getVersion();
                    builder.setLength(0);
                    builder.append(groupId).append(":").append(artifactId);
                    String ga = builder.toString();
                    getOrCreate(groupToArtifactMap, groupId).add(artifactId);
                    getOrCreate(groupWithArtifactToVersionMap, ga).add(version);
                }
            }
        });
        persist(groupToArtifactMap, data.groupToArtifactMap);
        persist(groupWithArtifactToVersionMap, data.groupWithArtifactToVersionMap);
    } finally {
        progress.popState();
    }
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId) THashSet(gnu.trove.THashSet) MavenIndicesProcessor(org.jetbrains.idea.maven.server.MavenIndicesProcessor) THashMap(gnu.trove.THashMap)

Example 47 with MavenId

use of org.jetbrains.idea.maven.model.MavenId 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 48 with MavenId

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

the class MavenArtifactSearchPanel method getResult.

@NotNull
public List<MavenId> getResult() {
    List<MavenId> result = new ArrayList<>();
    for (TreePath each : myResultList.getSelectionPaths()) {
        Object sel = each.getLastPathComponent();
        MavenArtifactInfo info;
        if (sel instanceof MavenArtifactInfo) {
            info = (MavenArtifactInfo) sel;
        } else {
            info = ((MavenArtifactSearchResult) sel).versions.get(0);
        }
        result.add(new MavenId(info.getGroupId(), info.getArtifactId(), info.getVersion()));
    }
    return result;
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId) TreePath(javax.swing.tree.TreePath) MavenArtifactInfo(org.jetbrains.idea.maven.model.MavenArtifactInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 49 with MavenId

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

the class Maven2ServerIndexerImpl method addArtifact.

public MavenId addArtifact(int indexId, File artifactFile) throws MavenServerIndexerException {
    try {
        IndexingContext index = getIndex(indexId);
        ArtifactContext artifactContext = myArtifactContextProducer.getArtifactContext(index, artifactFile);
        if (artifactContext == null)
            return null;
        addArtifact(myIndexer, index, artifactContext);
        org.sonatype.nexus.index.ArtifactInfo a = artifactContext.getArtifactInfo();
        return new MavenId(a.groupId, a.artifactId, a.version);
    } catch (Exception e) {
        throw new MavenServerIndexerException(wrapException(e));
    }
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId) IndexingContext(org.sonatype.nexus.index.context.IndexingContext) org.sonatype.nexus.index(org.sonatype.nexus.index) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ArchetypeDataSourceException(org.apache.maven.archetype.source.ArchetypeDataSourceException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException)

Example 50 with MavenId

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

the class Maven3ServerIndexerImpl method processArtifacts.

@Override
public void processArtifacts(int indexId, MavenServerIndicesProcessor processor) throws RemoteException, MavenServerIndexerException {
    try {
        final int CHUNK_SIZE = 10000;
        IndexReader r = getIndex(indexId).getIndexReader();
        int total = r.numDocs();
        List<MavenId> result = new ArrayList<MavenId>(Math.min(CHUNK_SIZE, total));
        for (int i = 0; i < total; i++) {
            if (r.isDeleted(i))
                continue;
            Document doc = r.document(i);
            String uinfo = doc.get(SEARCH_TERM_COORDINATES);
            if (uinfo == null)
                continue;
            List<String> parts = StringUtil.split(uinfo, "|");
            String groupId = parts.get(0);
            String artifactId = parts.get(1);
            String version = parts.get(2);
            if (groupId == null || artifactId == null || version == null)
                continue;
            result.add(new MavenId(groupId, artifactId, version));
            if (result.size() == CHUNK_SIZE) {
                processor.processArtifacts(result);
                result.clear();
            }
        }
        if (!result.isEmpty()) {
            processor.processArtifacts(result);
        }
    } catch (Exception e) {
        throw new MavenServerIndexerException(wrapException(e));
    }
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ArchetypeDataSourceException(org.apache.maven.archetype.source.ArchetypeDataSourceException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException)

Aggregations

MavenId (org.jetbrains.idea.maven.model.MavenId)59 MavenProject (org.jetbrains.idea.maven.project.MavenProject)16 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 Module (com.intellij.openapi.module.Module)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)4 IOException (java.io.IOException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 RemoteException (java.rmi.RemoteException)4 ArchetypeDataSourceException (org.apache.maven.archetype.source.ArchetypeDataSourceException)4 SoutMavenConsole (org.jetbrains.idea.maven.execution.SoutMavenConsole)4 MavenArtifact (org.jetbrains.idea.maven.model.MavenArtifact)4 MavenServerExecutionResult (org.jetbrains.idea.maven.server.MavenServerExecutionResult)4 Project (com.intellij.openapi.project.Project)3 File (java.io.File)3 MavenArchetype (org.jetbrains.idea.maven.model.MavenArchetype)3 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2