org.gradle.tooling.model.idea.IdeaModule Java Examples

The following examples show how to use org.gradle.tooling.model.idea.IdeaModule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GradleDependencyAdapter.java    From thorntail with Apache License 2.0 6 votes vote down vote up
/**
 * Compute the dependencies of a given {@code IdeaModule} and group them by their scope.
 *
 * Note: This method does not follow project->project dependencies. It just makes a note of them in a separate collection.
 *
 * @param module the IdeaModule reference.
 */
@SuppressWarnings("UnstableApiUsage")
private void computeProjectDependencies(IdeaModule module) {
    ARTIFACT_DEPS_OF_PRJ.computeIfAbsent(module.getName(), moduleName -> {
        Map<String, Set<ArtifactSpec>> dependencies = new HashMap<>();
        module.getDependencies().forEach(dep -> {
            if (dep instanceof IdeaModuleDependency) {
                // Add the dependency to the list.
                String name = ((IdeaModuleDependency) dep).getTargetModuleName();
                PRJ_DEPS_OF_PRJ.computeIfAbsent(moduleName, key -> new HashSet<>()).add(name);
            } else if (dep instanceof ExternalDependency) {
                ExternalDependency extDep = (ExternalDependency) dep;
                GradleModuleVersion gav = extDep.getGradleModuleVersion();
                ArtifactSpec spec = new ArtifactSpec("compile", gav.getGroup(), gav.getName(), gav.getVersion(),
                                                     "jar", null, extDep.getFile());
                String depScope = dep.getScope().getScope();
                dependencies.computeIfAbsent(depScope, s -> new HashSet<>()).add(spec);
            }
        });
        return dependencies;
    });
}
 
Example #2
Source File: AbstractModelBuilderTest.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
protected <T> Map<String, T> getModulesMap(final Class<T> aClass) {
  final DomainObjectSet<? extends IdeaModule> ideaModules = allModels.getIdeaProject().getModules();

  final String filterKey = "to_filter";
  final Map<String, T> map = ContainerUtil.map2Map(ideaModules, (Function<IdeaModule, Pair<String, T>>)module -> {
    final T value = allModels.getExtraProject(module, aClass);
    final String key = value != null ? module.getGradleProject().getPath() : filterKey;
    return Pair.create(key, value);
  });

  map.remove(filterKey);
  return map;
}
 
Example #3
Source File: GradleProject.java    From meghanada-server with GNU General Public License v3.0 5 votes vote down vote up
private void parseIdeaModule(final IdeaModule ideaModule) throws IOException {
  final org.gradle.tooling.model.GradleProject gradleProject = ideaModule.getGradleProject();
  String name = convertName(gradleProject.getPath());
  if (nonNull(name) && !name.isEmpty()) {
    this.name = name;
  }
  final AndroidProject androidProject =
      AndroidSupport.getAndroidProject(this.rootProject, gradleProject);
  if (nonNull(androidProject)) {
    Set<ProjectDependency> projectDependencies = analyzeDependencies(ideaModule);
    this.dependencies.addAll(projectDependencies);
    // parse android
    this.isAndroidProject = true;
    this.androidApiVersion = androidProject.getApiVersion();
    this.androidModelVersion = androidProject.getModelVersion();
    log.info(
        "detect android project {}. api {} model {}",
        name,
        androidApiVersion,
        androidModelVersion);
    System.setProperty("meghanada.android.project", "true");
    System.setProperty("meghanada.android.project.name", name);
    final AndroidSupport androidSupport = new AndroidSupport(this);
    androidSupport.parseAndroidProject(androidProject);
  } else {
    // normal
    this.parseIdeaModule(gradleProject, ideaModule);
  }
}
 
Example #4
Source File: GradleDependencyAdapter.java    From thorntail with Apache License 2.0 4 votes vote down vote up
/**
 * Get the dependencies via the Gradle IDEA model.
 *
 * @param connection the Gradle project connection.
 * @return the computed dependencies.
 */
private DeclaredDependencies getDependenciesViaIdeaModel(ProjectConnection connection) {
    DeclaredDependencies declaredDependencies = new DeclaredDependencies();

    // 1. Get the IdeaProject model from the Gradle connection.
    IdeaProject prj = connection.getModel(IdeaProject.class);
    prj.getModules().forEach(this::computeProjectDependencies);

    // 2. Find the IdeaModule that maps to the project that we are looking at.
    Optional<? extends IdeaModule> prjModule = prj.getModules().stream()
            .filter(m -> m.getGradleProject().getProjectDirectory().toPath().equals(rootPath))
            .findFirst();


    // We need to return the following collection of dependencies,
    // 1. For the current project, return all artifacts that are marked as ["COMPILE", "TEST"]
    // 2. From the upstream-projects, add all dependencies that are marked as ["COMPILE"]
    // 3. For the current project, iterate through each dependencies marked as ["PROVIDED"] and do the following,
    //      a.) Check if they are already available from 1 & 2. If yes, then nothing to do here.
    //      b.) Check if this entry is defined as ["TEST"] in the upstream-projects. If yes, then include it.
    //          -- The reason for doing this is because of the optimization that Gradle does on the IdeaModule library set.
    Set<ArtifactSpec> collectedDependencies = new HashSet<>();
    prjModule.ifPresent(m -> {
        Map<String, Set<ArtifactSpec>> currentPrjDeps = ARTIFACT_DEPS_OF_PRJ.get(m.getName());
        Set<String> upstreamProjects = PRJ_DEPS_OF_PRJ.getOrDefault(m.getName(), emptySet());

        collectedDependencies.addAll(currentPrjDeps.getOrDefault(DEP_SCOPE_COMPILE, emptySet()));
        collectedDependencies.addAll(currentPrjDeps.getOrDefault(DEP_SCOPE_TEST, emptySet()));

        upstreamProjects.forEach(moduleName -> {
            Map<String, Set<ArtifactSpec>> moduleDeps = ARTIFACT_DEPS_OF_PRJ.getOrDefault(moduleName, emptyMap());
            collectedDependencies.addAll(moduleDeps.getOrDefault(DEP_SCOPE_COMPILE, emptySet()));
        });

        Set<ArtifactSpec> providedScopeDeps = currentPrjDeps.getOrDefault(DEP_SCOPE_PROVIDED, emptySet());
        providedScopeDeps.removeAll(collectedDependencies);

        if (!providedScopeDeps.isEmpty()) {
            List<ArtifactSpec> testScopedLibs = new ArrayList<>();

            upstreamProjects.forEach(moduleName -> testScopedLibs.addAll(
                    ARTIFACT_DEPS_OF_PRJ.getOrDefault(moduleName, emptyMap())
                            .getOrDefault(DEP_SCOPE_TEST, emptySet())));
            providedScopeDeps.stream().filter(testScopedLibs::contains).forEach(collectedDependencies::add);
        }

    });

    collectedDependencies.forEach(declaredDependencies::add);
    return declaredDependencies;
}