com.marvelution.utils.maven.model.DependencyUtils Maven / Gradle / Ivy
/*
* Copyright 2008 Marvelution.com
*
* 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.
*/
package com.marvelution.utils.maven.model;
import org.apache.maven.model.Dependency;
import org.codehaus.plexus.util.StringUtils;
/**
* Maven {@link Dependency} utility class
*
* @author Mark Rekveld
*/
public final class DependencyUtils {
/**
* Create a version-less key
*
* @param dependency the Maven Model {@link Dependency}
* @return the version-less key
*/
public static String versionlessKey(Dependency dependency) {
return versionlessKey(dependency.getGroupId(), dependency.getArtifactId(), dependency.getType(), dependency
.getClassifier());
}
/**
* Create a version-less key
*
* @param groupId the groupId of the dependency
* @param artifactId the artifactId of the dependency
* @param type the type of the dependency
* @return the version-less key
*/
public static String versionlessKey(String groupId, String artifactId, String type) {
return versionlessKey(groupId, artifactId, type, null);
}
/**
* Create a version-less key
*
* @param groupId the groupId of the dependency
* @param artifactId the artifactId of the dependency
* @param type the type of the dependency
* @param classifier the classifier of the dependency
* @return the version-less key
*/
public static String versionlessKey(String groupId, String artifactId, String type, String classifier) {
if (groupId == null) {
throw new NullPointerException("groupId is null");
}
if (artifactId == null) {
throw new NullPointerException("artifactId is null");
}
String key = groupId + ":" + artifactId + ":";
if (type != null) {
key += type;
} else {
key += "jar";
}
if (classifier != null) {
key += ":" + classifier;
}
return key;
}
/**
* Create a {@link Dependency} from a Dependency version-less key
*
* @param versionlessKey the version-less key of a dependency
* @return the {@link Dependency}
*/
public static Dependency versionlessKeyToDependency(String versionlessKey) {
if (versionlessKey == null) {
throw new NullPointerException("versionlessKey is null");
}
final String[] elements = versionlessKey.split(":");
if (elements.length < 3) {
throw new IllegalArgumentException("versionlessKey doesn't contain a groupId, artifactId or type");
}
if (elements.length > 4) {
throw new IllegalArgumentException(
"versionlessKey doesn't comply to format groupId:artifactId:type:classifier");
}
final Dependency dependency = new Dependency();
dependency.setGroupId(elements[0]);
dependency.setArtifactId(elements[1]);
dependency.setType(elements[2]);
if (elements.length == 4) {
dependency.setClassifier(elements[3]);
}
return dependency;
}
/**
* Compare two Maven Model Dependencyies
*
* @param toBeCompared Maven Model {@link Dependency} to compare
* @param compareTo Maven Model {@link Dependency} to compare to
* @return true if equal, otherwise false
*/
public static boolean equals(Dependency toBeCompared, Dependency compareTo) {
return (toBeCompared.toString().equals(compareTo.toString()) && toBeCompared.getClassifier().equals(
compareTo.getClassifier()));
}
/**
* Check if the {@link Dependency} contains at least the required settings:
*
* - groupId
* - artifactId
* - version
*
*
* @param dependency the {@link Dependency} to check
* @return true if groupId, artifactId and version are set and not empty,
* false otherwise
*/
public static boolean isValidDependecy(Dependency dependency) {
return (dependency != null && !StringUtils.isEmpty(dependency.getGroupId())
&& !StringUtils.isEmpty(dependency.getArtifactId()) && !StringUtils.isEmpty(dependency.getVersion()));
}
/**
* Converts a {@link Dependency} into a {@link String}
*
* @param dependency the {@link Dependency} to convert
* @return the resulting {@link String}
*/
public static String dependencyToString(Dependency dependency) {
return dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getVersion()
+ (!StringUtils.isEmpty(dependency.getType()) ? ":" + dependency.getType() : "")
+ (!StringUtils.isEmpty(dependency.getClassifier()) ? ":" + dependency.getClassifier() : "");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy