Search in sources :

Example 6 with MavenArtifactFacet

use of org.apache.archiva.maven.metadata.model.MavenArtifactFacet in project archiva by apache.

the class ArtifactBuilder method build.

public Artifact build() {
    String type = null, classifier = null;
    MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    if (facet != null) {
        type = facet.getType();
        classifier = facet.getClassifier();
    }
    ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactMetadata.getNamespace()).withProjectId(artifactMetadata.getProject()).withVersion(artifactMetadata.getProjectVersion()).withArtifactId(artifactMetadata.getProject()).withArtifactVersion(artifactMetadata.getVersion());
    if (StringUtils.isNotEmpty(type)) {
        selectorBuilder.withType(type);
    }
    if (StringUtils.isNotEmpty(classifier)) {
        selectorBuilder.withClassifier(classifier);
    }
    BaseRepositoryContentLayout layout;
    try {
        layout = managedRepositoryContent.getLayout(BaseRepositoryContentLayout.class);
    } catch (LayoutException e) {
        throw new RuntimeException("Could not convert to layout " + e.getMessage());
    }
    org.apache.archiva.repository.content.Artifact repoArtifact = layout.getArtifact(selectorBuilder.build());
    String extension = repoArtifact.getExtension();
    Artifact artifact = new Artifact(repoArtifact.getVersion().getProject().getNamespace().getId(), repoArtifact.getId(), repoArtifact.getArtifactVersion());
    artifact.setRepositoryId(artifactMetadata.getRepositoryId());
    artifact.setClassifier(classifier);
    artifact.setPackaging(type);
    artifact.setType(type);
    artifact.setFileExtension(extension);
    artifact.setPath(managedRepositoryContent.toPath(repoArtifact));
    // TODO: find a reusable formatter for this
    double s = this.artifactMetadata.getSize();
    String symbol = "b";
    if (s > 1024) {
        symbol = "K";
        s /= 1024;
        if (s > 1024) {
            symbol = "M";
            s /= 1024;
            if (s > 1024) {
                symbol = "G";
                s /= 1024;
            }
        }
    }
    artifact.setContext(managedRepositoryContent.getId());
    DecimalFormat df = new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.US));
    artifact.setSize(df.format(s) + " " + symbol);
    artifact.setId(repoArtifact.getId() + "-" + repoArtifact.getArtifactVersion() + "." + repoArtifact.getType());
    return artifact;
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) MavenArtifactFacet(org.apache.archiva.maven.metadata.model.MavenArtifactFacet) Artifact(org.apache.archiva.maven.model.Artifact) LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout)

Example 7 with MavenArtifactFacet

use of org.apache.archiva.maven.metadata.model.MavenArtifactFacet in project archiva by apache.

the class ManagedRepositoryContentMock method getArtifactFromId.

public ArtifactMetadata getArtifactFromId(String repoId, String namespace, String projectId, String projectVersion, String id) {
    if (!id.startsWith(projectId + "-")) {
        throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't start with artifact ID '" + projectId + "'");
    }
    MavenArtifactFacet facet = new MavenArtifactFacet();
    int index = projectId.length() + 1;
    String version;
    String idSubStrFromVersion = id.substring(index);
    if (idSubStrFromVersion.startsWith(projectVersion) && !VersionUtil.isUniqueSnapshot(projectVersion)) {
        // non-snapshot versions, or non-timestamped snapshot versions
        version = projectVersion;
    } else if (VersionUtil.isGenericSnapshot(projectVersion)) {
        // timestamped snapshots
        try {
            // 8 is length of "SNAPSHOT"
            int mainVersionLength = projectVersion.length() - 8;
            if (mainVersionLength == 0) {
                throw new IllegalArgumentException("Timestamped snapshots must contain the main version, filename was '" + id + "'");
            }
            Matcher m = TIMESTAMP_PATTERN.matcher(idSubStrFromVersion.substring(mainVersionLength));
            m.matches();
            String timestamp = m.group(1);
            String buildNumber = m.group(2);
            facet.setTimestamp(timestamp);
            facet.setBuildNumber(Integer.parseInt(buildNumber));
            version = idSubStrFromVersion.substring(0, mainVersionLength) + timestamp + "-" + buildNumber;
        } catch (IllegalStateException e) {
            throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain a timestamped version matching snapshot '" + projectVersion + "'", e);
        }
    } else {
        // invalid
        throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '" + projectVersion + "'");
    }
    String classifier;
    String ext;
    index += version.length();
    if (index == id.length()) {
        // no classifier or extension
        classifier = null;
        ext = null;
    } else {
        char c = id.charAt(index);
        if (c == '-') {
            // classifier up until '.'
            int extIndex = id.indexOf('.', index);
            if (extIndex >= 0) {
                classifier = id.substring(index + 1, extIndex);
                ext = id.substring(extIndex + 1);
            } else {
                classifier = id.substring(index + 1);
                ext = null;
            }
        } else if (c == '.') {
            // rest is the extension
            classifier = null;
            ext = id.substring(index + 1);
        } else {
            throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' expected classifier or extension but got '" + id.substring(index) + "'");
        }
    }
    ArtifactMetadata metadata = new ArtifactMetadata();
    metadata.setId(id);
    metadata.setNamespace(namespace);
    metadata.setProject(projectId);
    metadata.setRepositoryId(repoId);
    metadata.setProjectVersion(projectVersion);
    metadata.setVersion(version);
    facet.setClassifier(classifier);
    // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
    // to select the correct order to apply multiple extensions mappings to a preferred type
    // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
    // perhaps the plugins could register missing entries in configuration, then we just use configuration
    // here?
    String type = null;
    // use extension as default
    if (type == null) {
        type = ext;
    }
    // TODO: should we allow this instead?
    if (type == null) {
        throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type");
    }
    facet.setType(type);
    metadata.addFacet(facet);
    return metadata;
}
Also used : Matcher(java.util.regex.Matcher) MavenArtifactFacet(org.apache.archiva.maven.metadata.model.MavenArtifactFacet) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 8 with MavenArtifactFacet

use of org.apache.archiva.maven.metadata.model.MavenArtifactFacet in project archiva by apache.

the class ArtifactExtensionMappingTest method getTypeFromArtifactId.

private String getTypeFromArtifactId(String artifactId) {
    ArtifactMetadata artifact = pathTranslator.getArtifactFromId(null, "groupId", artifactId, "1.0", artifactId + "-1.0.jar");
    MavenArtifactFacet facet = (MavenArtifactFacet) artifact.getFacet(MavenArtifactFacet.FACET_ID);
    return facet.getType();
}
Also used : MavenArtifactFacet(org.apache.archiva.maven.metadata.model.MavenArtifactFacet) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 9 with MavenArtifactFacet

use of org.apache.archiva.maven.metadata.model.MavenArtifactFacet in project archiva by apache.

the class AbstractRepositoryPurge method removeArtifact.

/*
     * Removes the artifact from the metadataRepository. If a classifier is set, the facet will be removed.
     */
private void removeArtifact(MetadataRepository metadataRepository, ArtifactInfo artifactInfo, ArtifactMetadata artifactMetadata) throws MetadataRepositoryException {
    if (artifactInfo.hasClassifier()) {
        // cleanup facet which contains classifier information
        MavenArtifactFacet mavenArtifactFacet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
        if (mavenArtifactFacet != null && StringUtils.equals(artifactInfo.classifier, mavenArtifactFacet.getClassifier())) {
            artifactMetadata.removeFacet(MavenArtifactFacet.FACET_ID);
            String groupId = artifactInfo.getNamespace(), artifactId = artifactInfo.getName(), version = artifactInfo.getProjectVersion();
            MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
            mavenArtifactFacetToCompare.setClassifier(artifactInfo.getClassifier());
            metadataRepository.removeFacetFromArtifact(repositorySession, repository.getId(), groupId, artifactId, version, mavenArtifactFacetToCompare);
            try {
                repositorySession.save();
            } catch (MetadataSessionException e) {
                log.error("Could not save session {}", e.getMessage());
            }
        }
    } else {
        metadataRepository.removeTimestampedArtifact(repositorySession, artifactMetadata, artifactInfo.getProjectVersion());
    }
}
Also used : MavenArtifactFacet(org.apache.archiva.maven.metadata.model.MavenArtifactFacet)

Example 10 with MavenArtifactFacet

use of org.apache.archiva.maven.metadata.model.MavenArtifactFacet in project archiva by apache.

the class Maven2RepositoryPathTranslator method getArtifactFromId.

@Override
public ArtifactMetadata getArtifactFromId(String repoId, String namespace, String projectId, String projectVersion, String id) {
    if (!id.startsWith(projectId + "-")) {
        throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't start with artifact ID '" + projectId + "'");
    }
    MavenArtifactFacet facet = new MavenArtifactFacet();
    int index = projectId.length() + 1;
    String version;
    String idSubStrFromVersion = id.substring(index);
    if (idSubStrFromVersion.startsWith(projectVersion) && !VersionUtil.isUniqueSnapshot(projectVersion)) {
        // non-snapshot versions, or non-timestamped snapshot versions
        version = projectVersion;
    } else if (VersionUtil.isGenericSnapshot(projectVersion)) {
        // timestamped snapshots
        try {
            // 8 is length of "SNAPSHOT"
            int mainVersionLength = projectVersion.length() - 8;
            if (mainVersionLength == 0) {
                throw new IllegalArgumentException("Timestamped snapshots must contain the main version, filename was '" + id + "'");
            }
            Matcher m = TIMESTAMP_PATTERN.matcher(idSubStrFromVersion.substring(mainVersionLength));
            m.matches();
            String timestamp = m.group(1);
            String buildNumber = m.group(2);
            facet.setTimestamp(timestamp);
            facet.setBuildNumber(Integer.parseInt(buildNumber));
            version = idSubStrFromVersion.substring(0, mainVersionLength) + timestamp + "-" + buildNumber;
        } catch (IllegalStateException e) {
            throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain a timestamped version matching snapshot '" + projectVersion + "'", e);
        }
    } else {
        // invalid
        throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '" + projectVersion + "'");
    }
    String classifier;
    String ext;
    index += version.length();
    if (index == id.length()) {
        // no classifier or extension
        classifier = null;
        ext = null;
    } else {
        char c = id.charAt(index);
        if (c == '-') {
            // classifier up until '.'
            int extIndex = id.indexOf('.', index);
            if (extIndex >= 0) {
                classifier = id.substring(index + 1, extIndex);
                ext = id.substring(extIndex + 1);
            } else {
                classifier = id.substring(index + 1);
                ext = null;
            }
        } else if (c == '.') {
            // rest is the extension
            classifier = null;
            ext = id.substring(index + 1);
        } else {
            throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' expected classifier or extension but got '" + id.substring(index) + "'");
        }
    }
    ArtifactMetadata metadata = new ArtifactMetadata();
    metadata.setId(id);
    metadata.setNamespace(namespace);
    metadata.setProject(projectId);
    metadata.setRepositoryId(repoId);
    metadata.setProjectVersion(projectVersion);
    metadata.setVersion(version);
    facet.setClassifier(classifier);
    // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
    // to select the correct order to apply multiple extensions mappings to a preferred type
    // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
    // perhaps the plugins could register missing entries in configuration, then we just use configuration
    // here?
    String type = null;
    for (ArtifactMappingProvider mapping : artifactMappingProviders) {
        type = mapping.mapClassifierAndExtensionToType(classifier, ext);
        if (type != null) {
            break;
        }
    }
    // TODO: this is cheating! We should check the POM metadata instead
    if (type == null && "jar".equals(ext) && isArtifactIdValidMavenPlugin(projectId)) {
        type = "maven-plugin";
    }
    // use extension as default
    if (type == null) {
        type = ext;
    }
    // TODO: should we allow this instead?
    if (type == null) {
        throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type");
    }
    facet.setType(type);
    metadata.addFacet(facet);
    return metadata;
}
Also used : Matcher(java.util.regex.Matcher) MavenArtifactFacet(org.apache.archiva.maven.metadata.model.MavenArtifactFacet) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Aggregations

MavenArtifactFacet (org.apache.archiva.maven.metadata.model.MavenArtifactFacet)12 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)10 ArrayList (java.util.ArrayList)3 Matcher (java.util.regex.Matcher)3 ReadMetadataRequest (org.apache.archiva.metadata.repository.storage.ReadMetadataRequest)3 Test (org.junit.Test)3 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)2 LayoutException (org.apache.archiva.repository.content.LayoutException)2 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)2 URL (java.net.URL)1 Path (java.nio.file.Path)1 DateFormat (java.text.DateFormat)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 TimeZone (java.util.TimeZone)1 Artifact (org.apache.archiva.maven.model.Artifact)1 RepositoryListener (org.apache.archiva.metadata.audit.RepositoryListener)1 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)1