use of org.apache.maven.artifact.repository.metadata.SnapshotVersion in project indy by Commonjava.
the class MavenMetadataMerger method merge.
public Metadata merge(final Metadata master, final Metadata src, final Group group, final String path) {
if (src == null) {
return master;
}
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("Merge metadata, group: {}, path: {}, src: {}", group.getKey(), path, src);
// there is a lot of junk in here to make up for Metadata's anemic merge() method.
if (src.getGroupId() != null) {
master.setGroupId(src.getGroupId());
}
if (src.getArtifactId() != null) {
master.setArtifactId(src.getArtifactId());
}
if (src.getVersion() != null) {
master.setVersion(src.getVersion());
}
master.merge(src);
Versioning versioning = master.getVersioning();
Versioning srcVersioning = src.getVersioning();
if (srcVersioning != null && srcVersioning.getSnapshot() != null) {
logger.trace("INCLUDING snapshot information from: {} in: {}:{}", src, group.getKey(), path);
versioning.setSnapshot(srcVersioning.getSnapshot());
final List<SnapshotVersion> snapshotVersions = versioning.getSnapshotVersions();
boolean added = false;
for (final SnapshotVersion snap : srcVersioning.getSnapshotVersions()) {
if (!snapshotVersions.contains(snap)) {
snapshotVersions.add(snap);
added = true;
}
}
if (added) {
snapshotVersions.sort(new SnapshotVersionComparator());
}
} else {
logger.warn("SKIPPING snapshot information from: {} in: {}:{})", src, group.getKey(), path);
}
return master;
}
use of org.apache.maven.artifact.repository.metadata.SnapshotVersion in project indy by Commonjava.
the class MavenMetadataMerger method merge.
@Override
public byte[] merge(final Collection<Transfer> sources, final Group group, final String path) {
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("Generating merged metadata in: {}:{}", group.getKey(), path);
final Metadata master = new Metadata();
master.setVersioning(new Versioning());
final MetadataXpp3Reader reader = new MetadataXpp3Reader();
final FileReader fr = null;
InputStream stream = null;
boolean merged = false;
Transfer snapshotProvider = null;
for (final Transfer src : sources) {
if (!src.exists()) {
continue;
}
try {
stream = src.openInputStream();
String content = IOUtils.toString(stream);
logger.debug("Adding in metadata content from: {}\n\n{}\n\n", src, content);
// there is a lot of junk in here to make up for Metadata's anemic merge() method.
final Metadata md = reader.read(new StringReader(content), false);
if (md.getGroupId() != null) {
master.setGroupId(md.getGroupId());
}
if (md.getArtifactId() != null) {
master.setArtifactId(md.getArtifactId());
}
if (md.getVersion() != null) {
master.setVersion(md.getVersion());
}
master.merge(md);
Versioning versioning = master.getVersioning();
Versioning mdVersioning = md.getVersioning();
// FIXME: Should we try to merge snapshot lists instead of using the first one we encounter??
if (versioning.getSnapshot() == null && mdVersioning != null) {
logger.info("INCLUDING snapshot information from: {} in: {}:{}", src, group.getKey(), path);
snapshotProvider = src;
versioning.setSnapshot(mdVersioning.getSnapshot());
final List<SnapshotVersion> snapshotVersions = versioning.getSnapshotVersions();
boolean added = false;
for (final SnapshotVersion snap : mdVersioning.getSnapshotVersions()) {
if (!snapshotVersions.contains(snap)) {
snapshotVersions.add(snap);
added = true;
}
}
if (added) {
Collections.sort(snapshotVersions, new SnapshotVersionComparator());
}
} else {
logger.warn("SKIPPING snapshot information from: {} in: {}:{} (obscured by: {})", src, group.getKey(), path, snapshotProvider);
}
merged = true;
} catch (final IOException e) {
final StoreKey key = getKey(src);
logger.error(String.format("Cannot read metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
} catch (final XmlPullParserException e) {
final StoreKey key = getKey(src);
logger.error(String.format("Cannot parse metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
} finally {
closeQuietly(fr);
closeQuietly(stream);
}
}
Versioning versioning = master.getVersioning();
if (versioning != null && versioning.getVersions() != null) {
if (metadataProviders != null) {
for (MavenMetadataProvider provider : metadataProviders) {
try {
Metadata toMerge = provider.getMetadata(group.getKey(), path);
if (toMerge != null) {
merged = master.merge(toMerge) || merged;
}
} catch (IndyWorkflowException e) {
logger.error(String.format("Cannot read metadata: %s from metadata provider: %s. Reason: %s", path, provider.getClass().getSimpleName(), e.getMessage()), e);
}
}
}
List<SingleVersion> versionObjects = versioning.getVersions().stream().map(VersionUtils::createSingleVersion).collect(Collectors.toList());
Collections.sort(versionObjects);
versioning.setVersions(versionObjects.stream().map(SingleVersion::renderStandard).collect(Collectors.toList()));
if (versionObjects.size() > 0) {
String latest = versionObjects.get(versionObjects.size() - 1).renderStandard();
versioning.setLatest(latest);
versioning.setRelease(latest);
}
}
if (merged) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
new MetadataXpp3Writer().write(baos, master);
return baos.toByteArray();
} catch (final IOException e) {
logger.error(String.format("Cannot write consolidated metadata: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
}
}
return null;
}
use of org.apache.maven.artifact.repository.metadata.SnapshotVersion in project fabric8 by jboss-fuse.
the class AetherResolutionSupport method mvnDeploy.
/**
* <p>Simulation of <code>mvn deploy</code> in remote (even if <code>file://</code>-based) repository</p>
* <p>Like <code>mvn clean deploy -DskipTests -Dmaven.repo.local=_repo -DaltDeploymentRepository=id::default::file:_remote-repo</code></p>
* @param localRepository repository where the artifact is first <code>mvn install</code>ed
* @param remoteRepository repository where the artifact is <code>mvn deploy</code>ed fom <code>localRepository</code>
* @param groupId
* @param artifactId
* @param version
* @param timestamp
* @param content
* @throws IOException
*/
protected void mvnDeploy(File localRepository, File remoteRepository, String groupId, String artifactId, String version, Date timestamp, String content) throws IOException {
mvnInstall(localRepository, groupId, artifactId, version, timestamp, content);
Metadata gamd = getOrCreateMetadata(remoteRepository, groupId, artifactId);
Metadata gavmd = getOrCreateMetadata(remoteRepository, groupId, artifactId, version);
File from = new File(localRepository, String.format("%s/%s/%s", groupId.replaceAll("\\.", "/"), artifactId, version));
File location = new File(remoteRepository, String.format("%s/%s/%s", groupId.replaceAll("\\.", "/"), artifactId, version));
location.mkdirs();
String transformedVersion = version;
if (!gamd.getVersioning().getVersions().contains(version)) {
gamd.getVersioning().addVersion(version);
}
if (!version.endsWith("-SNAPSHOT")) {
} else {
if (gavmd.getVersioning().getSnapshot() == null) {
gavmd.getVersioning().setSnapshot(new Snapshot());
gavmd.getVersioning().getSnapshot().setBuildNumber(1);
} else {
gavmd.getVersioning().getSnapshot().setBuildNumber(gavmd.getVersioning().getSnapshot().getBuildNumber() + 1);
}
transformedVersion = version.replaceFirst("SNAPSHOT$", HM2.format(timestamp) + "-" + gavmd.getVersioning().getSnapshot().getBuildNumber());
gavmd.getVersioning().getSnapshot().setTimestamp(HM2.format(timestamp));
gavmd.getVersioning().setLastUpdatedTimestamp(timestamp);
gavmd.getVersioning().getSnapshotVersions().clear();
SnapshotVersion sv1 = new SnapshotVersion();
sv1.setUpdated(gavmd.getVersioning().getLastUpdated());
sv1.setVersion(transformedVersion);
sv1.setExtension("jar");
gavmd.getVersioning().getSnapshotVersions().add(sv1);
SnapshotVersion sv2 = new SnapshotVersion();
sv2.setUpdated(gavmd.getVersioning().getLastUpdated());
sv2.setVersion(transformedVersion);
sv2.setExtension("pom");
gavmd.getVersioning().getSnapshotVersions().add(sv2);
gavmd.getVersioning().setLastUpdatedTimestamp(timestamp);
File gavmdFile = new File(location, "maven-metadata.xml");
new MetadataXpp3Writer().write(new FileWriter(gavmdFile), gavmd);
gavmdFile.setLastModified(timestamp.getTime());
FileUtils.copyFile(new File(location, "maven-metadata.xml"), new File(from, String.format("maven-metadata-%s.xml", remoteRepository.getName())));
}
String jar = String.format("%s-%s.jar", artifactId, version);
String pom = String.format("%s-%s.pom", artifactId, version);
String rjar = String.format("%s-%s.jar", artifactId, transformedVersion);
String rpom = String.format("%s-%s.pom", artifactId, transformedVersion);
FileUtils.copyFile(new File(from, jar), new File(location, rjar));
FileUtils.copyFile(new File(from, pom), new File(location, rpom));
gamd.getVersioning().setLastUpdatedTimestamp(timestamp);
File gamdFile = new File(location.getParentFile(), "maven-metadata.xml");
new MetadataXpp3Writer().write(new FileWriter(gamdFile), gamd);
gamdFile.setLastModified(timestamp.getTime());
FileUtils.copyFile(new File(location.getParentFile(), "maven-metadata.xml"), new File(from.getParentFile(), String.format("maven-metadata-%s.xml", remoteRepository.getName())));
}
use of org.apache.maven.artifact.repository.metadata.SnapshotVersion in project gradle by gradle.
the class MavenLocalPublisher method createSnapshotMetadata.
@Override
protected Metadata createSnapshotMetadata(MavenNormalizedPublication publication, String groupId, String artifactId, String version, ExternalResourceRepository repository, ExternalResourceName metadataResource) {
Metadata metadata = new Metadata();
metadata.setModelVersion("1.1.0");
metadata.setGroupId(groupId);
metadata.setArtifactId(artifactId);
metadata.setVersion(version);
Snapshot snapshot = new Snapshot();
snapshot.setLocalCopy(true);
Versioning versioning = new Versioning();
versioning.updateTimestamp();
versioning.setSnapshot(snapshot);
for (MavenArtifact artifact : publication.getAllArtifacts()) {
SnapshotVersion sv = new SnapshotVersion();
sv.setClassifier(artifact.getClassifier());
sv.setExtension(artifact.getExtension());
sv.setVersion(version);
sv.setUpdated(versioning.getLastUpdated());
versioning.getSnapshotVersions().add(sv);
}
metadata.setVersioning(versioning);
return metadata;
}
use of org.apache.maven.artifact.repository.metadata.SnapshotVersion in project indy by Commonjava.
the class MavenMetadataGenerator method generateGroupMetadata.
/**
* Generate group related files (e.g maven-metadata.xml) from three levels.
* 1. cache, which means all the generation of the files will be cached. In terms of cache clearing, see #{@link MetadataMergeListener}
* 2. read cached from member hosted repos and try to download from member remote repos
* 3. generate by member hosted repos (list dir trying to find version directories)
*
* @param group
* @param members concrete store in group
* @param path
*/
private Metadata generateGroupMetadata(final Group group, final List<ArtifactStore> members, final List<StoreKey> contributingMembers, final String path) throws IndyWorkflowException {
if (!canProcess(path)) {
logger.error("The path is not a metadata file: {} ", path);
return null;
}
String toMergePath = path;
if (!path.endsWith(MavenMetadataMerger.METADATA_NAME)) {
toMergePath = normalize(normalize(parentPath(toMergePath)), MavenMetadataMerger.METADATA_NAME);
}
Metadata meta = getMetaFromCache(group.getKey(), toMergePath);
if (meta != null) {
return meta;
}
Metadata master = new Metadata();
master.setVersioning(new Versioning());
MetadataIncrementalResult incrementalResult = new MetadataIncrementalResult(new HashSet<>(members), Collections.emptySet(), master);
incrementalResult = mergeMissing(group, incrementalResult, toMergePath, "cached", this::retrieveCached);
contributingMembers.addAll(incrementalResult.merged);
incrementalResult = mergeMissing(group, incrementalResult, toMergePath, "downloaded", this::downloadMissing);
contributingMembers.addAll(incrementalResult.merged);
incrementalResult = mergeMissing(group, incrementalResult, toMergePath, "generated", this::generateMissing);
contributingMembers.addAll(incrementalResult.merged);
if (metadataProviders != null) {
master = mergeProviderMetadata(group, incrementalResult.result, toMergePath);
} else {
master = incrementalResult.result;
}
if (!incrementalResult.missing.isEmpty()) {
logger.warn("After download and generation attempts, metadata is still missing from the following stores: {}, size: {}", incrementalResult.missing, incrementalResult.missing.size());
}
Versioning versioning = master.getVersioning();
List<String> versions = versioning.getVersions();
logger.debug("Get versioning, versions: {}, release: {}, latest: {}", versions, versioning.getRelease(), versioning.getLatest());
if (versions != null && !versions.isEmpty()) {
merger.sortVersions(master);
return master;
}
List<SnapshotVersion> snapshotVersions = versioning.getSnapshotVersions();
if (snapshotVersions != null && !snapshotVersions.isEmpty()) {
if (logger.isTraceEnabled()) {
snapshotVersions.forEach(snapshotVersion -> logger.trace("snapshotVersion: {}", snapshotVersion.getVersion()));
}
return master;
}
List<Plugin> plugins = master.getPlugins();
if (plugins != null && !plugins.isEmpty()) {
return master;
}
logger.info("The group metadata generation is not successful for path: {} in group: {}", path, group);
return null;
}
Aggregations