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.stream.Collectors; 022 023/// Converts between maven models and aether dependencies. 024/// 025public final class DependencyConverter { 026 027 private DependencyConverter() { 028 // utility class 029 } 030 031 /// Convert an aether dependency to a maven model dependency. 032 /// 033 /// @param aetherDep the aether dependency 034 /// @return the maven model dependency 035 /// 036 public static org.apache.maven.model.Dependency 037 convert(org.eclipse.aether.graph.Dependency aetherDep) { 038 org.eclipse.aether.artifact.Artifact artifact = aetherDep.getArtifact(); 039 040 // Create base model 041 org.apache.maven.model.Dependency modelDependency 042 = new org.apache.maven.model.Dependency(); 043 modelDependency.setGroupId(artifact.getGroupId()); 044 modelDependency.setArtifactId(artifact.getArtifactId()); 045 modelDependency.setVersion(artifact.getVersion()); 046 modelDependency.setScope(aetherDep.getScope()); 047 modelDependency.setOptional(aetherDep.isOptional()); 048 049 // Get type from extension 050 modelDependency.setType(artifact.getExtension() != null 051 && !artifact.getExtension().isEmpty() ? artifact.getExtension() 052 : "jar"); 053 054 // Optionally copy classifier 055 if (artifact.getClassifier() != null 056 && !artifact.getClassifier().isEmpty()) { 057 modelDependency.setClassifier(artifact.getClassifier()); 058 } 059 060 // Copy exclusions 061 if (!aetherDep.getExclusions().isEmpty()) { 062 modelDependency.setExclusions( 063 aetherDep.getExclusions().stream() 064 .map(e -> { 065 org.apache.maven.model.Exclusion exclusion 066 = new org.apache.maven.model.Exclusion(); 067 exclusion.setGroupId(e.getGroupId()); 068 exclusion.setArtifactId(e.getArtifactId()); 069 return exclusion; 070 }) 071 .collect(Collectors.toList())); 072 } 073 return modelDependency; 074 } 075 076 /// Convert a maven model dependency to an aether dependency. 077 /// 078 /// @param modelDep the maven model dependency 079 /// @return the aether dependency 080 /// 081 public static org.eclipse.aether.graph.Dependency 082 convert(org.apache.maven.model.Dependency modelDep) { 083 // Get type from extension 084 String extension 085 = modelDep.getType() != null && !modelDep.getType().isEmpty() 086 ? modelDep.getType() 087 : "jar"; 088 089 // Create aether base artifact 090 org.eclipse.aether.artifact.Artifact artifact 091 = new org.eclipse.aether.artifact.DefaultArtifact( 092 modelDep.getGroupId(), modelDep.getArtifactId(), 093 modelDep.getClassifier(), extension, modelDep.getVersion()); 094 095 // Create aether dependency 096 return new org.eclipse.aether.graph.Dependency( 097 artifact, modelDep.getScope(), modelDep.isOptional(), 098 modelDep.getExclusions().stream() 099 .map(x -> new org.eclipse.aether.graph.Exclusion( 100 x.getGroupId(), x.getArtifactId(), "*", "*")) 101 .collect(Collectors.toList())); 102 } 103 104 /// Convert a maven model dependency to an aether dependency. 105 /// 106 /// @param mvnResource the maven resource 107 /// @param scope the scope of aether dependency 108 /// @return the aether dependency 109 /// 110 public static org.apache.maven.model.Dependency 111 convert(MvnRepoDependency mvnResource, String scope) { 112 var dep = new org.apache.maven.model.Dependency(); 113 dep.setGroupId(mvnResource.groupId()); 114 dep.setArtifactId(mvnResource.artifactId()); 115 dep.setType( 116 mvnResource.mvnType().isBlank() ? "jar" : mvnResource.mvnType()); 117 if (!mvnResource.classifier().isBlank()) { 118 dep.setClassifier(mvnResource.classifier()); 119 } 120 dep.setVersion(mvnResource.version()); 121 dep.setScope(scope); 122 return dep; 123 } 124 125}