List of usage examples for org.apache.maven.artifact.repository.metadata SnapshotArtifactRepositoryMetadata SnapshotArtifactRepositoryMetadata
public SnapshotArtifactRepositoryMetadata(Artifact artifact)
From source file:com.redhat.tools.nexus.audit.serial.store.AbstractAuditStore.java
License:Open Source License
private Gav resolveSnapshotsInGav(final String repoId, final Gav src, final boolean resolveExistingSnapshot) throws AuditStoreException { Gav gav = src;// w w w . ja va 2s .co m if (resolveExistingSnapshot && ArtifactUtils.isSnapshot(gav.getVersion())) { final Artifact a = artifactFactory.createArtifactWithClassifier(gav.getGroupId(), gav.getArtifactId(), gav.getVersion(), gav.getExtension(), gav.getClassifier()); if (logger.isDebugEnabled()) { logger.debug(String.format("Resolving snapshot version in: '%s'", a.getId())); } final SnapshotArtifactRepositoryMetadata m = new SnapshotArtifactRepositoryMetadata(a); final String mPath = repoLayout.pathOfRemoteRepositoryMetadata(m); final File mappingFile = getMetadataFile(repoId, mPath); Reader reader = null; String version = null; try { reader = ReaderFactory.newXmlReader(mappingFile); final MetadataXpp3Reader mappingReader = new MetadataXpp3Reader(); final Metadata metadata = mappingReader.read(reader, false); if (metadata != null && metadata.getVersioning() != null) { final Versioning versioning = metadata.getVersioning(); final Snapshot snapshot = versioning.getSnapshot(); if (snapshot != null) { if (logger.isDebugEnabled()) { logger.debug( String.format("found snapshot metadata. timestamp: '%s'; build-number: '%s'", snapshot.getTimestamp(), snapshot.getBuildNumber())); } Long timestamp = gav.getSnapshotTimeStamp(); Integer buildnumber = gav.getSnapshotBuildNumber(); if (snapshot.getTimestamp() != null) { try { timestamp = Long.valueOf(new SimpleDateFormat(TIMESTAMP_FORMAT) .parse(snapshot.getTimestamp()).getTime()); buildnumber = snapshot.getBuildNumber(); } catch (final ParseException e) { throw new AuditStoreException("Cannot parse snapshot timestamp from metadata: '%s'", e, snapshot.getTimestamp()); } final StringBuilder sb = new StringBuilder(); sb.append(metadata.getVersion().substring(0, metadata.getVersion().indexOf("SNAPSHOT"))) .append(snapshot.getTimestamp()).append("-").append(snapshot.getBuildNumber()); version = sb.toString(); if (logger.isDebugEnabled()) { logger.debug(String.format("resolved snapshot to: '%s'", version)); } gav = new Gav(gav.getGroupId(), gav.getArtifactId(), version, gav.getClassifier(), gav.getExtension(), buildnumber, timestamp, null, true, false, null, false, null); } } } } catch (final FileNotFoundException e) { throw new AuditStoreException("Cannot read metadata from '" + mappingFile + "'", e); } catch (final IOException e) { throw new AuditStoreException("Cannot read metadata from '" + mappingFile + "': " + e.getMessage(), e); } catch (final XmlPullParserException e) { throw new AuditStoreException("Cannot read metadata from '" + mappingFile + "': " + e.getMessage(), e); } catch (final IllegalArtifactCoordinateException e) { throw new AuditStoreException( "Invalid artifact coordinate. Error: %s\nGroup-Id: %s\nArtifact-Id: %s" + "\nVersion: %s\nClassifier: %s\nExtension: %s", e, e.getMessage(), gav.getGroupId(), gav.getArtifactId(), version, gav.getClassifier(), gav.getExtension()); } finally { IOUtil.close(reader); } } return gav; }
From source file:hudson.maven.artifact.transform.AbstractVersionTransformation.java
License:Apache License
protected String resolveVersion(Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories) throws RepositoryMetadataResolutionException { RepositoryMetadata metadata;/*from w w w.ja va2 s . c o m*/ // Don't use snapshot metadata for LATEST (which isSnapshot returns true for) if (!artifact.isSnapshot() || Artifact.LATEST_VERSION.equals(artifact.getBaseVersion())) { metadata = new ArtifactRepositoryMetadata(artifact); } else { metadata = new SnapshotArtifactRepositoryMetadata(artifact); } repositoryMetadataManager.resolve(metadata, remoteRepositories, localRepository); artifact.addMetadata(metadata); Metadata repoMetadata = metadata.getMetadata(); String version = null; if (repoMetadata != null && repoMetadata.getVersioning() != null) { version = constructVersion(repoMetadata.getVersioning(), artifact.getBaseVersion()); } if (version == null) { // use the local copy, or if it doesn't exist - go to the remote repo for it version = artifact.getBaseVersion(); } // TODO: also do this logging for other metadata? // TODO: figure out way to avoid duplicated message if (getLogger().isDebugEnabled()) { if (!version.equals(artifact.getBaseVersion())) { String message = artifact.getArtifactId() + ": resolved to version " + version; if (artifact.getRepository() != null) { message += " from repository " + artifact.getRepository().getId(); } else { message += " from local repository"; } getLogger().debug(message); } else { // Locally installed file is newer, don't use the resolved version getLogger().debug(artifact.getArtifactId() + ": using locally installed snapshot"); } } return version; }
From source file:hudson.maven.artifact.transform.SnapshotTransformation.java
License:Apache License
private int resolveLatestSnapshotBuildNumber(Artifact artifact, ArtifactRepository localRepository, ArtifactRepository remoteRepository) throws RepositoryMetadataResolutionException { RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata(artifact); getLogger().info("Retrieving previous build number from " + remoteRepository.getId()); repositoryMetadataManager.resolveAlways(metadata, localRepository, remoteRepository); int buildNumber = 0; Metadata repoMetadata = metadata.getMetadata(); if ((repoMetadata != null) && (repoMetadata.getVersioning() != null && repoMetadata.getVersioning().getSnapshot() != null)) { buildNumber = repoMetadata.getVersioning().getSnapshot().getBuildNumber(); }/* w w w .j a v a 2 s. c o m*/ return buildNumber; }
From source file:org.apache.archiva.converter.artifact.LegacyToDefaultConverter.java
License:Apache License
@Override public void convert(Artifact artifact, ArtifactRepository targetRepository) throws ArtifactConversionException { if (artifact.getRepository().getUrl().equals(targetRepository.getUrl())) { throw new ArtifactConversionException(Messages.getString("exception.repositories.match")); //$NON-NLS-1$ }//from w w w .j av a2s . co m if (!validateMetadata(artifact)) { addWarning(artifact, Messages.getString("unable.to.validate.metadata")); //$NON-NLS-1$ return; } FileTransaction transaction = new FileTransaction(); if (!copyPom(artifact, targetRepository, transaction)) { addWarning(artifact, Messages.getString("unable.to.copy.pom")); //$NON-NLS-1$ return; } if (!copyArtifact(artifact, targetRepository, transaction)) { addWarning(artifact, Messages.getString("unable.to.copy.artifact")); //$NON-NLS-1$ return; } Metadata metadata = createBaseMetadata(artifact); Versioning versioning = new Versioning(); versioning.addVersion(artifact.getBaseVersion()); metadata.setVersioning(versioning); updateMetadata(new ArtifactRepositoryMetadata(artifact), targetRepository, metadata, transaction); metadata = createBaseMetadata(artifact); metadata.setVersion(artifact.getBaseVersion()); versioning = new Versioning(); Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion()); if (matcher.matches()) { Snapshot snapshot = new Snapshot(); snapshot.setBuildNumber(Integer.parseInt(matcher.group(3))); snapshot.setTimestamp(matcher.group(2)); versioning.setSnapshot(snapshot); } // TODO: merge latest/release/snapshot from source instead metadata.setVersioning(versioning); updateMetadata(new SnapshotArtifactRepositoryMetadata(artifact), targetRepository, metadata, transaction); if (!dryrun) { try { transaction.commit(); } catch (TransactionException e) { throw new ArtifactConversionException(Messages.getString("transaction.failure", e.getMessage()), e); //$NON-NLS-1$ } } }
From source file:org.apache.archiva.converter.artifact.LegacyToDefaultConverter.java
License:Apache License
private boolean validateMetadata(Artifact artifact) throws ArtifactConversionException { ArtifactRepository repository = artifact.getRepository(); boolean result = true; RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata(artifact); File file = new File(repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata(repositoryMetadata)); if (file.exists()) { Metadata metadata = readMetadata(file); result = validateMetadata(metadata, repositoryMetadata, artifact); }/*from w ww . java 2s . c o m*/ repositoryMetadata = new SnapshotArtifactRepositoryMetadata(artifact); file = new File(repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata(repositoryMetadata)); if (file.exists()) { Metadata metadata = readMetadata(file); result = result && validateMetadata(metadata, repositoryMetadata, artifact); } return result; }
From source file:org.codehaus.mojo.mrm.maven.ProxyArtifactStore.java
License:Apache License
/** * {@inheritDoc}/* ww w .ja v a 2s . c o m*/ */ public Metadata getMetadata(String path) throws IOException, MetadataNotFoundException { path = StringUtils.strip(path, "/"); int index = path.lastIndexOf('/'); int index2 = index == -1 ? -1 : path.lastIndexOf('/', index - 1); String version = index2 == -1 ? null : path.substring(index + 1); String artifactId = index2 == -1 ? null : path.substring(index2 + 1, index); String groupId = index2 == -1 ? null : path.substring(0, index2).replace('/', '.'); Metadata metadata = new Metadata(); boolean foundSomething = false; // is this path a groupId:artifactId pair? if (version != null && version.endsWith("-SNAPSHOT") && !StringUtils.isEmpty(artifactId) && !StringUtils.isEmpty(groupId)) { final org.apache.maven.artifact.Artifact artifact = artifactFactory.createDependencyArtifact(groupId, artifactId, VersionRange.createFromVersion(version), "pom", null, "compile"); final SnapshotArtifactRepositoryMetadata artifactRepositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact); try { repositoryMetadataManager.resolve(artifactRepositoryMetadata, remoteRepositories, localRepository); final Metadata artifactMetadata = artifactRepositoryMetadata.getMetadata(); if (artifactMetadata.getVersioning() != null && artifactMetadata.getVersioning().getSnapshot() != null) { foundSomething = true; metadata.setGroupId(groupId); metadata.setArtifactId(artifactId); metadata.setVersion(version); metadata.merge(artifactMetadata); } try { if (artifactMetadata.getVersioning() != null && !artifactMetadata.getVersioning().getSnapshotVersions().isEmpty()) { // TODO up to and including Maven 3.0.3 we do not get a populated SnapshotVersions for (SnapshotVersion v : artifactMetadata.getVersioning().getSnapshotVersions()) { metadata.getVersioning().addSnapshotVersion(v); if (v.getVersion().endsWith("-SNAPSHOT")) { addResolved(new Artifact(groupId, artifactId, version, v.getClassifier(), v.getExtension())); } } } } catch (NoSuchMethodError e) { // ignore Maven 2.x doesn't give us the info } } catch (RepositoryMetadataResolutionException e) { log.debug(e); } } // is this path a groupId:artifactId pair? artifactId = index == -1 ? null : path.substring(index + 1); groupId = index == -1 ? null : path.substring(0, index).replace('/', '.'); if (!StringUtils.isEmpty(artifactId) && !StringUtils.isEmpty(groupId)) { final org.apache.maven.artifact.Artifact artifact = artifactFactory.createDependencyArtifact(groupId, artifactId, anyVersion, "pom", null, "compile"); final ArtifactRepositoryMetadata artifactRepositoryMetadata = new ArtifactRepositoryMetadata(artifact); try { repositoryMetadataManager.resolve(artifactRepositoryMetadata, remoteRepositories, localRepository); final Metadata artifactMetadata = artifactRepositoryMetadata.getMetadata(); if (artifactMetadata.getVersioning() != null) { foundSomething = true; if (StringUtils.isEmpty(metadata.getGroupId())) { metadata.setGroupId(groupId); metadata.setArtifactId(artifactId); } metadata.merge(artifactMetadata); for (String v : artifactMetadata.getVersioning().getVersions()) { addResolved(path + "/" + v); } } } catch (RepositoryMetadataResolutionException e) { log.debug(e); } } // if this path a groupId on its own? groupId = path.replace('/', '.'); final GroupRepositoryMetadata groupRepositoryMetadata = new GroupRepositoryMetadata(groupId); try { repositoryMetadataManager.resolve(groupRepositoryMetadata, remotePluginRepositories, localRepository); foundSomething = true; metadata.merge(groupRepositoryMetadata.getMetadata()); for (Plugin plugin : groupRepositoryMetadata.getMetadata().getPlugins()) { addResolved(path + "/" + plugin.getArtifactId()); } } catch (RepositoryMetadataResolutionException e) { log.debug(e); } if (!foundSomething) { throw new MetadataNotFoundException(path); } addResolved(path); return metadata; }