Add classifier to extra info

This commit is contained in:
Mark Kittisopikul 2024-02-03 01:58:39 -05:00
parent 503aafc594
commit a0109bab80
6 changed files with 205 additions and 37 deletions

View File

@ -0,0 +1,118 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
*/
package org.apache.ivy.plugins.parser.m2;
import org.apache.ivy.core.module.id.ModuleId;
public class ClassifiedModuleId extends ModuleId {
static final String ENCODE_SEPARATOR = ":#@#:";
public static final String CLASSIFIER_KEY = "classifier";
public static final String DEFAULT_CLASSIFIER = "defaultclassifier";
private String classifier;
private int hash;
public static ClassifiedModuleId newInstance(String org, String name, String classifier) {
return (ClassifiedModuleId) intern(new ClassifiedModuleId(org, name, classifier));
}
public ClassifiedModuleId(String organisation, String name, String classifier) {
super(organisation, name);
if (classifier == null) {
this.classifier = DEFAULT_CLASSIFIER;
} else {
this.classifier = classifier;
}
getAttributes().put(CLASSIFIER_KEY, classifier);
}
public String getClassifier() {
return classifier;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ClassifiedModuleId)) {
return false;
}
ClassifiedModuleId other = (ClassifiedModuleId) obj;
String organisation = getOrganisation();
String name = getName();
return (organisation == null) ? organisation == null && other.getName().equals(name)
: other.getOrganisation().equals(organisation) && other.getName().equals(name)
&& other.getClassifier().equals(classifier);
}
@Override
public int hashCode() {
if (hash == 0) {
// CheckStyle:MagicNumber| OFF
hash = super.hashCode();
hash = hash * 13 + classifier.hashCode();
// CheckStyle:MagicNumber| ON
}
return hash;
}
@Override
public String toString() {
return super.encodeToString() + "#" + classifier;
}
public int compareTo(ClassifiedModuleId that) {
int result = super.compareTo(that);
if (result == 0) {
result = classifier.compareTo(that.classifier);
}
return result;
}
/**
* Returns the encoded String representing this ModuleId.
*
* @return The ModuleId encoded as String.
*/
public String encodeToString() {
return getOrganisation() + ENCODE_SEPARATOR + getName() + ENCODE_SEPARATOR
+ getClassifier();
}
/**
* Returns a ModuleId
*
* @param encoded
* String
* @return The new ClassifiedModuleId.
* @throws IllegalArgumentException
* If the given String could not be decoded.
*/
public static ClassifiedModuleId decode(String encoded) {
String[] parts = encoded.split(ENCODE_SEPARATOR);
if (parts.length != 3) {
throw new IllegalArgumentException("badly encoded module id: '" + encoded + "'");
}
return new ClassifiedModuleId(parts[0], parts[1], parts[2]);
}
}

View File

@ -30,6 +30,8 @@ public class DefaultPomDependencyMgt implements PomDependencyMgt {
private String scope;
private String classifier;
private List<ModuleId> excludedModules;
public DefaultPomDependencyMgt(String groupId, String artifactId, String version, String scope,
@ -39,6 +41,17 @@ public class DefaultPomDependencyMgt implements PomDependencyMgt {
this.version = version;
this.scope = scope;
this.excludedModules = excludedModules;
this.classifier = "defaultclassifier";
}
public DefaultPomDependencyMgt(String groupId, String artifactId, String version, String scope,
String classifier, List<ModuleId> excludedModules) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.scope = scope;
this.excludedModules = excludedModules;
this.classifier = classifier;
}
public String getScope() {
@ -57,6 +70,10 @@ public class DefaultPomDependencyMgt implements PomDependencyMgt {
return version;
}
public String getClassifier() {
return classifier;
}
public List<ModuleId> getExcludedModules() {
return excludedModules;
}

View File

@ -31,5 +31,9 @@ public interface PomDependencyMgt {
String getScope();
default String getClassifier() {
return "defaultclassifier";
}
List<ModuleId> getExcludedModules();
}

View File

@ -69,7 +69,7 @@ public class PomModuleDescriptorBuilder {
*/
private static final String IVY_XML_MAVEN_NAMESPACE_URI = "http://ant.apache.org/ivy/maven";
private static final int DEPENDENCY_MANAGEMENT_KEY_PARTS_COUNT = 4;
private static final int DEPENDENCY_MANAGEMENT_KEY_PARTS_COUNT = 5;
public static final Configuration[] MAVEN2_CONFIGURATIONS = new Configuration[] {
new Configuration("default", PUBLIC,
@ -299,7 +299,7 @@ public class PomModuleDescriptorBuilder {
List<ModuleId> excluded = dep.getExcludedModules();
if (excluded.isEmpty()) {
excluded = getDependencyMgtExclusions(ivyModuleDescriptor, dep.getGroupId(),
dep.getArtifactId());
dep.getArtifactId(), dep.getClassifier());
}
final boolean excludeAllTransitiveDeps = shouldExcludeAllTransitiveDeps(excluded);
// the same dependency mrid could appear twice in the module descriptor,
@ -414,16 +414,17 @@ public class PomModuleDescriptorBuilder {
public void addDependencyMgt(PomDependencyMgt dep) {
ivyModuleDescriptor.addDependencyManagement(dep);
String key = getDependencyMgtExtraInfoKeyForVersion(dep.getGroupId(), dep.getArtifactId());
String key = getDependencyMgtExtraInfoKeyForVersion(dep.getGroupId(), dep.getArtifactId(),
dep.getClassifier());
overwriteExtraInfoIfExists(key, dep.getVersion());
if (dep.getScope() != null) {
String scopeKey = getDependencyMgtExtraInfoKeyForScope(dep.getGroupId(),
dep.getArtifactId());
dep.getArtifactId(), dep.getClassifier());
overwriteExtraInfoIfExists(scopeKey, dep.getScope());
}
if (!dep.getExcludedModules().isEmpty()) {
String exclusionPrefix = getDependencyMgtExtraInfoPrefixForExclusion(dep.getGroupId(),
dep.getArtifactId());
dep.getArtifactId(), dep.getClassifier());
int index = 0;
for (ModuleId excludedModule : dep.getExcludedModules()) {
overwriteExtraInfoIfExists(
@ -502,27 +503,35 @@ public class PomModuleDescriptorBuilder {
return null;
}
public String getClassifier() {
return "defaultclassifier";
}
public List<ModuleId> getExcludedModules() {
return Collections.emptyList(); // probably not used?
}
}
private String getDefaultVersion(PomDependencyData dep) {
ModuleId moduleId = ModuleId.newInstance(dep.getGroupId(), dep.getArtifactId());
ClassifiedModuleId moduleId = ClassifiedModuleId.newInstance(dep.getGroupId(),
dep.getArtifactId(), dep.getClassifier());
if (ivyModuleDescriptor.getDependencyManagementMap().containsKey(moduleId)) {
return ivyModuleDescriptor.getDependencyManagementMap().get(moduleId).getVersion();
}
String key = getDependencyMgtExtraInfoKeyForVersion(dep.getGroupId(), dep.getArtifactId());
String key = getDependencyMgtExtraInfoKeyForVersion(dep.getGroupId(), dep.getArtifactId(),
dep.getClassifier());
return ivyModuleDescriptor.getExtraInfoContentByTagName(key);
}
private String getDefaultScope(PomDependencyData dep) {
String result;
ModuleId moduleId = ModuleId.newInstance(dep.getGroupId(), dep.getArtifactId());
ClassifiedModuleId moduleId = ClassifiedModuleId.newInstance(dep.getGroupId(),
dep.getArtifactId(), dep.getClassifier());
if (ivyModuleDescriptor.getDependencyManagementMap().containsKey(moduleId)) {
result = ivyModuleDescriptor.getDependencyManagementMap().get(moduleId).getScope();
} else {
String key = getDependencyMgtExtraInfoKeyForScope(dep.getGroupId(), dep.getArtifactId());
String key = getDependencyMgtExtraInfoKeyForScope(dep.getGroupId(), dep.getArtifactId(),
dep.getClassifier());
result = ivyModuleDescriptor.getExtraInfoContentByTagName(key);
}
if (result == null || !MAVEN2_CONF_MAPPING.containsKey(result)) {
@ -531,14 +540,17 @@ public class PomModuleDescriptorBuilder {
return result;
}
private static String getDependencyMgtExtraInfoKeyForVersion(String groupId, String artifactId) {
private static String getDependencyMgtExtraInfoKeyForVersion(String groupId, String artifactId,
String classifierId) {
return DEPENDENCY_MANAGEMENT + EXTRA_INFO_DELIMITER + groupId + EXTRA_INFO_DELIMITER
+ artifactId + EXTRA_INFO_DELIMITER + "version";
+ artifactId + EXTRA_INFO_DELIMITER + classifierId + EXTRA_INFO_DELIMITER
+ "version";
}
private static String getDependencyMgtExtraInfoKeyForScope(String groupId, String artifactId) {
private static String getDependencyMgtExtraInfoKeyForScope(String groupId, String artifactId,
String classifierId) {
return DEPENDENCY_MANAGEMENT + EXTRA_INFO_DELIMITER + groupId + EXTRA_INFO_DELIMITER
+ artifactId + EXTRA_INFO_DELIMITER + "scope";
+ artifactId + EXTRA_INFO_DELIMITER + classifierId + EXTRA_INFO_DELIMITER + "scope";
}
private static String getPropertyExtraInfoKey(String propertyName) {
@ -546,21 +558,24 @@ public class PomModuleDescriptorBuilder {
}
private static String getDependencyMgtExtraInfoPrefixForExclusion(String groupId,
String artifactId) {
String artifactId, String classifierId) {
return DEPENDENCY_MANAGEMENT + EXTRA_INFO_DELIMITER + groupId + EXTRA_INFO_DELIMITER
+ artifactId + EXTRA_INFO_DELIMITER + "exclusion_";
+ artifactId + EXTRA_INFO_DELIMITER + classifierId + EXTRA_INFO_DELIMITER
+ "exclusion_";
}
private static List<ModuleId> getDependencyMgtExclusions(ModuleDescriptor descriptor,
String groupId, String artifactId) {
String groupId, String artifactId, String classifierId) {
if (descriptor instanceof PomModuleDescriptor) {
PomDependencyMgt dependencyMgt = ((PomModuleDescriptor) descriptor)
.getDependencyManagementMap().get(ModuleId.newInstance(groupId, artifactId));
.getDependencyManagementMap()
.get(ClassifiedModuleId.newInstance(groupId, artifactId, classifierId));
if (dependencyMgt != null) {
return dependencyMgt.getExcludedModules();
}
}
String exclusionPrefix = getDependencyMgtExtraInfoPrefixForExclusion(groupId, artifactId);
String exclusionPrefix = getDependencyMgtExtraInfoPrefixForExclusion(groupId, artifactId,
classifierId);
List<ModuleId> exclusionIds = new LinkedList<>();
for (ExtraInfoHolder extraInfoHolder : descriptor.getExtraInfos()) {
String key = extraInfoHolder.getName();
@ -578,10 +593,10 @@ public class PomModuleDescriptorBuilder {
return exclusionIds;
}
public static Map<ModuleId, String> getDependencyManagementMap(ModuleDescriptor md) {
Map<ModuleId, String> ret = new LinkedHashMap<>();
public static Map<ClassifiedModuleId, String> getDependencyManagementMap(ModuleDescriptor md) {
Map<ClassifiedModuleId, String> ret = new LinkedHashMap<>();
if (md instanceof PomModuleDescriptor) {
for (Map.Entry<ModuleId, PomDependencyMgt> e : ((PomModuleDescriptor) md)
for (Map.Entry<ClassifiedModuleId, PomDependencyMgt> e : ((PomModuleDescriptor) md)
.getDependencyManagementMap().entrySet()) {
PomDependencyMgt dependencyMgt = e.getValue();
ret.put(e.getKey(), dependencyMgt.getVersion());
@ -595,7 +610,7 @@ public class PomModuleDescriptorBuilder {
Message.warn("what seem to be a dependency management extra info "
+ "doesn't match expected pattern: " + key);
} else {
ret.put(ModuleId.newInstance(parts[1], parts[2]),
ret.put(ClassifiedModuleId.newInstance(parts[1], parts[2], parts[3]),
extraInfoHolder.getContent());
}
}
@ -619,16 +634,17 @@ public class PomModuleDescriptorBuilder {
+ "doesn't match expected pattern: " + key);
} else {
String versionKey = DEPENDENCY_MANAGEMENT + EXTRA_INFO_DELIMITER + parts[1]
+ EXTRA_INFO_DELIMITER + parts[2] + EXTRA_INFO_DELIMITER
+ "version";
+ EXTRA_INFO_DELIMITER + parts[2] + EXTRA_INFO_DELIMITER + parts[3]
+ EXTRA_INFO_DELIMITER + "version";
String scopeKey = DEPENDENCY_MANAGEMENT + EXTRA_INFO_DELIMITER + parts[1]
+ EXTRA_INFO_DELIMITER + parts[2] + EXTRA_INFO_DELIMITER + "scope";
+ EXTRA_INFO_DELIMITER + parts[2] + EXTRA_INFO_DELIMITER + parts[3]
+ EXTRA_INFO_DELIMITER + "scope";
String version = md.getExtraInfoContentByTagName(versionKey);
String scope = md.getExtraInfoContentByTagName(scopeKey);
List<ModuleId> exclusions = getDependencyMgtExclusions(md, parts[1],
parts[2]);
parts[2], parts[3]);
result.add(new DefaultPomDependencyMgt(parts[1], parts[2], version, scope,
exclusions));
}
@ -754,7 +770,8 @@ public class PomModuleDescriptorBuilder {
}
public static class PomModuleDescriptor extends DefaultModuleDescriptor {
private final Map<ModuleId, PomDependencyMgt> dependencyManagementMap = new LinkedHashMap<>();
private final Map<ClassifiedModuleId, PomDependencyMgt> dependencyManagementMap = new LinkedHashMap<>();
// dependency descriptor keyed by its dependency revision id
private final Map<ModuleRevisionId, DependencyDescriptor> depDescriptors = new HashMap<>();
@ -763,12 +780,11 @@ public class PomModuleDescriptorBuilder {
}
public void addDependencyManagement(PomDependencyMgt dependencyMgt) {
dependencyManagementMap.put(
ModuleId.newInstance(dependencyMgt.getGroupId(), dependencyMgt.getArtifactId()),
dependencyMgt);
dependencyManagementMap.put(ClassifiedModuleId.newInstance(dependencyMgt.getGroupId(),
dependencyMgt.getArtifactId(), dependencyMgt.getClassifier()), dependencyMgt);
}
public Map<ModuleId, PomDependencyMgt> getDependencyManagementMap() {
public Map<ClassifiedModuleId, PomDependencyMgt> getDependencyManagementMap() {
return dependencyManagementMap;
}

View File

@ -409,6 +409,14 @@ public class PomReader {
return replaceProps(val);
}
public String getClassifier() {
String val = getFirstChildText(depElement, CLASSIFIER);
if (val == null) {
val = "defaultclassifier";
}
return replaceProps(val);
}
public List<ModuleId> getExcludedModules() {
Element exclusionsElement = getFirstChildElement(depElement, EXCLUSIONS);
if (exclusionsElement == null) {
@ -491,6 +499,11 @@ public class PomReader {
return null; // not used
}
public String getClassifier() {
return "defaultclassifier";
}
public List<ModuleId> getExcludedModules() {
return Collections.emptyList(); // probably not used?
}

View File

@ -15,7 +15,7 @@
"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.
under the License.
-->
<ivy-module version="2.0" xmlns:m="http://ant.apache.org/ivy/maven">
<info organisation="org.apache"
@ -25,12 +25,12 @@
publication="20140429153143"
>
<description homepage="http://ivy.jayasoft.org/" />
<m:properties__commons.collections.version>1.0.5</m:properties__commons.collections.version>
<m:properties__commons.logging.version>1.0.4</m:properties__commons.logging.version>
<m:dependency.management__commons-logging__commons-logging__version>1.0.4</m:dependency.management__commons-logging__commons-logging__version>
<m:dependency.management__commons-collections__commons-collections__version>1.0.5</m:dependency.management__commons-collections__commons-collections__version>
<m:dependency.management__commons-collections__commons-collections__exclusion_0>javax.mail__mail</m:dependency.management__commons-collections__commons-collections__exclusion_0>
<m:dependency.management__commons-collections__commons-collections__exclusion_1>javax.jms__jms</m:dependency.management__commons-collections__commons-collections__exclusion_1>
<m:properties__commons.collections.version>1.0.5</m:properties__commons.collections.version>
<m:dependency.management__commons-logging__commons-logging__defaultclassifier__version>1.0.4</m:dependency.management__commons-logging__commons-logging__defaultclassifier__version>
<m:dependency.management__commons-collections__commons-collections__defaultclassifier__version>1.0.5</m:dependency.management__commons-collections__commons-collections__defaultclassifier__version>
<m:dependency.management__commons-collections__commons-collections__defaultclassifier__exclusion_0>javax.mail__mail</m:dependency.management__commons-collections__commons-collections__defaultclassifier__exclusion_0>
<m:dependency.management__commons-collections__commons-collections__defaultclassifier__exclusion_1>javax.jms__jms</m:dependency.management__commons-collections__commons-collections__defaultclassifier__exclusion_1>
</info>
<configurations>
<conf name="default" visibility="public" description="runtime dependencies and master artifact can be used with this conf" extends="runtime,master"/>