public static void addTypeAndClassifierAndVersion(
      @NotNull InsertionContext context,
      @NotNull MavenDomDependency dependency,
      @NotNull String groupId,
      @NotNull String artifactId) {
    if (!StringUtil.isEmpty(dependency.getVersion().getStringValue())) return;

    Project project = context.getProject();

    if (!isInsideManagedDependency(dependency)) {
      MavenDomProjectModel model =
          DomUtil.<MavenDomProjectModel>getFileElement(dependency).getRootElement();
      MavenDomDependency managedDependency =
          findManagedDependency(model, project, groupId, artifactId);
      if (managedDependency != null) {
        if (dependency.getClassifier().getXmlTag() == null
            && dependency.getType().getXmlTag() == null) {
          String classifier = managedDependency.getClassifier().getRawText();
          if (StringUtil.isNotEmpty(classifier)) {
            dependency.getClassifier().setStringValue(classifier);
          }
          String type = managedDependency.getType().getRawText();
          if (StringUtil.isNotEmpty(type)) {
            dependency.getType().setStringValue(type);
          }
        }
        return;
      }
    }

    MavenProjectIndicesManager manager = MavenProjectIndicesManager.getInstance(project);

    Set<String> versions = manager.getVersions(groupId, artifactId);
    if (versions.size() == 1) {
      dependency.getVersion().setStringValue(ContainerUtil.getFirstItem(versions));
      return;
    }

    dependency.getVersion().setStringValue("");

    int versionPosition =
        dependency.getVersion().getXmlTag().getValue().getTextRange().getStartOffset();

    context.getEditor().getCaretModel().moveToOffset(versionPosition);

    if (versions.size() > 0) {
      invokeCompletion(context, CompletionType.BASIC);
    }
  }
Ejemplo n.º 2
0
  @NotNull
  public static MavenDomDependency createDomDependency(
      MavenDomDependencies dependencies, @Nullable Editor editor, @NotNull final MavenId id) {
    MavenDomDependency dep = createDomDependency(dependencies, editor);

    dep.getGroupId().setStringValue(id.getGroupId());
    dep.getArtifactId().setStringValue(id.getArtifactId());
    dep.getVersion().setStringValue(id.getVersion());

    return dep;
  }
    @NotNull
    @Override
    public String getText() {
      StringBuffer sb = new StringBuffer();

      append(sb, myDependency.getGroupId().getStringValue());
      append(sb, myDependency.getArtifactId().getStringValue());
      append(sb, myDependency.getVersion().getStringValue());

      return sb.toString();
    }
  @Nullable
  private static String createId(MavenDomDependency coordinates) {
    String groupId = coordinates.getGroupId().getStringValue();
    String artifactId = coordinates.getArtifactId().getStringValue();

    if (StringUtil.isEmptyOrSpaces(groupId) || StringUtil.isEmptyOrSpaces(artifactId)) return null;

    String version = coordinates.getVersion().getStringValue();
    String type = coordinates.getType().getStringValue();
    String classifier = coordinates.getClassifier().getStringValue();

    return groupId + ":" + artifactId + ":" + version + ":" + type + ":" + classifier;
  }
  public static boolean processDependenciesInDependencyManagement(
      @NotNull MavenDomProjectModel projectDom,
      @NotNull final Processor<MavenDomDependency> processor,
      @NotNull final Project project) {

    Processor<MavenDomDependencies> managedDependenciesListProcessor =
        dependencies -> {
          SmartList<MavenDomDependency> importDependencies = null;

          for (MavenDomDependency domDependency : dependencies.getDependencies()) {
            if ("import".equals(domDependency.getScope().getRawText())) {
              if (importDependencies == null) {
                importDependencies = new SmartList<MavenDomDependency>();
              }

              importDependencies.add(domDependency);
            } else {
              if (processor.process(domDependency)) return true;
            }
          }

          if (importDependencies != null) {
            for (MavenDomDependency domDependency : importDependencies) {
              GenericDomValue<String> version = domDependency.getVersion();
              if (version.getXmlElement() != null) {
                GenericDomValueReference reference = new GenericDomValueReference(version);
                PsiElement resolve = reference.resolve();

                if (resolve instanceof XmlFile) {
                  MavenDomProjectModel dependModel =
                      MavenDomUtil.getMavenDomModel((PsiFile) resolve, MavenDomProjectModel.class);
                  if (dependModel != null) {
                    for (MavenDomDependency dep :
                        dependModel.getDependencyManagement().getDependencies().getDependencies()) {
                      if (processor.process(dep)) return true;
                    }
                  }
                }
              }
            }
          }

          return false;
        };

    Function<MavenDomProjectModelBase, MavenDomDependencies> domFunction =
        mavenDomProfile -> mavenDomProfile.getDependencyManagement().getDependencies();

    return process(projectDom, managedDependenciesListProcessor, project, domFunction, domFunction);
  }