|
1 |
| -package org.tudo.sse.utils;public class GAUtils { |
| 1 | +package org.tudo.sse.utils; |
| 2 | + |
| 3 | +import org.apache.logging.log4j.LogManager; |
| 4 | +import org.apache.logging.log4j.Logger; |
| 5 | +import org.apache.maven.artifact.repository.metadata.Metadata; |
| 6 | +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; |
| 7 | +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 8 | +import org.tudo.sse.Main; |
| 9 | +import org.tudo.sse.model.Artifact; |
| 10 | +import org.tudo.sse.model.ArtifactIdent; |
| 11 | +import org.tudo.sse.resolution.FileNotFoundException; |
| 12 | +import org.tudo.sse.resolution.PomResolutionException; |
| 13 | +import org.tudo.sse.resolution.PomResolver; |
| 14 | + |
| 15 | +import java.io.BufferedReader; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.io.InputStreamReader; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +public class GAUtils { |
| 24 | + private static final MavenCentralRepository MavenRepo = MavenCentralRepository.getInstance(); |
| 25 | + public static final Logger log = LogManager.getLogger(GAUtils.class); |
| 26 | + |
| 27 | + public static List<Artifact> retrieveAllVersions(String groupId, String artifactId) { |
| 28 | + try { |
| 29 | + PomResolver pomResolver = new PomResolver(false); |
| 30 | + List<Artifact> toReturn = new ArrayList<>(); |
| 31 | + Metadata meta = getVersions(groupId, artifactId); |
| 32 | + |
| 33 | + if(meta.getVersioning() == null) { |
| 34 | + return toReturn; |
| 35 | + } |
| 36 | + |
| 37 | + List<String> versions = meta.getVersioning().getVersions(); |
| 38 | + |
| 39 | + for(String version : versions) { |
| 40 | + try { |
| 41 | + toReturn.add(pomResolver.resolveArtifact(new ArtifactIdent(groupId, artifactId, version))); |
| 42 | + } catch (PomResolutionException | FileNotFoundException e) { |
| 43 | + log.error(e); |
| 44 | + } |
| 45 | + } |
| 46 | + return toReturn; |
| 47 | + } catch (FileNotFoundException | IOException | XmlPullParserException e) { |
| 48 | + throw new RuntimeException(e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static Artifact getLastModifiedVersion(String groupId, String artifactId) throws PomResolutionException { |
| 53 | + try { |
| 54 | + Metadata meta = getVersions(groupId, artifactId); |
| 55 | + |
| 56 | + if(meta.getVersioning() == null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + String latestVersion; |
| 61 | + if(meta.getVersioning().getRelease() != null) { |
| 62 | + latestVersion = meta.getVersioning().getRelease(); |
| 63 | + } else { |
| 64 | + long latestLastModified = 0; |
| 65 | + List<String> versions = meta.getVersioning().getVersions(); |
| 66 | + latestVersion = versions.get(0); |
| 67 | + for(String version : versions) { |
| 68 | + long currentLastModified = MavenRepo.getLastModified(new ArtifactIdent(groupId, artifactId, version)); |
| 69 | + if(currentLastModified > latestLastModified) { |
| 70 | + latestLastModified = currentLastModified; |
| 71 | + latestVersion = version; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + PomResolver pomResolver = new PomResolver(false); |
| 77 | + Artifact toReturn = pomResolver.resolveArtifact(new ArtifactIdent(groupId, artifactId, latestVersion)); |
| 78 | + return toReturn; |
| 79 | + } catch (FileNotFoundException | IOException | XmlPullParserException e) { |
| 80 | + throw new RuntimeException(e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static Artifact getHighestVersion(String groupId, String artifactId) throws PomResolutionException { |
| 85 | + try { |
| 86 | + Metadata meta = getVersions(groupId, artifactId); |
| 87 | + |
| 88 | + if(meta.getVersioning() == null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + String highestVersion; |
| 93 | + if(meta.getVersioning().getRelease() != null) { |
| 94 | + highestVersion = meta.getVersioning().getLatest(); |
| 95 | + } else { |
| 96 | + List<String> versions = meta.getVersioning().getVersions(); |
| 97 | + highestVersion = versions.get(versions.size() - 1); |
| 98 | + } |
| 99 | + |
| 100 | + PomResolver pomResolver = new PomResolver(false); |
| 101 | + Artifact toReturn = pomResolver.resolveArtifact(new ArtifactIdent(groupId, artifactId, highestVersion)); |
| 102 | + return toReturn; |
| 103 | + } catch (FileNotFoundException | IOException | XmlPullParserException e) { |
| 104 | + throw new RuntimeException(e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static Metadata getVersions(String groupId, String artifactId) throws FileNotFoundException, IOException, XmlPullParserException { |
| 109 | + InputStream versionings = MavenRepo.openXMLFileInputStream(new ArtifactIdent(groupId, artifactId, null)); |
| 110 | + MetadataXpp3Reader reader = new MetadataXpp3Reader(); |
| 111 | + return reader.read(new BufferedReader(new InputStreamReader(Objects.requireNonNull(versionings)))); |
| 112 | + } |
| 113 | + |
2 | 114 | }
|
0 commit comments