Search in sources :

Example 16 with Dependency

use of org.jfrog.build.extractor.ci.Dependency in project build-info by JFrogDev.

the class GradleModuleExtractor method calculateDependencies.

private List<Dependency> calculateDependencies(Project project, String moduleId) throws Exception {
    ArtifactoryDependencyResolutionListener artifactoryDependencyResolutionListener = project.getRootProject().getPlugins().getPlugin(ArtifactoryPlugin.class).getArtifactoryDependencyResolutionListener();
    Map<String, String[][]> requestedByMap = artifactoryDependencyResolutionListener.getModulesHierarchyMap().get(moduleId);
    Set<Configuration> configurationSet = project.getConfigurations();
    List<Dependency> dependencies = newArrayList();
    for (Configuration configuration : configurationSet) {
        if (configuration.getState() != Configuration.State.RESOLVED) {
            log.info("Artifacts for configuration '{}' were not all resolved, skipping", configuration.getName());
            continue;
        }
        ResolvedConfiguration resolvedConfiguration = configuration.getResolvedConfiguration();
        Set<ResolvedArtifact> resolvedArtifactSet = resolvedConfiguration.getResolvedArtifacts();
        for (final ResolvedArtifact artifact : resolvedArtifactSet) {
            File file = artifact.getFile();
            if (file.exists()) {
                ModuleVersionIdentifier id = artifact.getModuleVersion().getId();
                final String depId = getModuleIdString(id.getGroup(), id.getName(), id.getVersion());
                // if it's already in the dependencies list just add the current scope
                Dependency existingDependency = dependencies.stream().filter(input -> input.getId().equals(depId)).findAny().orElse(null);
                if (existingDependency != null) {
                    Set<String> existingScopes = existingDependency.getScopes();
                    existingScopes.add(configuration.getName());
                    existingDependency.setScopes(existingScopes);
                } else {
                    DependencyBuilder dependencyBuilder = new DependencyBuilder().type(getTypeString(artifact.getType(), artifact.getClassifier(), artifact.getExtension())).id(depId).scopes(Sets.newHashSet(configuration.getName()));
                    if (requestedByMap != null) {
                        dependencyBuilder.requestedBy(requestedByMap.get(depId));
                    }
                    if (file.isFile()) {
                        // In recent gradle builds (3.4+) subproject dependencies are represented by a dir not jar.
                        Map<String, String> checksums = FileChecksumCalculator.calculateChecksums(file, MD5_ALGORITHM, SHA1_ALGORITHM, SHA256_ALGORITHM);
                        dependencyBuilder.md5(checksums.get(MD5_ALGORITHM)).sha1(checksums.get(SHA1_ALGORITHM)).sha256(checksums.get(SHA256_ALGORITHM));
                    }
                    dependencies.add(dependencyBuilder.build());
                }
            }
        }
    }
    return dependencies;
}
Also used : ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) Configuration(org.gradle.api.artifacts.Configuration) ResolvedConfiguration(org.gradle.api.artifacts.ResolvedConfiguration) ArtifactoryClientConfiguration(org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration) BuildInfoExtractorUtils.getModuleIdString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getModuleIdString) BuildInfoExtractorUtils.getTypeString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getTypeString) Dependency(org.jfrog.build.extractor.ci.Dependency) ArtifactoryPlugin(org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) ResolvedConfiguration(org.gradle.api.artifacts.ResolvedConfiguration) DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) File(java.io.File) ArtifactoryDependencyResolutionListener(org.jfrog.gradle.plugin.artifactory.extractor.listener.ArtifactoryDependencyResolutionListener)

Example 17 with Dependency

use of org.jfrog.build.extractor.ci.Dependency in project build-info by JFrogDev.

the class DownloadTest method testDownloadArtifactFromDifferentPath.

public void testDownloadArtifactFromDifferentPath() throws IOException {
    String targetDirPath = tempWorkspace.getPath() + File.separatorChar + "testDownloadDupArtifactFromDifferentPath" + File.separatorChar;
    FileSpec fileSpec = new FileSpec();
    // Upload one file to different locations in Artifactory.
    try {
        File file = createRandomFile(tempWorkspace.getPath() + File.pathSeparatorChar + "file", 1);
        for (int i = 0; i < 3; i++) {
            String filePath = TEST_REPO_PATH + "/" + i + "/file";
            DeployDetails deployDetails = new DeployDetails.Builder().file(file).artifactPath(filePath).targetRepository(localRepo1).explode(false).packageType(DeployDetails.PackageType.GENERIC).build();
            FilesGroup fg = new FilesGroup();
            fg.setPattern(localRepo1 + "/" + filePath);
            fg.setTarget(targetDirPath);
            fileSpec.addFilesGroup(fg);
            // Upload artifact
            artifactoryManager.upload(deployDetails);
        }
        DependenciesDownloaderHelper helper = new DependenciesDownloaderHelper(artifactoryManager, tempWorkspace.getPath(), log);
        List<Dependency> dependencies = helper.downloadDependencies(fileSpec);
        Assert.assertEquals(dependencies.size(), 3);
    } finally {
        FileUtils.deleteDirectory(tempWorkspace);
    }
}
Also used : DeployDetails(org.jfrog.build.extractor.clientConfiguration.deploy.DeployDetails) FileSpec(org.jfrog.filespecs.FileSpec) Dependency(org.jfrog.build.extractor.ci.Dependency) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FilesGroup(org.jfrog.filespecs.entities.FilesGroup)

Example 18 with Dependency

use of org.jfrog.build.extractor.ci.Dependency in project build-info by JFrogDev.

the class DependenciesDownloaderHelper method downloadDependencies.

public List<Dependency> downloadDependencies(Set<DownloadableArtifact> downloadableArtifacts) throws IOException {
    log.info("Beginning to resolve Build Info published dependencies.");
    List<Dependency> dependencies = new ArrayList<>();
    Set<DownloadableArtifact> downloadedArtifacts = new HashSet<>();
    for (DownloadableArtifact downloadableArtifact : downloadableArtifacts) {
        Dependency dependency = downloadArtifact(downloadableArtifact);
        if (dependency != null) {
            dependencies.add(dependency);
            downloadedArtifacts.add(downloadableArtifact);
            explodeDependenciesIfNeeded(downloadableArtifact);
        }
    }
    removeUnusedArtifactsFromLocal(downloadedArtifacts);
    log.info("Finished resolving Build Info published dependencies.");
    return dependencies;
}
Also used : DownloadableArtifact(org.jfrog.build.api.dependency.DownloadableArtifact) Dependency(org.jfrog.build.extractor.ci.Dependency)

Example 19 with Dependency

use of org.jfrog.build.extractor.ci.Dependency in project build-info by JFrogDev.

the class DependenciesDownloaderHelper method downloadArtifact.

/**
 * Download artifact.
 *
 * @param downloadableArtifact download recipe
 * @param artifactMetaData     the artifact metadata
 * @param uriWithParams        full artifact uri with matrix params
 * @param filePath             the path to file in file system
 * @return artifact dependency
 */
Dependency downloadArtifact(DownloadableArtifact downloadableArtifact, ArtifactMetaData artifactMetaData, String uriWithParams, String filePath) throws IOException {
    String fileDestination = downloader.getTargetDir(downloadableArtifact.getTargetDirPath(), downloadableArtifact.getRelativeDirPath());
    String remotePath = downloadableArtifact.getRepoUrl() + "/" + filePath;
    Dependency dependencyResult = getDependencyLocally(artifactMetaData, fileDestination, remotePath);
    if (dependencyResult != null) {
        return dependencyResult;
    }
    try {
        log.info(String.format("Downloading '%s'...", uriWithParams));
        Map<String, String> checksumsMap = artifactMetaData.getSize() >= MIN_SIZE_FOR_CONCURRENT_DOWNLOAD && artifactMetaData.isAcceptRange() ? downloadFileConcurrently(uriWithParams, artifactMetaData.getSize(), fileDestination, filePath) : downloadFile(uriWithParams, fileDestination);
        // If the checksums map is null then something went wrong and we should fail the build
        if (checksumsMap == null) {
            throw new IOException("Received null checksums map for downloaded file.");
        }
        dependencyResult = validateChecksumsAndBuildDependency(checksumsMap, artifactMetaData, filePath, fileDestination, remotePath);
        log.info(String.format("Successfully downloaded '%s' to '%s'", uriWithParams, fileDestination));
        return dependencyResult;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : Dependency(org.jfrog.build.extractor.ci.Dependency) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 20 with Dependency

use of org.jfrog.build.extractor.ci.Dependency in project build-info by JFrogDev.

the class NugetRun method collectDependenciesFromProjectAssets.

private List<Dependency> collectDependenciesFromProjectAssets(String projectAssetsPath) throws Exception {
    File projectAssets = new File(projectAssetsPath);
    List<Dependency> dependenciesList = new ArrayList<>();
    NugetProjectAssets assets = new NugetProjectAssets();
    assets.readProjectAssets(projectAssets);
    for (Map.Entry<String, NugetProjectAssets.Library> entry : assets.getLibraries().entrySet()) {
        String pkgKey = entry.getKey();
        NugetProjectAssets.Library library = entry.getValue();
        if (library.getType().equals("project")) {
            continue;
        }
        File nupkg = new File(assets.getPackagesPath(), library.getNupkgFilePath());
        if (nupkg.exists()) {
            Map<String, String> checksums = FileChecksumCalculator.calculateChecksums(nupkg, MD5_ALGORITHM, SHA1_ALGORITHM, SHA256_ALGORITHM);
            Dependency dependency = new DependencyBuilder().id(pkgKey.replace('/', ':')).md5(checksums.get(MD5_ALGORITHM)).sha1(checksums.get(SHA1_ALGORITHM)).sha256(checksums.get(SHA256_ALGORITHM)).build();
            dependenciesList.add(dependency);
        } else {
            if (isPackagePartOfTargetDependencies(library.getPath(), assets.getTargets())) {
                logger.warn(String.format("The file %s doesn't exist in the NuGet cache directory but it does exist as a target in the assets files. %s", nupkg.getPath(), ABSENT_NUPKG_WARN_MSG));
                continue;
            }
            throw new Exception(String.format("The file %s doesn't exist in the NuGet cache directory.", nupkg.getPath()));
        }
    }
    return dependenciesList;
}
Also used : Dependency(org.jfrog.build.extractor.ci.Dependency) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) File(java.io.File) NugetProjectAssets(org.jfrog.build.extractor.nuget.types.NugetProjectAssets)

Aggregations

Dependency (org.jfrog.build.extractor.ci.Dependency)29 File (java.io.File)12 DependencyBuilder (org.jfrog.build.extractor.builder.DependencyBuilder)9 Module (org.jfrog.build.extractor.ci.Module)7 IOException (java.io.IOException)5 Test (org.testng.annotations.Test)5 Path (java.nio.file.Path)4 DownloadableArtifact (org.jfrog.build.api.dependency.DownloadableArtifact)4 RandomAccessFile (java.io.RandomAccessFile)3 ArrayList (java.util.ArrayList)3 Artifact (org.jfrog.build.extractor.ci.Artifact)3 BuildInfo (org.jfrog.build.extractor.ci.BuildInfo)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Collectors (java.util.stream.Collectors)2