001 /**
002 * Copyright 2010-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.common.util;
017
018 import java.io.File;
019
020 import org.apache.commons.io.FileUtils;
021 import org.apache.commons.lang3.StringUtils;
022 import org.kuali.common.util.nullify.NullUtils;
023
024 public class RepositoryUtils {
025
026 private static final String FS = File.separator;
027 private static final String DEFAULT_MAVEN_REPO_PATH = ".m2" + FS + "repository";
028
029 public static final void copyArtifact(String repository, Artifact artifact) {
030 File file = getFile(artifact);
031 copyArtifactToFile(repository, artifact, file);
032 }
033
034 public static final void copyArtifactToDirectory(String repository, Artifact artifact, File directory) {
035 String filename = getFilename(artifact);
036 File file = new File(directory, filename);
037 copyArtifactToFile(repository, artifact, file);
038 }
039
040 public static final void copyArtifactToFile(String repository, Artifact artifact, File file) {
041 String location = repository + getRepositoryPath(artifact);
042 LocationUtils.copyLocationToFile(location, file);
043 }
044
045 public static final String toString(Artifact artifact) {
046 StringBuilder sb = new StringBuilder();
047 sb.append(artifact.getGroupId());
048 sb.append(":");
049 sb.append(artifact.getArtifactId());
050 sb.append(":");
051 sb.append(artifact.getVersion());
052 if (!isSkipClassifier(artifact)) {
053 sb.append(":");
054 sb.append(artifact.getClassifier());
055 }
056 sb.append(":");
057 sb.append(artifact.getPackaging());
058 return sb.toString();
059 }
060
061 protected static boolean isSkipClassifier(Artifact artifact) {
062 return StringUtils.isBlank(artifact.getClassifier()) || NullUtils.isNullOrNone(artifact.getClassifier());
063 }
064
065 public static final String getRepositoryPath(Artifact artifact) {
066 StringBuilder sb = new StringBuilder();
067 sb.append(Str.getPath(artifact.getGroupId()));
068 sb.append(FS);
069 sb.append(artifact.getArtifactId());
070 sb.append(FS);
071 sb.append(artifact.getVersion());
072 return sb.toString();
073 }
074
075 public static final String getFilename(Artifact artifact) {
076 StringBuilder sb = new StringBuilder();
077 sb.append(artifact.getArtifactId());
078 sb.append("-");
079 sb.append(artifact.getVersion());
080 if (!isSkipClassifier(artifact)) {
081 sb.append(":");
082 sb.append(artifact.getClassifier());
083 }
084 sb.append(".");
085 sb.append(artifact.getPackaging());
086 return sb.toString();
087 }
088
089 public static final File getDefaultLocalRepositoryDir() {
090 return new File(FileUtils.getUserDirectoryPath() + FS + DEFAULT_MAVEN_REPO_PATH);
091 }
092
093 public static final File getFile(Artifact artifact) {
094 return getFile(getDefaultLocalRepositoryDir(), artifact);
095 }
096
097 public static final File getFile(File localRepositoryDir, Artifact artifact) {
098 String path = getRepositoryPath(artifact);
099 String filename = getFilename(artifact);
100 return new File(localRepositoryDir.getAbsolutePath() + FS + path, filename);
101 }
102
103 }