Search in sources :

Example 6 with ArtifactBuilder

use of org.jfrog.build.extractor.builder.ArtifactBuilder in project build-info by JFrogDev.

the class BuildInfoRecorder method addArtifactsToCurrentModule.

private void addArtifactsToCurrentModule(MavenProject project, ModuleBuilder module) {
    Set<Artifact> moduleArtifacts = currentModuleArtifacts.get();
    if (moduleArtifacts == null) {
        logger.warn("Skipping Artifactory Build-Info module artifact addition: Null current module artifact list.");
        return;
    }
    ArtifactoryClientConfiguration.PublisherHandler publisher = conf.publisher;
    IncludeExcludePatterns patterns = new IncludeExcludePatterns(publisher.getIncludePatterns(), publisher.getExcludePatterns());
    boolean excludeArtifactsFromBuild = publisher.isFilterExcludedArtifactsFromBuild();
    boolean pomFileAdded = false;
    Artifact nonPomArtifact = null;
    String pomFileName = null;
    for (Artifact moduleArtifact : moduleArtifacts) {
        String groupId = moduleArtifact.getGroupId();
        String artifactId = moduleArtifact.getArtifactId();
        String artifactVersion = moduleArtifact.getVersion();
        String artifactClassifier = moduleArtifact.getClassifier();
        String artifactExtension = moduleArtifact.getArtifactHandler().getExtension();
        String type = getTypeString(moduleArtifact.getType(), artifactClassifier, artifactExtension);
        String artifactName = getArtifactName(artifactId, artifactVersion, artifactClassifier, artifactExtension);
        ArtifactBuilder artifactBuilder = new ArtifactBuilder(artifactName).remotePath(getRemotePath(groupId, artifactId, artifactVersion)).type(type);
        File artifactFile = moduleArtifact.getFile();
        if ("pom".equals(type)) {
            pomFileAdded = true;
            // For pom projects take the file from the project if the artifact file is null.
            if (moduleArtifact.equals(project.getArtifact())) {
                // project.getFile() returns the project pom file
                artifactFile = project.getFile();
            }
        } else {
            for (ArtifactMetadata metadata : moduleArtifact.getMetadataList()) {
                if (metadata instanceof ProjectArtifactMetadata) {
                    nonPomArtifact = moduleArtifact;
                    pomFileName = StringUtils.removeEnd(artifactName, artifactExtension) + "pom";
                    break;
                }
            }
        }
        org.jfrog.build.extractor.ci.Artifact artifact = artifactBuilder.build();
        String deploymentPath = getDeploymentPath(groupId, artifactId, artifactVersion, artifactClassifier, artifactExtension);
        if (artifactFile != null && artifactFile.isFile()) {
            boolean pathConflicts = PatternMatcher.pathConflicts(deploymentPath, patterns);
            addArtifactToBuildInfo(artifact, pathConflicts, excludeArtifactsFromBuild, module);
            if (conf.publisher.shouldAddDeployableArtifacts()) {
                addDeployableArtifact(artifact, artifactFile, pathConflicts, groupId, artifactId, artifactVersion, artifactClassifier, artifactExtension);
            }
        }
    }
    /*
         * In case of non packaging Pom project module, we need to create the pom file from the ProjectArtifactMetadata on the Artifact
         */
    if (!pomFileAdded && nonPomArtifact != null) {
        String deploymentPath = getDeploymentPath(nonPomArtifact.getGroupId(), nonPomArtifact.getArtifactId(), nonPomArtifact.getVersion(), nonPomArtifact.getClassifier(), "pom");
        addPomArtifact(nonPomArtifact, module, patterns, deploymentPath, pomFileName, excludeArtifactsFromBuild);
    }
}
Also used : ArtifactoryClientConfiguration(org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration) ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata) IncludeExcludePatterns(org.jfrog.build.extractor.clientConfiguration.IncludeExcludePatterns) BuildInfoExtractorUtils.getModuleIdString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getModuleIdString) BuildInfoExtractorUtils.getTypeString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getTypeString) ArtifactBuilder(org.jfrog.build.extractor.builder.ArtifactBuilder) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) File(java.io.File) ArtifactMetadata(org.apache.maven.artifact.metadata.ArtifactMetadata) ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata)

Example 7 with ArtifactBuilder

use of org.jfrog.build.extractor.builder.ArtifactBuilder in project build-info by JFrogDev.

the class BuildInfoRecorder method addPomArtifact.

private void addPomArtifact(Artifact nonPomArtifact, ModuleBuilder module, IncludeExcludePatterns patterns, String deploymentPath, String pomFileName, boolean excludeArtifactsFromBuild) {
    for (ArtifactMetadata metadata : nonPomArtifact.getMetadataList()) {
        if (metadata instanceof ProjectArtifactMetadata) {
            // The pom metadata
            ArtifactBuilder artifactBuilder = new ArtifactBuilder(pomFileName).remotePath(getRemotePath(metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion())).type("pom");
            File pomFile = ((ProjectArtifactMetadata) metadata).getFile();
            org.jfrog.build.extractor.ci.Artifact pomArtifact = artifactBuilder.build();
            if (pomFile != null && pomFile.isFile()) {
                boolean pathConflicts = PatternMatcher.pathConflicts(deploymentPath, patterns);
                addArtifactToBuildInfo(pomArtifact, pathConflicts, excludeArtifactsFromBuild, module);
                if (conf.publisher.shouldAddDeployableArtifacts()) {
                    addDeployableArtifact(pomArtifact, pomFile, pathConflicts, nonPomArtifact.getGroupId(), nonPomArtifact.getArtifactId(), nonPomArtifact.getVersion(), nonPomArtifact.getClassifier(), "pom");
                }
            }
            break;
        }
    }
}
Also used : ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata) ArtifactMetadata(org.apache.maven.artifact.metadata.ArtifactMetadata) ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata) ArtifactBuilder(org.jfrog.build.extractor.builder.ArtifactBuilder) File(java.io.File)

Example 8 with ArtifactBuilder

use of org.jfrog.build.extractor.builder.ArtifactBuilder in project build-info by JFrogDev.

the class ArtifactoryBuildInfoTrigger method collectModuleInformation.

/**
 * Collect module information for each module.
 *
 * @param event the Ivy publish event
 */
private void collectModuleInformation(IvyEvent event) {
    ArtifactoryClientConfiguration.PublisherHandler publisher = ctx.getClientConf().publisher;
    IncludeExcludePatterns patterns = new IncludeExcludePatterns(publisher.getIncludePatterns(), publisher.getExcludePatterns());
    boolean excludeArtifactsFromBuild = publisher.isFilterExcludedArtifactsFromBuild();
    Project project = (Project) IvyContext.peekInContextStack(IvyTask.ANT_PROJECT_CONTEXT_KEY);
    // Finding module object from context
    @SuppressWarnings("unchecked") final Map<String, String> map = event.getAttributes();
    Module module = getOrCreateModule(map);
    List<Artifact> artifacts = module.getArtifacts();
    if (artifacts == null) {
        module.setArtifacts(new ArrayList<>());
    }
    List<Artifact> excludedArtifacts = module.getExcludedArtifacts();
    if (excludedArtifacts == null) {
        module.setExcludedArtifacts(new ArrayList<>());
    }
    final org.apache.ivy.core.module.descriptor.Artifact pubArtifact = ((PublishEvent) event).getArtifact();
    @SuppressWarnings("unchecked") Map<String, String> extraAttributes = pubArtifact.getExtraAttributes();
    // Using the original file, not the published one that can be far away (network wise)
    String file = map.get("file");
    // But all other attributes are taken from the actual published artifact
    final ModuleRevisionId mrid = pubArtifact.getModuleRevisionId();
    String moduleName = mrid.getName();
    String type = getType(pubArtifact);
    // By default simple name
    String name = pubArtifact.getName() + "-" + mrid.getRevision() + "." + pubArtifact.getExt();
    // Set name from name of published file
    String fullPath = IvyResolverHelper.calculateArtifactPath(publisher, map, extraAttributes);
    int lastSlash = fullPath.lastIndexOf('/');
    if (lastSlash > 0 && lastSlash + 1 < fullPath.length()) {
        name = fullPath.substring(lastSlash + 1);
    }
    project.log("[buildinfo:collect] Collecting artifact " + name + " for module " + moduleName + " using file " + file, Project.MSG_INFO);
    if (isArtifactExist(module.getArtifacts(), name) || isArtifactExist(module.getExcludedArtifacts(), name)) {
        return;
    }
    ArtifactBuilder artifactBuilder = new ArtifactBuilder(name);
    artifactBuilder.type(type);
    File artifactFile = new File(file);
    Map<String, String> checksums = calculateFileChecksum(artifactFile);
    String md5 = checksums.get(MD5_ALGORITHM);
    String sha1 = checksums.get(SHA1_ALGORITHM);
    String sha256 = checksums.get(SHA256_ALGORITHM);
    artifactBuilder.md5(md5).sha1(sha1).sha256(sha256);
    Artifact artifact = artifactBuilder.build();
    if (excludeArtifactsFromBuild && PatternMatcher.pathConflicts(fullPath, patterns)) {
        module.getExcludedArtifacts().add(artifact);
    } else {
        module.getArtifacts().add(artifact);
    }
    @SuppressWarnings("unchecked") DeployDetails deployDetails = buildDeployDetails(artifactFile, artifact, ctx, map, extraAttributes);
    ctx.addDeployDetailsForModule(deployDetails);
    List<Module> contextModules = ctx.getModules();
    if (contextModules.indexOf(module) == -1) {
        ctx.addModule(module);
    }
}
Also used : ArtifactoryClientConfiguration(org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration) EndArtifactPublishEvent(org.apache.ivy.core.event.publish.EndArtifactPublishEvent) PublishEvent(org.apache.ivy.core.event.publish.PublishEvent) DeployDetails(org.jfrog.build.extractor.clientConfiguration.deploy.DeployDetails) IncludeExcludePatterns(org.jfrog.build.extractor.clientConfiguration.IncludeExcludePatterns) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) BuildInfoExtractorUtils.getModuleIdString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getModuleIdString) BuildInfoExtractorUtils.getTypeString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getTypeString) ArtifactBuilder(org.jfrog.build.extractor.builder.ArtifactBuilder) Artifact(org.jfrog.build.extractor.ci.Artifact) Project(org.apache.tools.ant.Project) Module(org.jfrog.build.extractor.ci.Module) File(java.io.File)

Aggregations

ArtifactBuilder (org.jfrog.build.extractor.builder.ArtifactBuilder)8 DeployDetails (org.jfrog.build.extractor.clientConfiguration.deploy.DeployDetails)4 File (java.io.File)3 Artifact (org.jfrog.build.extractor.ci.Artifact)3 ArtifactMetadata (org.apache.maven.artifact.metadata.ArtifactMetadata)2 ProjectArtifactMetadata (org.apache.maven.project.artifact.ProjectArtifactMetadata)2 ArtifactoryUploadResponse (org.jfrog.build.client.ArtifactoryUploadResponse)2 BuildInfoExtractorUtils.getModuleIdString (org.jfrog.build.extractor.BuildInfoExtractorUtils.getModuleIdString)2 BuildInfoExtractorUtils.getTypeString (org.jfrog.build.extractor.BuildInfoExtractorUtils.getTypeString)2 ModuleBuilder (org.jfrog.build.extractor.builder.ModuleBuilder)2 ArtifactoryClientConfiguration (org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration)2 IncludeExcludePatterns (org.jfrog.build.extractor.clientConfiguration.IncludeExcludePatterns)2 ArrayList (java.util.ArrayList)1 EndArtifactPublishEvent (org.apache.ivy.core.event.publish.EndArtifactPublishEvent)1 PublishEvent (org.apache.ivy.core.event.publish.PublishEvent)1 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)1 Artifact (org.apache.maven.artifact.Artifact)1 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)1 Project (org.apache.tools.ant.Project)1 BuildInfoMavenBuilder (org.jfrog.build.extractor.builder.BuildInfoMavenBuilder)1