Skip to content

Commit

Permalink
Support SNAPSHOT versions by localRepo
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Oct 26, 2023
1 parent 125f549 commit d398f6e
Show file tree
Hide file tree
Showing 22 changed files with 640 additions and 149 deletions.
47 changes: 23 additions & 24 deletions mrm-api/src/main/java/org/codehaus/mojo/mrm/api/maven/Artifact.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

package org.codehaus.mojo.mrm.api.maven;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;

/**
* Represents a specific artifact in a Maven repository. Implements {@link Comparable} to sort based on
* {@link #getGroupId()} and then {@link #getName()}.
Expand Down Expand Up @@ -82,13 +86,6 @@ public final class Artifact implements Comparable<Artifact> {
*/
private final Integer buildNumber;

/**
* The lazy idempotent cache of the artifact's name.
*
* @since 1.0
*/
private String name;

/**
* The lazy idempotent cache of the artifact's timestamp version string (which will be equal to the {@link #version}
* for either a release version or a non-timestamped SNAPSHOT.
Expand Down Expand Up @@ -199,13 +196,17 @@ public Artifact(String groupId, String artifactId, String version, String type)
* @since 1.0
*/
public String getName() {
if (name == null) {
name = MessageFormat.format(
"{0}-{1}{2}.{3}",
new Object[] {artifactId, getTimestampVersion(), (classifier == null ? "" : "-" + classifier), type
});
}
return name;
return artifactId + "-" + getTimestampVersion() + (classifier == null ? "" : "-" + classifier) + "." + type;
}

/**
* Returns the name of the artifact.
*
* @return the name of the artifact.
* @since 1.0
*/
public String getBaseVersionName() {
return artifactId + "-" + version + (classifier == null ? "" : "-" + classifier) + "." + type;
}

/**
Expand Down Expand Up @@ -312,15 +313,13 @@ public String getTimestampVersion() {
if (timestampVersion == null) {
if (timestamp != null) {
assert isSnapshot();
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
timestampVersion = MessageFormat.format(
"{0}-{1}-{2}",
new Object[] {
this.version.substring(0, this.version.length() - "-SNAPSHOT".length()),
fmt.format(new Date(timestamp.longValue())),
buildNumber
});

DateTimeFormatter timestampFormatter =
DateTimeFormatter.ofPattern("yyyyMMdd.HHmmss").withZone(ZoneId.of("UTC"));

timestampVersion = StringUtils.removeEnd(version, "-SNAPSHOT")
+ "-" + timestampFormatter.format(Instant.ofEpochMilli(timestamp))
+ "-" + buildNumber;
} else {
timestampVersion = version;
}
Expand Down
6 changes: 3 additions & 3 deletions mrm-maven-plugin/src/it/hostedrepo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@
</goals>
<configuration>
<repositories>
<mockRepo>
<source>${project.build.directory}/mock-repo</source>
</mockRepo>
<localRepo>
<source>${project.basedir}/src/mrm-local-repo</source>
</localRepo>
<proxyRepo/>
</repositories>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.mojohaus.mrm.hostedrepo.its</groupId>
<artifactId>local-repo-snapshot-dep</artifactId>
<version>1.0.0</version>

<description>Test snapshot dependencies from local repo</description>

<dependencies>
<dependency>
<!-- artifact exist local repo with SNAPSHOT in name -->
<groupId>org.group1</groupId>
<artifactId>artifact1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<!-- artifact exist local repo with timestamps in name -->
<groupId>org.group1</groupId>
<artifactId>artifact2</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--
~ Copyright MojoHaus and Contributors
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.group1</groupId>
<artifactId>artifact2</artifactId>
<version>1.0.0-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--
~ Copyright MojoHaus and Contributors
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.group1</groupId>
<artifactId>artifact1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--
~ Copyright MojoHaus and Contributors
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.group1</groupId>
<artifactId>artifact1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.archetype.catalog.ArchetypeCatalog;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.Plugin;
import org.apache.maven.artifact.repository.metadata.Snapshot;
import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
import org.apache.maven.artifact.repository.metadata.Versioning;
import org.codehaus.mojo.mrm.api.maven.ArchetypeCatalogNotFoundException;
import org.codehaus.mojo.mrm.api.maven.Artifact;
import org.codehaus.mojo.mrm.api.maven.ArtifactNotFoundException;
Expand Down Expand Up @@ -152,92 +146,22 @@ public void set(Artifact artifact, InputStream content) throws IOException {

@Override
public Metadata getMetadata(String path) throws IOException, MetadataNotFoundException {
boolean found = false;
Metadata result = new Metadata();
Set<String> pluginArtifactIds = new HashSet<>();
Set<String> snapshotVersions = new HashSet<>();
Metadata result = null;

for (ArtifactStore store : stores) {
try {
Metadata partial = store.getMetadata(path);
if (StringUtils.isEmpty(result.getArtifactId()) && !StringUtils.isEmpty(partial.getArtifactId())) {
result.setArtifactId(partial.getArtifactId());
found = true;
}
if (StringUtils.isEmpty(result.getGroupId()) && !StringUtils.isEmpty(partial.getGroupId())) {
result.setGroupId(partial.getGroupId());
found = true;
}
if (StringUtils.isEmpty(result.getVersion()) && !StringUtils.isEmpty(partial.getVersion())) {
result.setVersion(partial.getVersion());
found = true;
}
if (partial.getPlugins() != null && !partial.getPlugins().isEmpty()) {
for (Plugin plugin : partial.getPlugins()) {
if (!pluginArtifactIds.contains(plugin.getArtifactId())) {
result.addPlugin(plugin);
pluginArtifactIds.add(plugin.getArtifactId());
}
}
found = true;
}
if (partial.getVersioning() != null) {
Versioning rVers = result.getVersioning();
if (rVers == null) {
rVers = new Versioning();
}
Versioning pVers = partial.getVersioning();
String rLU = found ? rVers.getLastUpdated() : null;
String pLU = pVers.getLastUpdated();
if (pLU != null && (rLU == null || rLU.compareTo(pLU) < 0)) {
// partial is newer or only
if (!StringUtils.isEmpty(pVers.getLatest())) {
rVers.setLatest(pVers.getLatest());
}

if (!StringUtils.isEmpty(pVers.getRelease())) {
rVers.setRelease(pVers.getRelease());
}
rVers.setLastUpdated(pVers.getLastUpdated());
}
for (String version : pVers.getVersions()) {
if (!rVers.getVersions().contains(version)) {
rVers.addVersion(version);
}
}
if (pVers.getSnapshot() != null) {
if (rVers.getSnapshot() == null
|| pVers.getSnapshot().getBuildNumber()
> rVers.getSnapshot().getBuildNumber()) {
Snapshot snapshot = new Snapshot();
snapshot.setBuildNumber(pVers.getSnapshot().getBuildNumber());
snapshot.setTimestamp(pVers.getSnapshot().getTimestamp());
rVers.setSnapshot(snapshot);
}
}
try {
if (pVers.getSnapshotVersions() != null
&& !pVers.getSnapshotVersions().isEmpty()) {
for (SnapshotVersion snapshotVersion : pVers.getSnapshotVersions()) {
String key = snapshotVersion.getVersion() + "-" + snapshotVersion.getClassifier() + "."
+ snapshotVersion.getExtension();
if (!snapshotVersions.contains(key)) {
rVers.addSnapshotVersion(snapshotVersion);
snapshotVersions.add(key);
}
}
}
} catch (NoSuchMethodError e) {
// Maven 2
}

result.setVersioning(rVers);
found = true;
Metadata metadata = store.getMetadata(path);
if (result == null) {
result = metadata;
} else {
result.merge(metadata);
}
} catch (MetadataNotFoundException e) {
// ignore
}
}
if (!found) {

if (result == null) {
throw new MetadataNotFoundException(path);
}
return result;
Expand Down
Loading

0 comments on commit d398f6e

Please sign in to comment.