Search in sources :

Example 1 with SnapshotVersion

use of org.apache.archiva.model.SnapshotVersion in project archiva by apache.

the class Maven2RepositoryStorage method readProjectVersionMetadata.

@Override
public ProjectVersionMetadata readProjectVersionMetadata(ReadMetadataRequest readMetadataRequest) throws RepositoryStorageMetadataNotFoundException, RepositoryStorageMetadataInvalidException, RepositoryStorageRuntimeException {
    try {
        ManagedRepository managedRepository = repositoryRegistry.getManagedRepository(readMetadataRequest.getRepositoryId());
        boolean isReleases = managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE);
        boolean isSnapshots = managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT);
        String artifactVersion = readMetadataRequest.getProjectVersion();
        // olamy: in case of browsing via the ui we can mix repos (parent of a SNAPSHOT can come from release repo)
        if (!readMetadataRequest.isBrowsingRequest()) {
            if (VersionUtil.isSnapshot(artifactVersion)) {
                // skygo trying to improve speed by honoring managed configuration MRM-1658
                if (isReleases && !isSnapshots) {
                    throw new RepositoryStorageRuntimeException("lookforsnaponreleaseonly", "managed repo is configured for release only");
                }
            } else {
                if (!isReleases && isSnapshots) {
                    throw new RepositoryStorageRuntimeException("lookforsreleaseonsneponly", "managed repo is configured for snapshot only");
                }
            }
        }
        Path basedir = Paths.get(managedRepository.getLocation());
        if (VersionUtil.isSnapshot(artifactVersion)) {
            Path metadataFile = pathTranslator.toFile(basedir, readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), artifactVersion, METADATA_FILENAME);
            try {
                ArchivaRepositoryMetadata metadata = MavenMetadataReader.read(metadataFile);
                // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
                SnapshotVersion snapshotVersion = metadata.getSnapshotVersion();
                if (snapshotVersion != null) {
                    artifactVersion = // remove SNAPSHOT from end
                    artifactVersion.substring(0, artifactVersion.length() - 8);
                    artifactVersion = artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
                }
            } catch (XMLException e) {
                // unable to parse metadata - LOGGER it, and continue with the version as the original SNAPSHOT version
                LOGGER.warn("Invalid metadata: {} - {}", metadataFile, e.getMessage());
            }
        }
        // TODO: won't work well with some other layouts, might need to convert artifact parts to ID by path translator
        String id = readMetadataRequest.getProjectId() + "-" + artifactVersion + ".pom";
        Path file = pathTranslator.toFile(basedir, readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(), id);
        if (!Files.exists(file)) {
            // metadata could not be resolved
            throw new RepositoryStorageMetadataNotFoundException("The artifact's POM file '" + file.toAbsolutePath() + "' was missing");
        }
        // TODO: this is a workaround until we can properly resolve using proxies as well - this doesn't cache
        // anything locally!
        List<RemoteRepository> remoteRepositories = new ArrayList<>();
        Map<String, NetworkProxy> networkProxies = new HashMap<>();
        Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
        List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get(readMetadataRequest.getRepositoryId());
        if (proxyConnectors != null) {
            for (ProxyConnector proxyConnector : proxyConnectors) {
                RemoteRepository remoteRepoConfig = repositoryRegistry.getRemoteRepository(proxyConnector.getTargetRepoId());
                if (remoteRepoConfig != null) {
                    remoteRepositories.add(remoteRepoConfig);
                    NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy(proxyConnector.getProxyId());
                    if (networkProxyConfig != null) {
                        // key/value: remote repo ID/proxy info
                        networkProxies.put(proxyConnector.getTargetRepoId(), networkProxyConfig);
                    }
                }
            }
        }
        // can have released parent pom
        if (readMetadataRequest.isBrowsingRequest()) {
            remoteRepositories.addAll(repositoryRegistry.getRemoteRepositories());
        }
        ModelBuildingRequest req = new DefaultModelBuildingRequest().setProcessPlugins(false).setPomFile(file.toFile()).setTwoPhaseBuilding(false).setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
        // MRM-1607. olamy this will resolve jdk profiles on the current running archiva jvm
        req.setSystemProperties(System.getProperties());
        // MRM-1411
        req.setModelResolver(new RepositoryModelResolver(managedRepository, pathTranslator, wagonFactory, remoteRepositories, networkProxies, managedRepository));
        Model model;
        try {
            model = builder.build(req).getEffectiveModel();
        } catch (ModelBuildingException e) {
            String msg = "The artifact's POM file '" + file + "' was invalid: " + e.getMessage();
            List<ModelProblem> modelProblems = e.getProblems();
            for (ModelProblem problem : modelProblems) {
                // but setTwoPhaseBuilding(true) fix that
                if (((problem.getException() instanceof FileNotFoundException || problem.getException() instanceof NoSuchFileException) && e.getModelId() != null && !e.getModelId().equals(problem.getModelId()))) {
                    LOGGER.warn("The artifact's parent POM file '{}' cannot be resolved. " + "Using defaults for project version metadata..", file);
                    ProjectVersionMetadata metadata = new ProjectVersionMetadata();
                    metadata.setId(readMetadataRequest.getProjectVersion());
                    MavenProjectFacet facet = new MavenProjectFacet();
                    facet.setGroupId(readMetadataRequest.getNamespace());
                    facet.setArtifactId(readMetadataRequest.getProjectId());
                    facet.setPackaging("jar");
                    metadata.addFacet(facet);
                    String errMsg = "Error in resolving artifact's parent POM file. " + (problem.getException() == null ? problem.getMessage() : problem.getException().getMessage());
                    RepositoryProblemFacet repoProblemFacet = new RepositoryProblemFacet();
                    repoProblemFacet.setRepositoryId(readMetadataRequest.getRepositoryId());
                    repoProblemFacet.setId(readMetadataRequest.getRepositoryId());
                    repoProblemFacet.setMessage(errMsg);
                    repoProblemFacet.setProblem(errMsg);
                    repoProblemFacet.setProject(readMetadataRequest.getProjectId());
                    repoProblemFacet.setVersion(readMetadataRequest.getProjectVersion());
                    repoProblemFacet.setNamespace(readMetadataRequest.getNamespace());
                    metadata.addFacet(repoProblemFacet);
                    return metadata;
                }
            }
            throw new RepositoryStorageMetadataInvalidException("invalid-pom", msg, e);
        }
        // Check if the POM is in the correct location
        boolean correctGroupId = readMetadataRequest.getNamespace().equals(model.getGroupId());
        boolean correctArtifactId = readMetadataRequest.getProjectId().equals(model.getArtifactId());
        boolean correctVersion = readMetadataRequest.getProjectVersion().equals(model.getVersion());
        if (!correctGroupId || !correctArtifactId || !correctVersion) {
            StringBuilder message = new StringBuilder("Incorrect POM coordinates in '" + file + "':");
            if (!correctGroupId) {
                message.append("\nIncorrect group ID: ").append(model.getGroupId());
            }
            if (!correctArtifactId) {
                message.append("\nIncorrect artifact ID: ").append(model.getArtifactId());
            }
            if (!correctVersion) {
                message.append("\nIncorrect version: ").append(model.getVersion());
            }
            throw new RepositoryStorageMetadataInvalidException("mislocated-pom", message.toString());
        }
        ProjectVersionMetadata metadata = new ProjectVersionMetadata();
        metadata.setCiManagement(convertCiManagement(model.getCiManagement()));
        metadata.setDescription(model.getDescription());
        metadata.setId(readMetadataRequest.getProjectVersion());
        metadata.setIssueManagement(convertIssueManagement(model.getIssueManagement()));
        metadata.setLicenses(convertLicenses(model.getLicenses()));
        metadata.setMailingLists(convertMailingLists(model.getMailingLists()));
        metadata.setDependencies(convertDependencies(model.getDependencies()));
        metadata.setName(model.getName());
        metadata.setOrganization(convertOrganization(model.getOrganization()));
        metadata.setScm(convertScm(model.getScm()));
        metadata.setUrl(model.getUrl());
        metadata.setProperties(model.getProperties());
        MavenProjectFacet facet = new MavenProjectFacet();
        facet.setGroupId(model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId());
        facet.setArtifactId(model.getArtifactId());
        facet.setPackaging(model.getPackaging());
        if (model.getParent() != null) {
            MavenProjectParent parent = new MavenProjectParent();
            parent.setGroupId(model.getParent().getGroupId());
            parent.setArtifactId(model.getParent().getArtifactId());
            parent.setVersion(model.getParent().getVersion());
            facet.setParent(parent);
        }
        metadata.addFacet(facet);
        return metadata;
    } catch (RepositoryAdminException e) {
        throw new RepositoryStorageRuntimeException("repo-admin", e.getMessage(), e);
    }
}
Also used : ModelBuildingRequest(org.apache.maven.model.building.ModelBuildingRequest) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) ManagedRepository(org.apache.archiva.repository.ManagedRepository) HashMap(java.util.HashMap) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) RemoteRepository(org.apache.archiva.repository.RemoteRepository) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) List(java.util.List) ArrayList(java.util.ArrayList) MailingList(org.apache.maven.model.MailingList) ModelProblem(org.apache.maven.model.building.ModelProblem) ProxyConnector(org.apache.archiva.admin.model.beans.ProxyConnector) Path(java.nio.file.Path) RepositoryProblemFacet(org.apache.archiva.metadata.model.facets.RepositoryProblemFacet) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) XMLException(org.apache.archiva.xml.XMLException) Model(org.apache.maven.model.Model) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 2 with SnapshotVersion

use of org.apache.archiva.model.SnapshotVersion in project archiva by apache.

the class RepositoryModelResolver method findTimeStampedSnapshotPom.

protected Path findTimeStampedSnapshotPom(String groupId, String artifactId, String version, String parentDirectory) {
    // reading metadata if there
    Path mavenMetadata = Paths.get(parentDirectory, METADATA_FILENAME);
    if (Files.exists(mavenMetadata)) {
        try {
            ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read(mavenMetadata);
            SnapshotVersion snapshotVersion = archivaRepositoryMetadata.getSnapshotVersion();
            if (snapshotVersion != null) {
                String lastVersion = snapshotVersion.getTimestamp();
                int buildNumber = snapshotVersion.getBuildNumber();
                String snapshotPath = StringUtils.replaceChars(groupId, '.', '/') + '/' + artifactId + '/' + version + '/' + artifactId + '-' + StringUtils.remove(version, "-" + VersionUtil.SNAPSHOT) + '-' + lastVersion + '-' + buildNumber + ".pom";
                log.debug("use snapshot path {} for maven coordinate {}:{}:{}", snapshotPath, groupId, artifactId, version);
                Path model = basedir.resolve(snapshotPath);
                // model = pathTranslator.toFile( basedir, groupId, artifactId, lastVersion, filename );
                if (Files.exists(model)) {
                    return model;
                }
            }
        } catch (XMLException e) {
            log.warn("fail to read {}, {}", mavenMetadata.toAbsolutePath(), e.getCause());
        }
    }
    return null;
}
Also used : Path(java.nio.file.Path) XMLException(org.apache.archiva.xml.XMLException) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 3 with SnapshotVersion

use of org.apache.archiva.model.SnapshotVersion in project archiva by apache.

the class RepositoryModelResolver method getModelFromProxy.

// FIXME: we need to do some refactoring, we cannot re-use the proxy components of archiva-proxy in maven2-repository
// because it's causing a cyclic dependency
private boolean getModelFromProxy(RemoteRepository remoteRepository, String groupId, String artifactId, String version, String filename) throws AuthorizationException, TransferFailedException, ResourceDoesNotExistException, WagonFactoryException, XMLException, IOException {
    boolean success = false;
    Path tmpMd5 = null;
    Path tmpSha1 = null;
    Path tmpResource = null;
    String artifactPath = pathTranslator.toPath(groupId, artifactId, version, filename);
    Path resource = Paths.get(targetRepository.getLocation()).resolve(artifactPath);
    Path workingDirectory = createWorkingDirectory(targetRepository.getLocation().toString());
    try {
        Wagon wagon = null;
        try {
            String protocol = getProtocol(remoteRepository.getLocation().toString());
            final NetworkProxy networkProxy = this.networkProxyMap.get(remoteRepository.getId());
            wagon = wagonFactory.getWagon(new WagonFactoryRequest("wagon#" + protocol, remoteRepository.getExtraHeaders()).networkProxy(networkProxy));
            if (wagon == null) {
                throw new RuntimeException("Unsupported remote repository protocol: " + protocol);
            }
            boolean connected = connectToRepository(wagon, remoteRepository);
            if (connected) {
                tmpResource = workingDirectory.resolve(filename);
                if (VersionUtil.isSnapshot(version)) {
                    // get the metadata first!
                    Path tmpMetadataResource = workingDirectory.resolve(METADATA_FILENAME);
                    String metadataPath = StringUtils.substringBeforeLast(artifactPath, "/") + "/" + METADATA_FILENAME;
                    wagon.get(addParameters(metadataPath, remoteRepository), tmpMetadataResource.toFile());
                    log.debug("Successfully downloaded metadata.");
                    ArchivaRepositoryMetadata metadata = MavenMetadataReader.read(tmpMetadataResource);
                    // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
                    SnapshotVersion snapshotVersion = metadata.getSnapshotVersion();
                    String timestampVersion = version;
                    if (snapshotVersion != null) {
                        timestampVersion = timestampVersion.substring(0, timestampVersion.length() - // remove SNAPSHOT from end
                        8);
                        timestampVersion = timestampVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
                        filename = artifactId + "-" + timestampVersion + ".pom";
                        artifactPath = pathTranslator.toPath(groupId, artifactId, version, filename);
                        log.debug("New artifactPath :{}", artifactPath);
                    }
                }
                log.info("Retrieving {} from {}", artifactPath, remoteRepository.getName());
                wagon.get(addParameters(artifactPath, remoteRepository), tmpResource.toFile());
                log.debug("Downloaded successfully.");
                tmpSha1 = transferChecksum(wagon, remoteRepository, artifactPath, tmpResource, workingDirectory, ".sha1");
                tmpMd5 = transferChecksum(wagon, remoteRepository, artifactPath, tmpResource, workingDirectory, ".md5");
            }
        } finally {
            if (wagon != null) {
                try {
                    wagon.disconnect();
                } catch (ConnectionException e) {
                    log.warn("Unable to disconnect wagon.", e);
                }
            }
        }
        if (resource != null) {
            synchronized (resource.toAbsolutePath().toString().intern()) {
                Path directory = resource.getParent();
                moveFileIfExists(tmpMd5, directory);
                moveFileIfExists(tmpSha1, directory);
                moveFileIfExists(tmpResource, directory);
                success = true;
            }
        }
    } finally {
        org.apache.archiva.common.utils.FileUtils.deleteQuietly(workingDirectory);
    }
    return success;
}
Also used : Path(java.nio.file.Path) WagonFactoryRequest(org.apache.archiva.proxy.common.WagonFactoryRequest) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) Wagon(org.apache.maven.wagon.Wagon) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 4 with SnapshotVersion

use of org.apache.archiva.model.SnapshotVersion in project archiva by apache.

the class DefaultFileUploadService method updateVersionMetadata.

/**
 * Update version level metadata for snapshot artifacts. If it does not exist, create the metadata and fix checksums
 * if necessary.
 */
private void updateVersionMetadata(ArchivaRepositoryMetadata metadata, Path metadataFile, Date lastUpdatedTimestamp, String timestamp, int buildNumber, boolean fixChecksums, FileMetadata fileMetadata, String groupId, String artifactId, String version, String packaging) throws RepositoryMetadataException {
    if (!Files.exists(metadataFile)) {
        metadata.setGroupId(groupId);
        metadata.setArtifactId(artifactId);
        metadata.setVersion(version);
    }
    if (metadata.getSnapshotVersion() == null) {
        metadata.setSnapshotVersion(new SnapshotVersion());
    }
    metadata.getSnapshotVersion().setBuildNumber(buildNumber);
    metadata.getSnapshotVersion().setTimestamp(timestamp);
    metadata.setLastUpdatedTimestamp(lastUpdatedTimestamp);
    RepositoryMetadataWriter.write(metadata, metadataFile);
    if (fixChecksums) {
        fixChecksums(metadataFile);
    }
}
Also used : SnapshotVersion(org.apache.archiva.model.SnapshotVersion)

Example 5 with SnapshotVersion

use of org.apache.archiva.model.SnapshotVersion in project archiva by apache.

the class MetadataTools method gatherSnapshotVersions.

/**
 * Gather the set of snapshot versions found in a particular versioned reference.
 *
 * @return the Set of snapshot artifact versions found.
 * @throws LayoutException
 * @throws ContentNotFoundException
 */
public Set<String> gatherSnapshotVersions(ManagedRepositoryContent managedRepository, VersionedReference reference) throws LayoutException, IOException, ContentNotFoundException {
    Set<String> foundVersions = managedRepository.getVersions(reference);
    // Next gather up the referenced 'latest' versions found in any proxied repositories
    // maven-metadata-${proxyId}.xml files that may be present.
    // Does this repository have a set of remote proxied repositories?
    Set<String> proxiedRepoIds = this.proxies.get(managedRepository.getId());
    if (CollectionUtils.isNotEmpty(proxiedRepoIds)) {
        String baseVersion = VersionUtil.getBaseVersion(reference.getVersion());
        baseVersion = baseVersion.substring(0, baseVersion.indexOf(VersionUtil.SNAPSHOT) - 1);
        // Add in the proxied repo version ids too.
        Iterator<String> it = proxiedRepoIds.iterator();
        while (it.hasNext()) {
            String proxyId = it.next();
            ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata(managedRepository, reference, proxyId);
            if (proxyMetadata == null) {
                // There is no proxy metadata, skip it.
                continue;
            }
            // Is there some snapshot info?
            SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
            if (snapshot != null) {
                String timestamp = snapshot.getTimestamp();
                int buildNumber = snapshot.getBuildNumber();
                // Only interested in the timestamp + buildnumber.
                if (StringUtils.isNotBlank(timestamp) && (buildNumber > 0)) {
                    foundVersions.add(baseVersion + "-" + timestamp + "-" + buildNumber);
                }
            }
        }
    }
    return foundVersions;
}
Also used : SnapshotVersion(org.apache.archiva.model.SnapshotVersion) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Aggregations

SnapshotVersion (org.apache.archiva.model.SnapshotVersion)9 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)7 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)2 NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)2 XMLException (org.apache.archiva.xml.XMLException)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)1 ProxyConnector (org.apache.archiva.admin.model.beans.ProxyConnector)1 ChecksummedFile (org.apache.archiva.checksum.ChecksummedFile)1 VersionComparator (org.apache.archiva.common.utils.VersionComparator)1 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)1 RepositoryProblemFacet (org.apache.archiva.metadata.model.facets.RepositoryProblemFacet)1