private void configDependencies() {
    for (MavenArtifact artifact : myMavenProject.getDependencies()) {
      if (!myMavenProject.isSupportedDependency(artifact, SupportedRequestType.FOR_IMPORT))
        continue;

      DependencyScope scope = selectScope(artifact.getScope());
      MavenProject depProject = myMavenTree.findProject(artifact.getMavenId());

      if (depProject != null) {
        if (depProject == myMavenProject) continue;
        boolean isTestJar =
            MavenConstants.TYPE_TEST_JAR.equals(artifact.getType())
                || "tests".equals(artifact.getClassifier());
        myRootModelAdapter.addModuleDependency(
            myMavenProjectToModuleName.get(depProject), scope, isTestJar);

        Element buildHelperCfg =
            depProject.getPluginGoalConfiguration(
                "org.codehaus.mojo", "build-helper-maven-plugin", "attach-artifact");
        if (buildHelperCfg != null) {
          addAttachArtifactDependency(buildHelperCfg, scope, depProject, artifact);
        }
      } else {
        myRootModelAdapter.addLibraryDependency(
            artifact, scope, myModifiableModelsProvider, myMavenProject);
      }
    }

    configSurefirePlugin();
  }
  @Nullable
  private String getRelativeAnnotationProcessorDirectory(boolean isTest) {
    String absoluteAnnotationProcessorDirectory =
        myMavenProject.getAnnotationProcessorDirectory(isTest);
    String absoluteProjectDirectory = myMavenProject.getDirectory();

    return FileUtil.getRelativePath(
        new File(absoluteProjectDirectory), new File(absoluteAnnotationProcessorDirectory));
  }
  private void configEncoding() {
    if (Boolean.parseBoolean(System.getProperty("maven.disable.encode.import"))) return;

    String encoding = myMavenProject.getEncoding();
    if (encoding != null) {
      try {
        EncodingProjectManager.getInstance(myModule.getProject())
            .setEncoding(myMavenProject.getDirectoryFile(), Charset.forName(encoding));
      } catch (UnsupportedCharsetException ignored) {
        /**/
      } catch (IllegalCharsetNameException ignored) {
        /**/
      }
    }
  }
Пример #4
0
  private void doImportProjects(final List<VirtualFile> files, String... profiles) {
    initProjectsManager(false);

    readProjects(files, profiles);

    UIUtil.invokeAndWaitIfNeeded(
        new Runnable() {
          @Override
          public void run() {
            myProjectsManager.waitForResolvingCompletion();
            myProjectsManager.scheduleImportInTests(files);
            myProjectsManager.importProjects();
          }
        });

    for (MavenProject each : myProjectsTree.getProjects()) {
      if (each.hasReadingProblems()) {
        System.out.println(each + " has problems: " + each.getProblems());
      }
    }
  }
  private void addAttachArtifactDependency(
      @NotNull Element buildHelperCfg,
      @NotNull DependencyScope scope,
      @NotNull MavenProject mavenProject,
      @NotNull MavenArtifact artifact) {
    Library.ModifiableModel libraryModel = null;

    for (Element artifactsElement : (List<Element>) buildHelperCfg.getChildren("artifacts")) {
      for (Element artifactElement : (List<Element>) artifactsElement.getChildren("artifact")) {
        String typeString = artifactElement.getChildTextTrim("type");
        if (typeString != null && !typeString.equals("jar")) continue;

        OrderRootType rootType = OrderRootType.CLASSES;

        String classifier = artifactElement.getChildTextTrim("classifier");
        if ("sources".equals(classifier)) {
          rootType = OrderRootType.SOURCES;
        } else if ("javadoc".equals(classifier)) {
          rootType = JavadocOrderRootType.getInstance();
        }

        String filePath = artifactElement.getChildTextTrim("file");
        if (StringUtil.isEmpty(filePath)) continue;

        VirtualFile file = VfsUtil.findRelativeFile(filePath, mavenProject.getDirectoryFile());
        if (file == null) continue;

        file = JarFileSystem.getInstance().getJarRootForLocalFile(file);
        if (file == null) continue;

        if (libraryModel == null) {
          String libraryName = artifact.getLibraryName();
          assert libraryName.startsWith(MavenArtifact.MAVEN_LIB_PREFIX);
          libraryName =
              MavenArtifact.MAVEN_LIB_PREFIX
                  + "ATTACHED-JAR: "
                  + libraryName.substring(MavenArtifact.MAVEN_LIB_PREFIX.length());

          Library library = myModifiableModelsProvider.getLibraryByName(libraryName);
          if (library == null) {
            library = myModifiableModelsProvider.createLibrary(libraryName);
          }
          libraryModel = myModifiableModelsProvider.getLibraryModel(library);

          LibraryOrderEntry entry = myRootModelAdapter.getRootModel().addLibraryEntry(library);
          entry.setScope(scope);
        }

        libraryModel.addRoot(file, rootType);
      }
    }
  }
  private void excludeFromCompilationArchetypeResources() {
    VirtualFile directoryFile = myMavenProject.getDirectoryFile();

    VirtualFile archetypeResourcesDir =
        VfsUtil.findRelativeFile(directoryFile, "src", "main", "resources", "archetype-resources");

    if (archetypeResourcesDir != null) {
      Project project = myModule.getProject();

      CompilerConfigurationImpl compilerConfiguration =
          (CompilerConfigurationImpl) CompilerConfiguration.getInstance(project);

      if (!compilerConfiguration.isExcludedFromCompilation(archetypeResourcesDir)) {
        ExcludedEntriesConfiguration cfg = compilerConfiguration.getExcludedEntriesConfiguration();

        cfg.addExcludeEntryDescription(
            new ExcludeEntryDescription(archetypeResourcesDir, true, false, project));
      }
    }
  }
 private void configLanguageLevel() {
   final LanguageLevel level = LanguageLevel.parse(myMavenProject.getSourceLevel());
   myRootModelAdapter.setLanguageLevel(level);
 }
 private boolean shouldEnableAnnotationProcessors() {
   return myMavenProject.getProcMode() != MavenProject.ProcMode.NONE
       && !"pom".equals(myMavenProject.getPackaging());
 }
  private void configAnnotationProcessors() {
    if (Boolean.parseBoolean(System.getProperty("idea.maven.keep.annotation.processors"))) return;

    Sdk sdk = ModuleRootManager.getInstance(myModule).getSdk();
    if (sdk != null) {
      String versionString = sdk.getVersionString();
      if (versionString != null) {
        if (versionString.contains("1.5")
            || versionString.contains("1.4")
            || versionString.contains("1.3")
            || versionString.contains("1.2")) {
          return;
        }
      }
    }

    CompilerConfigurationImpl compilerConfiguration =
        (CompilerConfigurationImpl) CompilerConfiguration.getInstance(myModule.getProject());

    ProcessorConfigProfile currentProfile =
        compilerConfiguration.getAnnotationProcessingConfiguration(myModule);

    String moduleProfileName = PROFILE_PREFIX + myModule.getName();

    if (currentProfile != compilerConfiguration.getDefaultProcessorProfile()
        && !MAVEN_DEFAULT_ANNOTATION_PROFILE.equals(currentProfile.getName())
        && !moduleProfileName.equals(currentProfile.getName())) {
      return;
    }

    ProcessorConfigProfile moduleProfile =
        compilerConfiguration.findModuleProcessorProfile(moduleProfileName);

    ProcessorConfigProfile defaultMavenProfile =
        compilerConfiguration.findModuleProcessorProfile(MAVEN_DEFAULT_ANNOTATION_PROFILE);

    if (shouldEnableAnnotationProcessors()) {
      String annotationProcessorDirectory = getRelativeAnnotationProcessorDirectory(false);
      if (annotationProcessorDirectory == null) {
        annotationProcessorDirectory = DEFAULT_ANNOTATION_PATH_OUTPUT;
      }

      String testAnnotationProcessorDirectory = getRelativeAnnotationProcessorDirectory(true);
      if (testAnnotationProcessorDirectory == null) {
        testAnnotationProcessorDirectory = DEFAULT_TEST_ANNOTATION_OUTPUT;
      }

      Map<String, String> options = myMavenProject.getAnnotationProcessorOptions();

      List<String> processors = myMavenProject.getDeclaredAnnotationProcessors();

      if (processors == null
          && options.isEmpty()
          && DEFAULT_ANNOTATION_PATH_OUTPUT.equals(annotationProcessorDirectory.replace('\\', '/'))
          && DEFAULT_TEST_ANNOTATION_OUTPUT.equals(
              testAnnotationProcessorDirectory.replace('\\', '/'))) {
        if (moduleProfile != null) {
          compilerConfiguration.removeModuleProcessorProfile(moduleProfile);
        }

        if (defaultMavenProfile == null) {
          defaultMavenProfile = new ProcessorConfigProfileImpl(MAVEN_DEFAULT_ANNOTATION_PROFILE);
          defaultMavenProfile.setEnabled(true);
          defaultMavenProfile.setOutputRelativeToContentRoot(true);
          defaultMavenProfile.setObtainProcessorsFromClasspath(true);
          defaultMavenProfile.setGeneratedSourcesDirectoryName(
              DEFAULT_ANNOTATION_PATH_OUTPUT, false);
          defaultMavenProfile.setGeneratedSourcesDirectoryName(
              DEFAULT_TEST_ANNOTATION_OUTPUT, true);
          compilerConfiguration.addModuleProcessorProfile(defaultMavenProfile);
        }

        defaultMavenProfile.addModuleName(myModule.getName());
      } else {
        if (defaultMavenProfile != null) {
          defaultMavenProfile.removeModuleName(myModule.getName());

          if (defaultMavenProfile.getModuleNames().isEmpty()) {
            compilerConfiguration.removeModuleProcessorProfile(defaultMavenProfile);
          }
        }

        if (moduleProfile == null) {
          moduleProfile = new ProcessorConfigProfileImpl(moduleProfileName);
          moduleProfile.setOutputRelativeToContentRoot(true);
          moduleProfile.setEnabled(true);
          moduleProfile.setObtainProcessorsFromClasspath(true);
          moduleProfile.addModuleName(myModule.getName());
          compilerConfiguration.addModuleProcessorProfile(moduleProfile);
        }

        moduleProfile.setGeneratedSourcesDirectoryName(annotationProcessorDirectory, false);
        moduleProfile.setGeneratedSourcesDirectoryName(testAnnotationProcessorDirectory, true);

        moduleProfile.clearProcessorOptions();
        for (Map.Entry<String, String> entry : options.entrySet()) {
          moduleProfile.setOption(entry.getKey(), entry.getValue());
        }

        moduleProfile.clearProcessors();

        if (processors != null) {
          for (String processor : processors) {
            moduleProfile.addProcessor(processor);
          }
        }
      }
    } else {
      if (defaultMavenProfile != null) {
        defaultMavenProfile.removeModuleName(myModule.getName());

        if (defaultMavenProfile.getModuleNames().isEmpty()) {
          compilerConfiguration.removeModuleProcessorProfile(defaultMavenProfile);
        }
      }

      if (moduleProfile != null) {
        compilerConfiguration.removeModuleProcessorProfile(moduleProfile);
      }
    }
  }
  private void configSurefirePlugin() {
    List<String> urls = new ArrayList<String>();

    AccessToken accessToken = ReadAction.start();
    try {
      MavenDomProjectModel domModel = null;

      Element config =
          myMavenProject.getPluginConfiguration(
              "org.apache.maven.plugins", "maven-surefire-plugin");
      for (String each :
          MavenJDOMUtil.findChildrenValuesByPath(
              config, "additionalClasspathElements", "additionalClasspathElement")) {
        String url = VfsUtil.pathToUrl(each);

        if (domModel == null) {
          domModel =
              MavenDomUtil.getMavenDomProjectModel(myModule.getProject(), myMavenProject.getFile());
        }

        if (domModel != null) {
          url = MavenPropertyResolver.resolve(url, domModel);
        }

        urls.add(url);
      }
    } finally {
      accessToken.finish();
    }

    LibraryTable moduleLibraryTable = myRootModelAdapter.getRootModel().getModuleLibraryTable();

    Library library = moduleLibraryTable.getLibraryByName(SUREFIRE_PLUGIN_LIBRARY_NAME);
    if (library == null) {
      if (urls.isEmpty()) {
        return;
      }

      library = moduleLibraryTable.createLibrary(SUREFIRE_PLUGIN_LIBRARY_NAME);
      LibraryOrderEntry orderEntry =
          myRootModelAdapter.getRootModel().findLibraryOrderEntry(library);
      orderEntry.setScope(DependencyScope.TEST);
    } else {
      if (urls.isEmpty()) {
        moduleLibraryTable.removeLibrary(library);
        return;
      }
    }

    String[] oldUrls = library.getUrls(OrderRootType.CLASSES);
    if (!urls.equals(Arrays.asList(oldUrls))) {
      Library.ModifiableModel modifiableModel = library.getModifiableModel();

      for (String url : oldUrls) {
        modifiableModel.removeRoot(url, OrderRootType.CLASSES);
      }

      for (String url : urls) {
        modifiableModel.addRoot(url, OrderRootType.CLASSES);
      }

      modifiableModel.commit();
    }
  }
 private List<MavenImporter> getSuitableImporters() {
   return myMavenProject.getSuitableImporters();
 }