Search in sources :

Example 6 with ArchivaRepositoryMetadata

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

the class RepositoryMetadataReaderTest method testLoadSimple.

@Test
public void testLoadSimple() throws XMLException {
    Path defaultRepoDir = Paths.get("src/test/repositories/default-repository");
    Path metadataFile = defaultRepoDir.resolve("org/apache/maven/shared/maven-downloader/maven-metadata.xml");
    ArchivaRepositoryMetadata metadata = MavenMetadataReader.read(metadataFile);
    assertNotNull(metadata);
    assertEquals("Group Id", "org.apache.maven.shared", metadata.getGroupId());
    assertEquals("Artifact Id", "maven-downloader", metadata.getArtifactId());
    assertEquals("Released Version", "1.1", metadata.getReleasedVersion());
    assertEquals("List of Available Versions", 2, metadata.getAvailableVersions().size());
    assertTrue("Available version 1.0", metadata.getAvailableVersions().contains("1.0"));
    assertTrue("Available version 1.1", metadata.getAvailableVersions().contains("1.1"));
}
Also used : Path(java.nio.file.Path) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) Test(org.junit.Test)

Example 7 with ArchivaRepositoryMetadata

use of org.apache.archiva.model.ArchivaRepositoryMetadata 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 8 with ArchivaRepositoryMetadata

use of org.apache.archiva.model.ArchivaRepositoryMetadata 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 9 with ArchivaRepositoryMetadata

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

the class Maven2RepositoryMerger method updateVersionMetadata.

private void updateVersionMetadata(Path versionMetaDataFileInTargetRepo, ArtifactMetadata artifactMetadata, Date lastUpdatedTimestamp) throws RepositoryMetadataException {
    ArchivaRepositoryMetadata versionMetadata = getMetadata(versionMetaDataFileInTargetRepo);
    if (!Files.exists(versionMetaDataFileInTargetRepo)) {
        versionMetadata.setGroupId(artifactMetadata.getNamespace());
        versionMetadata.setArtifactId(artifactMetadata.getProject());
        versionMetadata.setVersion(artifactMetadata.getProjectVersion());
    }
    versionMetadata.setLastUpdatedTimestamp(lastUpdatedTimestamp);
    RepositoryMetadataWriter.write(versionMetadata, versionMetaDataFileInTargetRepo);
}
Also used : ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 10 with ArchivaRepositoryMetadata

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

the class Maven2RepositoryMerger method updateProjectMetadata.

private void updateProjectMetadata(Path projectMetaDataFileIntargetRepo, ArtifactMetadata artifactMetadata, Date lastUpdatedTimestamp, String timestamp) throws RepositoryMetadataException {
    ArrayList<String> availableVersions = new ArrayList<>();
    String latestVersion = artifactMetadata.getProjectVersion();
    ArchivaRepositoryMetadata projectMetadata = getMetadata(projectMetaDataFileIntargetRepo);
    if (Files.exists(projectMetaDataFileIntargetRepo)) {
        availableVersions = (ArrayList<String>) projectMetadata.getAvailableVersions();
        Collections.sort(availableVersions, VersionComparator.getInstance());
        if (!availableVersions.contains(artifactMetadata.getVersion())) {
            availableVersions.add(artifactMetadata.getVersion());
        }
        latestVersion = availableVersions.get(availableVersions.size() - 1);
    } else {
        availableVersions.add(artifactMetadata.getProjectVersion());
        projectMetadata.setGroupId(artifactMetadata.getNamespace());
        projectMetadata.setArtifactId(artifactMetadata.getProject());
    }
    if (projectMetadata.getGroupId() == null) {
        projectMetadata.setGroupId(artifactMetadata.getNamespace());
    }
    if (projectMetadata.getArtifactId() == null) {
        projectMetadata.setArtifactId(artifactMetadata.getProject());
    }
    projectMetadata.setLatestVersion(latestVersion);
    projectMetadata.setAvailableVersions(availableVersions);
    projectMetadata.setLastUpdated(timestamp);
    projectMetadata.setLastUpdatedTimestamp(lastUpdatedTimestamp);
    if (!VersionUtil.isSnapshot(artifactMetadata.getVersion())) {
        projectMetadata.setReleasedVersion(latestVersion);
    }
    RepositoryMetadataWriter.write(projectMetadata, projectMetaDataFileIntargetRepo);
}
Also used : ArrayList(java.util.ArrayList) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Aggregations

ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)37 Path (java.nio.file.Path)29 StringWriter (java.io.StringWriter)8 XMLException (org.apache.archiva.xml.XMLException)8 ArrayList (java.util.ArrayList)7 SnapshotVersion (org.apache.archiva.model.SnapshotVersion)7 Test (org.junit.Test)7 IOException (java.io.IOException)5 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)5 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)4 Plugin (org.apache.archiva.model.Plugin)4 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)4 Date (java.util.Date)3 ChecksummedFile (org.apache.archiva.checksum.ChecksummedFile)3 ArtifactReference (org.apache.archiva.model.ArtifactReference)3 RepositoryException (org.apache.archiva.repository.RepositoryException)3 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)3 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 TimeZone (java.util.TimeZone)2