Search in sources :

Example 1 with ArtifactMappingProvider

use of org.apache.archiva.maven.repository.metadata.storage.ArtifactMappingProvider in project archiva by apache.

the class AbstractDefaultRepositoryContent method constructId.

// TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
// to the facet or filename (for the original ID)
private String constructId(String artifactId, String version, String classifier, String type) {
    String ext = null;
    for (ArtifactMappingProvider provider : artifactMappingProviders) {
        ext = provider.mapTypeToExtension(type);
        if (ext != null) {
            break;
        }
    }
    if (ext == null) {
        ext = type;
    }
    StringBuilder id = new StringBuilder();
    if ((version != null) && (type != null)) {
        id.append(artifactId).append(ARTIFACT_SEPARATOR).append(version);
        if (StringUtils.isNotBlank(classifier)) {
            id.append(ARTIFACT_SEPARATOR).append(classifier);
        }
        id.append(".").append(ext);
    }
    return id.toString();
}
Also used : ArtifactMappingProvider(org.apache.archiva.maven.repository.metadata.storage.ArtifactMappingProvider)

Example 2 with ArtifactMappingProvider

use of org.apache.archiva.maven.repository.metadata.storage.ArtifactMappingProvider in project archiva by apache.

the class AbstractDefaultRepositoryContent method getArtifactFromFilename.

public ArchivaItemSelector.Builder getArtifactFromFilename(String namespace, String projectId, String projectVersion, String artifactFileName) {
    if (!artifactFileName.startsWith(projectId + "-")) {
        throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + artifactFileName + "' doesn't start with artifact ID '" + projectId + "'");
    }
    int index = projectId.length() + 1;
    String version;
    String idSubStrFromVersion = artifactFileName.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 '" + artifactFileName + "'");
            }
            Matcher m = TIMESTAMP_PATTERN.matcher(idSubStrFromVersion.substring(mainVersionLength));
            m.matches();
            String timestamp = m.group(1);
            String buildNumber = m.group(2);
            version = idSubStrFromVersion.substring(0, mainVersionLength) + timestamp + "-" + buildNumber;
        } catch (IllegalStateException e) {
            throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + artifactFileName + "' 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 '" + artifactFileName + "' doesn't contain version '" + projectVersion + "'");
    }
    String classifier;
    String ext;
    index += version.length();
    if (index == artifactFileName.length()) {
        // no classifier or extension
        classifier = null;
        ext = null;
    } else {
        char c = artifactFileName.charAt(index);
        if (c == '-') {
            // classifier up until '.'
            int extIndex = artifactFileName.indexOf('.', index);
            if (extIndex >= 0) {
                classifier = artifactFileName.substring(index + 1, extIndex);
                ext = artifactFileName.substring(extIndex + 1);
            } else {
                classifier = artifactFileName.substring(index + 1);
                ext = null;
            }
        } else if (c == '.') {
            // rest is the extension
            classifier = null;
            ext = artifactFileName.substring(index + 1);
        } else {
            throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + artifactFileName + "' expected classifier or extension but got '" + artifactFileName.substring(index) + "'");
        }
    }
    ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder().withNamespace(namespace).withProjectId(projectId).withArtifactId(projectId).withVersion(projectVersion).withArtifactVersion(version).withClassifier(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 '" + artifactFileName + "' does not have a type");
    }
    selectorBuilder.withType(type);
    return selectorBuilder;
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ArtifactMappingProvider(org.apache.archiva.maven.repository.metadata.storage.ArtifactMappingProvider) Matcher(java.util.regex.Matcher)

Aggregations

ArtifactMappingProvider (org.apache.archiva.maven.repository.metadata.storage.ArtifactMappingProvider)2 Matcher (java.util.regex.Matcher)1 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)1