001/** 002 * Copyright 2010-2013 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 */ 016package org.kuali.common.util.project; 017 018import java.io.File; 019import java.util.ArrayList; 020import java.util.List; 021import java.util.Properties; 022 023import org.kuali.common.util.Assert; 024import org.kuali.common.util.Str; 025import org.kuali.common.util.file.CanonicalFile; 026import org.kuali.common.util.maven.MavenConstants; 027import org.kuali.common.util.project.model.Build; 028import org.kuali.common.util.project.model.ImmutableProject; 029import org.kuali.common.util.project.model.Project; 030import org.kuali.common.util.project.model.ProjectIdentifier; 031import org.kuali.common.util.project.model.ProjectResource; 032import org.springframework.util.ResourceUtils; 033 034public class ProjectUtils { 035 036 private static final String CLASSPATH = ResourceUtils.CLASSPATH_URL_PREFIX; 037 038 /** 039 * Get a <code>Project</code> from a <code>Properties</code> 040 */ 041 public static Project getProject(Properties properties) { 042 String groupId = properties.getProperty(MavenConstants.GROUP_ID_KEY); 043 String artifactId = properties.getProperty(MavenConstants.ARTIFACT_ID_KEY); 044 String version = properties.getProperty(MavenConstants.VERSION_KEY); 045 return new ImmutableProject(groupId, artifactId, version, properties); 046 } 047 048 /** 049 * Convert a project id into a <code>ProjectIdentifier's</code> 050 * 051 * Example project id: 052 * 053 * <pre> 054 * org.kuali.common:kuali-util 055 * </pre> 056 */ 057 public static ProjectIdentifier getIdentifier(String projectId) { 058 059 // Project id can't be blank 060 Assert.noBlanks("projectId is blank", projectId); 061 062 // Split up the id 063 String[] tokens = Str.split(projectId, ":", true); 064 065 // Must always have exactly 2 tokens 066 Assert.isTrue(tokens.length == 2, "tokens.length != 2"); 067 068 // 1st token is groupId, 2nd token is artifactId 069 String groupId = tokens[0]; 070 String artifactId = tokens[1]; 071 072 // Create a project identifier from the strings 073 return new ProjectIdentifier(groupId, artifactId); 074 } 075 076 /** 077 * Convert a list of project ids into a list of <code>ProjectIdentifier's</code> 078 * 079 * Example project id: 080 * 081 * <pre> 082 * org.kuali.common:kuali-util 083 * </pre> 084 */ 085 public static List<ProjectIdentifier> getIdentifiers(List<String> projectIds) { 086 List<ProjectIdentifier> list = new ArrayList<ProjectIdentifier>(); 087 for (String projectId : projectIds) { 088 ProjectIdentifier element = getIdentifier(projectId); 089 list.add(element); 090 } 091 return list; 092 } 093 094 /** 095 * Get a build object with local file system directories filled in. 096 * 097 * The typical directory structure looks like this: 098 * 099 * <pre> 100 * kuali-util/ 101 * kuali-util/target 102 * kuali-util/target/classes 103 * </pre> 104 */ 105 public static Build getBuild(Project project) { 106 File projectDir = getBasedir(project); 107 String encoding = getEncoding(project); 108 File buildDir = getBuildDirectory(project); 109 File outputDir = getBuildOutputDirectory(project); 110 File sourceDirectory = new CanonicalFile(project.getProperties().getProperty(MavenConstants.SOURCE_DIRECTORY_KEY)); 111 File testOutputDir = new CanonicalFile(project.getProperties().getProperty(MavenConstants.TEST_OUTPUT_DIRECTORY_KEY)); 112 File testSourceDir = new CanonicalFile(project.getProperties().getProperty(MavenConstants.TEST_SOURCE_DIRECTORY_KEY)); 113 File scriptSourceDirectory = new CanonicalFile(project.getProperties().getProperty(MavenConstants.SCRIPT_SOURCE_DIRECTORY_KEY)); 114 return new Build(project, encoding, projectDir, buildDir, outputDir, sourceDirectory, scriptSourceDirectory, testOutputDir, testSourceDir); 115 } 116 117 /** 118 * Convenience method for extracting the value of the property <code>project.build.directory</code> 119 */ 120 public static File getBuildDirectory(Project project) { 121 return new CanonicalFile(project.getProperties().getProperty(MavenConstants.BUILD_DIRECTORY_KEY)); 122 } 123 124 /** 125 * Convenience method for extracting the value of the property <code>project.basedir</code> 126 */ 127 public static File getBasedir(Project project) { 128 return new CanonicalFile(project.getProperties().getProperty(MavenConstants.BASEDIR_KEY)); 129 } 130 131 /** 132 * Convenience method for extracting the value of the property <code>project.build.outputDirectory</code> 133 */ 134 public static File getBuildOutputDirectory(Project project) { 135 return new CanonicalFile(project.getProperties().getProperty(MavenConstants.BUILD_OUTPUT_DIRECTORY_KEY)); 136 } 137 138 /** 139 * Convenience method for extracting the value of the property <code>project.encoding</code> 140 */ 141 public static String getEncoding(Project project) { 142 Assert.noNulls(project); 143 return project.getProperties().getProperty(MavenConstants.ENCODING_KEY); 144 } 145 146 /** 147 * Return a resource directory relative to <code>directory</code> 148 * 149 * <pre> 150 * /tmp/x/y/z + org.kuali.common:kuali-util -> /tmp/x/y/z/org/kuali/common/kuali-util 151 * </pre> 152 */ 153 public static File getResourceDirectory(File directory, String groupId, String artifactId) { 154 String path = getResourcePath(groupId, artifactId); 155 return new File(directory, path); 156 } 157 158 /** 159 * Return a resource friendly prefix. 160 * 161 * <pre> 162 * org.kuali.common:kuali-util -> org/kuali/common/kuali-util 163 * </pre> 164 */ 165 public static String getResourcePath(String groupId, String artifactId) { 166 Assert.noBlanks(groupId, artifactId); 167 return Str.getPath(groupId) + "/" + artifactId; 168 } 169 170 /** 171 * Use <code>getClasspathPrefix()</code> instead. (lowercase "p" in the word classpath) 172 * 173 * @deprecated 174 */ 175 @Deprecated 176 public static String getClassPathPrefix(String groupId, String artifactId) { 177 return getClasspathPrefix(groupId, artifactId); 178 } 179 180 /** 181 * Return a classpath prefix. 182 * 183 * <pre> 184 * org.kuali.common:kuali-util -> classpath:org/kuali/common/kuali-util 185 * </pre> 186 */ 187 public static String getClasspathPrefix(String groupId, String artifactId) { 188 return CLASSPATH + getResourcePath(groupId, artifactId); 189 } 190 191 /** 192 * Return a classpath prefix. 193 * 194 * <pre> 195 * org.kuali.common:kuali-util -> classpath:org/kuali/common/kuali-util 196 * </pre> 197 */ 198 public static String getClasspathPrefix(ProjectIdentifier project) { 199 return getClasspathPrefix(project.getGroupId(), project.getArtifactId()); 200 } 201 202 /** 203 * Return a classpath prefix. 204 * 205 * <pre> 206 * org.kuali.common -> classpath:org/kuali/common 207 * </pre> 208 */ 209 public static String getClasspathPrefix(String groupId) { 210 return CLASSPATH + Str.getPath(groupId); 211 } 212 213 /** 214 * Return a classpath prefix. 215 * 216 * <pre> 217 * org.kuali.common:kuali-util:metainf -> classpath:org/kuali/common/kuali-util/metainf 218 * </pre> 219 * 220 * @deprecated 221 */ 222 @Deprecated 223 public static String getClasspathPrefix(org.kuali.common.util.project.model.FeatureIdentifier feature) { 224 return getClasspathPrefix(feature.getProject()) + "/" + feature.getFeatureId(); 225 } 226 227 /** 228 * <pre> 229 * classpath:org/kuali/common/kuali-util/myfile.txt 230 * </pre> 231 */ 232 public static String getClasspathResource(ProjectIdentifier project, String filename) { 233 return getClasspathPrefix(project.getGroupId(), project.getArtifactId()) + "/" + filename; 234 } 235 236 /** 237 * <pre> 238 * [prefix]org/kuali/common/kuali-util/[path] 239 * </pre> 240 */ 241 public static String getPath(ProjectResource resource) { 242 StringBuilder sb = new StringBuilder(); 243 sb.append(resource.getPrefix()); 244 ProjectIdentifier project = resource.getProject(); 245 sb.append(Str.getPath(project.getGroupId())); 246 sb.append("/"); 247 sb.append(project.getArtifactId()); 248 sb.append("/"); 249 sb.append(resource.getPath()); 250 return sb.toString(); 251 } 252 253}