001/* 002 * JDrupes Builder 003 * Copyright (C) 2026 Michael N. Lipp 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <https://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.builder.mvnrepo; 020 021import java.util.List; 022import org.eclipse.aether.artifact.DefaultArtifact; 023import org.eclipse.aether.repository.RemoteRepository; 024import org.eclipse.aether.resolution.VersionRangeRequest; 025import org.eclipse.aether.resolution.VersionRangeResolutionException; 026import org.eclipse.aether.resolution.VersionRangeResult; 027 028/// The Class MvnUtils. 029/// 030public final class MvnUtils { 031 032 private MvnUtils() { 033 } 034 035 /// Checks if the version is a range. 036 /// 037 /// @param version the version 038 /// @return true, if is version range 039 /// 040 /* default */ static boolean isVersionRange(String version) { 041 return version.startsWith("[") || version.startsWith("("); 042 } 043 044 /// Resolve a version range with the highest available 045 /// included version. 046 /// 047 /// @param repositories the repositories 048 /// @param groupId the group id 049 /// @param artifactId the artifact id 050 /// @param versionRange the version range 051 /// 052 /// @return the string 053 /// 054 /* default */ static String resolveVersionRange( 055 List<RemoteRepository> repositories, 056 String groupId, String artifactId, String versionRange) { 057 var versionRangeRequest = new VersionRangeRequest(); 058 versionRangeRequest.setArtifact(new DefaultArtifact(groupId, 059 artifactId, "", versionRange)); 060 versionRangeRequest.setRepositories(repositories); 061 try { 062 VersionRangeResult result = MavenContext.repositorySystem() 063 .resolveVersionRange(MavenContext.repositorySession(), 064 versionRangeRequest); 065 var resolved = result.getHighestVersion(); 066 if (resolved != null) { 067 return resolved.toString(); 068 } 069 } catch (VersionRangeResolutionException e) { // NOPMD 070 // Fall through and let artifact resolution fail with a message 071 } 072 return versionRange; 073 } 074 075}