View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util;
17  
18  import java.io.File;
19  
20  import org.apache.commons.io.FileUtils;
21  import org.kuali.common.util.nullify.NullUtils;
22  
23  public class RepositoryUtils {
24  
25  	private static final String FS = File.separator;
26  	private static final String DEFAULT_MAVEN_REPO_PATH = ".m2" + FS + "repository";
27  
28  	public static final String toString(Artifact artifact) {
29  		StringBuilder sb = new StringBuilder();
30  		sb.append(artifact.getGroupId());
31  		sb.append(":");
32  		sb.append(artifact.getArtifactId());
33  		sb.append(":");
34  		sb.append(artifact.getVersion());
35  		if (!NullUtils.isNullOrNone(artifact.getClassifier())) {
36  			sb.append(":");
37  			sb.append(artifact.getClassifier());
38  		}
39  		sb.append(":");
40  		sb.append(artifact.getPackaging());
41  		return sb.toString();
42  	}
43  
44  	public static final String getRepositoryPath(Artifact artifact) {
45  		StringBuilder sb = new StringBuilder();
46  		sb.append(Str.getPath(artifact.getGroupId()));
47  		sb.append(FS);
48  		sb.append(artifact.getArtifactId());
49  		sb.append(FS);
50  		sb.append(artifact.getVersion());
51  		return sb.toString();
52  	}
53  
54  	public static final String getFilename(Artifact artifact) {
55  		StringBuilder sb = new StringBuilder();
56  		sb.append(artifact.getArtifactId());
57  		sb.append("-");
58  		sb.append(artifact.getVersion());
59  		if (!NullUtils.isNullOrNone(artifact.getClassifier())) {
60  			sb.append("-");
61  			sb.append(artifact.getClassifier());
62  		}
63  		sb.append(".");
64  		sb.append(artifact.getPackaging());
65  		return sb.toString();
66  	}
67  
68  	public static final File getDefaultLocalRepositoryDir() {
69  		return new File(FileUtils.getUserDirectoryPath() + FS + DEFAULT_MAVEN_REPO_PATH);
70  	}
71  
72  	public static final File getFile(Artifact artifact) {
73  		return getFile(getDefaultLocalRepositoryDir(), artifact);
74  	}
75  
76  	public static final File getFile(File localRepositoryDir, Artifact artifact) {
77  		String path = getRepositoryPath(artifact);
78  		String filename = getFilename(artifact);
79  		return new File(localRepositoryDir.getAbsolutePath() + FS + path, filename);
80  	}
81  
82  }