Java Code Examples for org.gradle.api.artifacts.Configuration#getResolvedConfiguration()

The following examples show how to use org.gradle.api.artifacts.Configuration#getResolvedConfiguration() . 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: AppModelGradleResolver.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public Path resolve(AppArtifact appArtifact) throws AppModelResolverException {
    if (!appArtifact.isResolved()) {

        final DefaultDependencyArtifact dep = new DefaultDependencyArtifact();
        dep.setExtension(appArtifact.getType());
        dep.setType(appArtifact.getType());
        dep.setName(appArtifact.getArtifactId());

        final DefaultExternalModuleDependency gradleDep = new DefaultExternalModuleDependency(appArtifact.getGroupId(),
                appArtifact.getArtifactId(), appArtifact.getVersion(), null);
        gradleDep.addArtifact(dep);

        final Configuration detachedConfig = project.getConfigurations().detachedConfiguration(gradleDep);

        final ResolvedConfiguration rc = detachedConfig.getResolvedConfiguration();
        Set<ResolvedArtifact> resolvedArtifacts = rc.getResolvedArtifacts();
        for (ResolvedArtifact a : resolvedArtifacts) {
            if (appArtifact.getArtifactId().equals(a.getName())
                    && appArtifact.getType().equals(a.getType())
                    && appArtifact.getGroupId().equals(a.getModuleVersion().getId().getGroup())) {
                appArtifact.setPath(a.getFile().toPath());
            }
        }

        if (!appArtifact.isResolved()) {
            throw new AppModelResolverException("Failed to resolve " + appArtifact);
        }

    }
    return appArtifact.getPath();
}
 
Example 2
Source File: AppModelGradleResolver.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public AppModel resolveModel(AppArtifact appArtifact) throws AppModelResolverException {
    AppModel.Builder appBuilder = new AppModel.Builder();
    if (appModel != null && appModel.getAppArtifact().equals(appArtifact)) {
        return appModel;
    }
    final List<Dependency> directExtensionDeps = new ArrayList<>();

    // collect enforced platforms
    final Configuration impl = project.getConfigurations().getByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME);
    for (Dependency d : impl.getAllDependencies()) {
        if (!(d instanceof ModuleDependency)) {
            continue;
        }
        final ModuleDependency module = (ModuleDependency) d;
        final Category category = module.getAttributes().getAttribute(Category.CATEGORY_ATTRIBUTE);
        if (category != null && Category.ENFORCED_PLATFORM.equals(category.getName())) {
            directExtensionDeps.add(d);
        }
    }

    final List<AppDependency> userDeps = new ArrayList<>();
    Map<AppArtifactKey, AppDependency> versionMap = new HashMap<>();
    Map<ModuleIdentifier, ModuleVersionIdentifier> userModules = new HashMap<>();

    final String classpathConfigName = launchMode == LaunchMode.TEST ? JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME
            : JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME;

    collectDependencies(project.getConfigurations().getByName(classpathConfigName),
            appBuilder, directExtensionDeps, userDeps,
            versionMap, userModules);

    if (launchMode == LaunchMode.DEVELOPMENT) {
        collectDependencies(project.getConfigurations().getByName(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME),
                appBuilder, directExtensionDeps, userDeps,
                versionMap, userModules);
    }

    final List<AppDependency> deploymentDeps = new ArrayList<>();
    final List<AppDependency> fullDeploymentDeps = new ArrayList<>(userDeps);
    if (!directExtensionDeps.isEmpty()) {
        final Configuration deploymentConfig = project.getConfigurations()
                .detachedConfiguration(directExtensionDeps.toArray(new Dependency[0]));
        final ResolvedConfiguration rc = deploymentConfig.getResolvedConfiguration();
        for (ResolvedArtifact a : rc.getResolvedArtifacts()) {
            final ModuleVersionIdentifier userVersion = userModules.get(getModuleId(a));
            if (userVersion != null || !isDependency(a)) {
                continue;
            }
            final AppDependency dependency = toAppDependency(a);
            fullDeploymentDeps.add(dependency);
            if (!userDeps.contains(dependency)) {
                AppDependency deploymentDep = alignVersion(dependency, versionMap);
                deploymentDeps.add(deploymentDep);
            }
        }
    }

    if (!appArtifact.isResolved()) {
        final Jar jarTask = (Jar) project.getTasks().findByName(JavaPlugin.JAR_TASK_NAME);
        if (jarTask == null) {
            throw new AppModelResolverException("Failed to locate task 'jar' in the project.");
        }
        if (jarTask.getDidWork()) {
            final Provider<RegularFile> jarProvider = jarTask.getArchiveFile();
            Path classesDir = null;
            if (jarProvider.isPresent()) {
                final File f = jarProvider.get().getAsFile();
                if (f.exists()) {
                    classesDir = f.toPath();
                }
            }
            if (classesDir == null) {
                throw new AppModelResolverException("Failed to locate classes directory for " + appArtifact);
            }
            appArtifact.setPaths(PathsCollection.of(classesDir));
        } else {
            final Convention convention = project.getConvention();
            JavaPluginConvention javaConvention = convention.findPlugin(JavaPluginConvention.class);
            if (javaConvention != null) {
                final SourceSet mainSourceSet = javaConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
                PathsCollection.Builder paths = PathsCollection.builder();
                mainSourceSet.getOutput().filter(s -> s.exists()).forEach(f -> {
                    paths.add(f.toPath());
                });
                for (File resourcesDir : mainSourceSet.getResources().getSourceDirectories()) {
                    if (resourcesDir.exists()) {
                        paths.add(resourcesDir.toPath());
                    }
                }
                appArtifact.setPaths(paths.build());
            }
        }
    }

    appBuilder.addRuntimeDeps(userDeps)
            .addFullDeploymentDeps(fullDeploymentDeps)
            .addDeploymentDeps(deploymentDeps)
            .setAppArtifact(appArtifact);
    return this.appModel = appBuilder.build();
}
 
Example 3
Source File: AppModelGradleResolver.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private void collectDependencies(Configuration config, AppModel.Builder appBuilder,
        final List<Dependency> directExtensionDeps,
        final List<AppDependency> userDeps, Map<AppArtifactKey, AppDependency> versionMap,
        Map<ModuleIdentifier, ModuleVersionIdentifier> userModules) {

    final ResolvedConfiguration resolvedConfig = config.getResolvedConfiguration();
    for (ResolvedArtifact a : resolvedConfig.getResolvedArtifacts()) {
        if (!isDependency(a)) {
            continue;
        }
        userModules.put(getModuleId(a), a.getModuleVersion().getId());

        final AppDependency dependency = toAppDependency(a);
        final AppArtifactKey artifactGa = new AppArtifactKey(dependency.getArtifact().getGroupId(),
                dependency.getArtifact().getArtifactId());

        // If we are running in dev mode we prefer directories of classes and resources over the JARs
        // for local project dependencies
        if (LaunchMode.DEVELOPMENT.equals(launchMode)
                && (a.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier)) {
            final Project depProject = project.getRootProject()
                    .findProject(((ProjectComponentIdentifier) a.getId().getComponentIdentifier()).getProjectPath());
            final JavaPluginConvention javaConvention = depProject.getConvention().findPlugin(JavaPluginConvention.class);
            if (javaConvention != null) {
                SourceSet mainSourceSet = javaConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
                final PathsCollection.Builder paths = PathsCollection.builder();
                final Path classesDir = Paths
                        .get(QuarkusGradleUtils.getClassesDir(mainSourceSet, depProject.getBuildDir(), false));
                if (Files.exists(classesDir)) {
                    paths.add(classesDir);
                }
                for (File resourcesDir : mainSourceSet.getResources().getSourceDirectories()) {
                    if (resourcesDir.exists()) {
                        paths.add(resourcesDir.toPath());
                    }
                }
                dependency.getArtifact().setPaths(paths.build());
            }
        }

        if (!dependency.getArtifact().isResolved()) {
            throw new IllegalStateException("Failed to resolve " + a.getId());
        }

        userDeps.add(dependency);
        versionMap.put(artifactGa, dependency);
    }

    collectExtensionDeps(resolvedConfig.getFirstLevelModuleDependencies(), versionMap, appBuilder, directExtensionDeps,
            true, new HashSet<>());
}
 
Example 4
Source File: GenerateProtoTask.java    From curiostack with MIT License 4 votes vote down vote up
private Map<String, File> downloadTools(List<String> artifacts) {
  RepositoryHandler repositories = getProject().getRepositories();
  List<ArtifactRepository> currentRepositories = ImmutableList.copyOf(repositories);
  // Make sure Maven Central is present as a repository since it's the usual place to
  // get protoc, even for non-Java projects. We restore to the previous state after the task.
  repositories.mavenCentral();

  List<Dependency> dependencies =
      artifacts.stream()
          .map(
              artifact -> {
                checkArgument(!artifact.isEmpty(), "artifact must not be empty");

                List<String> coordinateParts = COORDINATE_SPLITTER.splitToList(artifact);

                List<String> artifactParts =
                    ARTIFACT_SPLITTER.splitToList(coordinateParts.get(0));

                ImmutableMap.Builder<String, String> depParts =
                    ImmutableMap.builderWithExpectedSize(5);

                // Do a loose matching to allow for the possibility of dependency management
                // manipulation.
                if (artifactParts.size() > 0) {
                  depParts.put("group", artifactParts.get(0));
                }
                if (artifactParts.size() > 1) {
                  depParts.put("name", artifactParts.get(1));
                }
                if (artifactParts.size() > 2) {
                  depParts.put("version", artifactParts.get(2));
                }

                if (artifactParts.size() > 3) {
                  depParts.put("classifier", artifactParts.get(3));
                } else {
                  depParts.put(
                      "classifier",
                      getProject().getExtensions().getByType(OsDetector.class).getClassifier());
                }

                if (coordinateParts.size() > 1) {
                  depParts.put("ext", coordinateParts.get(1));
                } else {
                  depParts.put("ext", "exe");
                }

                return getProject().getDependencies().create(depParts.build());
              })
          .collect(toImmutableList());
  Configuration configuration = getProject().getConfigurations().getByName("protobufTools");
  configuration.getDependencies().addAll(dependencies);

  // Resolve once to download all tools in parallel.
  configuration.resolve();

  // This will not re-download.
  ResolvedConfiguration resolved = configuration.getResolvedConfiguration();
  Map<String, File> downloaded =
      Streams.zip(
              artifacts.stream(),
              dependencies.stream(),
              (artifact, dep) -> {
                Set<File> files =
                    resolved.getFiles(
                        d -> {
                          // Dependency.contentEquals doesn't match for some reason...
                          return Objects.equals(dep.getGroup(), d.getGroup())
                              && dep.getName().equals(d.getName())
                              && Objects.equals(dep.getVersion(), d.getVersion());
                        });
                checkState(files.size() == 1);

                File file = Iterables.getOnlyElement(files);
                if (!file.canExecute()) {
                  if (!file.setExecutable(true)) {
                    throw new IllegalStateException(
                        "Could not set proto tool to executable: " + file.getAbsolutePath());
                  }
                }

                return new SimpleImmutableEntry<>(artifact, file);
              })
          .collect(toImmutableMap(Entry::getKey, Entry::getValue));

  repositories.clear();
  repositories.addAll(currentRepositories);

  return downloaded;
}