You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SonarProjectBuilder.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Sonar Runner - Implementation
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner.internal.batch;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import com.google.common.collect.Lists;
  23. import org.apache.commons.io.IOUtils;
  24. import org.apache.commons.io.filefilter.AndFileFilter;
  25. import org.apache.commons.io.filefilter.FileFileFilter;
  26. import org.apache.commons.io.filefilter.WildcardFileFilter;
  27. import org.apache.commons.lang.StringUtils;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  31. import org.sonar.runner.RunnerException;
  32. import java.io.File;
  33. import java.io.FileFilter;
  34. import java.io.FileInputStream;
  35. import java.io.IOException;
  36. import java.util.HashMap;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.Map.Entry;
  40. import java.util.Properties;
  41. /**
  42. * Class that creates a Sonar project definition based on a set of properties.
  43. *
  44. * @since 1.5
  45. */
  46. public final class SonarProjectBuilder {
  47. private static final Logger LOG = LoggerFactory.getLogger(SonarProjectBuilder.class);
  48. private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir";
  49. private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
  50. private static final String PROPERTY_PROJECT_KEY = "sonar.projectKey";
  51. private static final String PROPERTY_PROJECT_NAME = "sonar.projectName";
  52. private static final String PROPERTY_PROJECT_DESCRIPTION = "sonar.projectDescription";
  53. private static final String PROPERTY_PROJECT_VERSION = "sonar.projectVersion";
  54. private static final String PROPERTY_MODULES = "sonar.modules";
  55. /**
  56. * New properties, to be consistent with Sonar naming conventions
  57. * @since 1.5
  58. */
  59. private static final String PROPERTY_SOURCES = "sonar.sources";
  60. private static final String PROPERTY_TESTS = "sonar.tests";
  61. private static final String PROPERTY_BINARIES = "sonar.binaries";
  62. private static final String PROPERTY_LIBRARIES = "sonar.libraries";
  63. /**
  64. * Old deprecated properties, replaced by the same ones preceded by "sonar."
  65. */
  66. private static final String PROPERTY_OLD_SOURCES = "sources";
  67. private static final String PROPERTY_OLD_TESTS = "tests";
  68. private static final String PROPERTY_OLD_BINARIES = "binaries";
  69. private static final String PROPERTY_OLD_LIBRARIES = "libraries";
  70. private static final Map<String, String> DEPRECATED_PROPS_TO_NEW_PROPS = new HashMap<String, String>() {
  71. {
  72. put(PROPERTY_OLD_SOURCES, PROPERTY_SOURCES);
  73. put(PROPERTY_OLD_TESTS, PROPERTY_TESTS);
  74. put(PROPERTY_OLD_BINARIES, PROPERTY_BINARIES);
  75. put(PROPERTY_OLD_LIBRARIES, PROPERTY_LIBRARIES);
  76. }
  77. };
  78. /**
  79. * @since 1.4
  80. */
  81. private static final String PROPERTY_WORK_DIRECTORY = "sonar.working.directory";
  82. private static final String DEF_VALUE_WORK_DIRECTORY = ".sonar";
  83. /**
  84. * Array of all mandatory properties required for a project without child.
  85. */
  86. private static final String[] MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT = {PROPERTY_PROJECT_BASEDIR, PROPERTY_PROJECT_KEY, PROPERTY_PROJECT_NAME, PROPERTY_PROJECT_VERSION,
  87. PROPERTY_SOURCES};
  88. /**
  89. * Array of all mandatory properties required for a project with children.
  90. */
  91. private static final String[] MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT = {PROPERTY_PROJECT_BASEDIR, PROPERTY_PROJECT_KEY, PROPERTY_PROJECT_NAME, PROPERTY_PROJECT_VERSION};
  92. /**
  93. * Array of all mandatory properties required for a child project before its properties get merged with its parent ones.
  94. */
  95. private static final String[] MANDATORY_PROPERTIES_FOR_CHILD = {PROPERTY_PROJECT_KEY, PROPERTY_PROJECT_NAME};
  96. /**
  97. * Properties that must not be passed from the parent project to its children.
  98. */
  99. private static final List<String> NON_HERITED_PROPERTIES_FOR_CHILD = Lists.newArrayList(PROPERTY_PROJECT_BASEDIR, PROPERTY_MODULES, PROPERTY_PROJECT_DESCRIPTION);
  100. private String command;
  101. private Properties properties;
  102. private File rootProjectWorkDir;
  103. private SonarProjectBuilder(String command, Properties properties) {
  104. this.command = command;
  105. this.properties = properties;
  106. }
  107. public static SonarProjectBuilder create(Properties properties) {
  108. return create(null, properties);
  109. }
  110. public static SonarProjectBuilder create(String command, Properties properties) {
  111. return new SonarProjectBuilder(command, properties);
  112. }
  113. public ProjectDefinition generateProjectDefinition() {
  114. if (StringUtils.isBlank(command) || "inspect".equals(command)) {
  115. ProjectDefinition rootProject = defineProject(properties, null);
  116. rootProjectWorkDir = rootProject.getWorkDir();
  117. defineChildren(rootProject);
  118. cleanAndCheckProjectDefinitions(rootProject);
  119. return rootProject;
  120. }
  121. else if (properties.containsKey(PROPERTY_PROJECT_KEY)) {
  122. return defineTaskContext();
  123. }
  124. else {
  125. return null;
  126. }
  127. }
  128. private ProjectDefinition defineTaskContext() {
  129. File baseDir = new File(System.getProperty("user.home"));
  130. File workDir = initRootProjectWorkDir(baseDir);
  131. ProjectDefinition definition = ProjectDefinition.create().setProperties(properties)
  132. .setBaseDir(baseDir)
  133. .setWorkDir(workDir);
  134. return definition;
  135. }
  136. private ProjectDefinition defineProject(Properties properties, ProjectDefinition parent) {
  137. if (properties.containsKey(PROPERTY_MODULES)) {
  138. checkMandatoryProperties(properties, MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT);
  139. }
  140. else {
  141. checkMandatoryProperties(properties, MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT);
  142. }
  143. File baseDir = new File(properties.getProperty(PROPERTY_PROJECT_BASEDIR));
  144. File workDir = null;
  145. if (parent == null) {
  146. workDir = initRootProjectWorkDir(baseDir);
  147. } else {
  148. workDir = initModuleWorkDir(properties);
  149. }
  150. ProjectDefinition definition = ProjectDefinition.create().setProperties(properties)
  151. .setBaseDir(baseDir)
  152. .setWorkDir(workDir);
  153. return definition;
  154. }
  155. @VisibleForTesting
  156. protected File initRootProjectWorkDir(File baseDir) {
  157. String workDir = properties.getProperty(PROPERTY_WORK_DIRECTORY);
  158. if (StringUtils.isBlank(workDir)) {
  159. return new File(baseDir, DEF_VALUE_WORK_DIRECTORY);
  160. }
  161. File customWorkDir = new File(workDir);
  162. if (customWorkDir.isAbsolute()) {
  163. return customWorkDir;
  164. }
  165. return new File(baseDir, customWorkDir.getPath());
  166. }
  167. @VisibleForTesting
  168. protected File initModuleWorkDir(Properties properties) {
  169. String cleanKey = StringUtils.deleteWhitespace(properties.getProperty(PROPERTY_PROJECT_KEY));
  170. cleanKey = StringUtils.replace(cleanKey, ":", "_");
  171. return new File(rootProjectWorkDir, cleanKey);
  172. }
  173. private void defineChildren(ProjectDefinition parentProject) {
  174. Properties parentProps = parentProject.getProperties();
  175. if (parentProps.containsKey(PROPERTY_MODULES)) {
  176. for (String module : SonarRunnerUtils.getListFromProperty(parentProps, PROPERTY_MODULES)) {
  177. Properties moduleProps = extractModuleProperties(module, parentProps);
  178. ProjectDefinition childProject = loadChildProject(parentProject, moduleProps, module);
  179. // check the uniqueness of the child key
  180. checkUniquenessOfChildKey(childProject, parentProject);
  181. // the child project may have children as well
  182. defineChildren(childProject);
  183. // and finally add this child project to its parent
  184. parentProject.addSubProject(childProject);
  185. }
  186. }
  187. }
  188. private ProjectDefinition loadChildProject(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
  189. setProjectKeyAndNameIfNotDefined(moduleProps, moduleId);
  190. final File baseDir;
  191. if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
  192. baseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), parentProject.getBaseDir());
  193. setProjectBaseDir(baseDir, moduleProps, moduleId);
  194. try {
  195. if (!parentProject.getBaseDir().getCanonicalFile().equals(baseDir.getCanonicalFile())) {
  196. tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
  197. }
  198. } catch (IOException e) {
  199. throw new RunnerException("Error when resolving baseDir", e);
  200. }
  201. } else if (moduleProps.containsKey(PROPERTY_PROJECT_CONFIG_FILE)) {
  202. baseDir = loadPropsFile(parentProject, moduleProps, moduleId);
  203. } else {
  204. baseDir = new File(parentProject.getBaseDir(), moduleId);
  205. setProjectBaseDir(baseDir, moduleProps, moduleId);
  206. tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
  207. }
  208. // and finish
  209. checkMandatoryProperties(moduleProps, MANDATORY_PROPERTIES_FOR_CHILD);
  210. if (!moduleProps.containsKey(PROPERTY_MODULES)) {
  211. // SONARPLUGINS-2285 Not an aggreator project so we can validate that paths are correct if defined
  212. // We need to resolve patterns that may have been used in "sonar.libraries"
  213. for (String pattern : SonarRunnerUtils.getListFromProperty(moduleProps, PROPERTY_LIBRARIES)) {
  214. File[] files = getLibraries(baseDir, pattern);
  215. if (files == null || files.length == 0) {
  216. LOG.error("Invalid value of " + PROPERTY_LIBRARIES + " for module " + moduleId);
  217. throw new RunnerException("No files matching pattern \"" + pattern + "\" in directory \"" + baseDir + "\"");
  218. }
  219. }
  220. // Check sonar.tests
  221. String[] testDirs = SonarRunnerUtils.getListFromProperty(moduleProps, PROPERTY_TESTS);
  222. checkExistenceOfDirectories(moduleId, baseDir, testDirs, PROPERTY_TESTS);
  223. // Check sonar.binaries
  224. String[] binDirs = SonarRunnerUtils.getListFromProperty(moduleProps, PROPERTY_BINARIES);
  225. checkExistenceOfDirectories(moduleId, baseDir, binDirs, PROPERTY_BINARIES);
  226. }
  227. mergeParentProperties(moduleProps, parentProject.getProperties());
  228. prefixProjectKeyWithParentKey(moduleProps, parentProject.getKey());
  229. return defineProject(moduleProps, parentProject);
  230. }
  231. /**
  232. * @return baseDir
  233. */
  234. protected File loadPropsFile(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
  235. File propertyFile = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_CONFIG_FILE), parentProject.getBaseDir());
  236. if (propertyFile.isFile()) {
  237. Properties propsFromFile = toProperties(propertyFile);
  238. for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
  239. moduleProps.put(entry.getKey(), entry.getValue());
  240. }
  241. File baseDir = null;
  242. if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
  243. baseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParentFile());
  244. } else {
  245. baseDir = propertyFile.getParentFile();
  246. }
  247. setProjectBaseDir(baseDir, moduleProps, moduleId);
  248. return baseDir;
  249. } else {
  250. throw new RunnerException("The properties file of the module '" + moduleId + "' does not exist: " + propertyFile.getAbsolutePath());
  251. }
  252. }
  253. private void tryToFindAndLoadPropsFile(File baseDir, Properties moduleProps, String moduleId) {
  254. File propertyFile = new File(baseDir, "sonar-project.properties");
  255. if (propertyFile.isFile()) {
  256. Properties propsFromFile = toProperties(propertyFile);
  257. for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
  258. moduleProps.put(entry.getKey(), entry.getValue());
  259. }
  260. if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
  261. File overwrittenBaseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParentFile());
  262. setProjectBaseDir(overwrittenBaseDir, moduleProps, moduleId);
  263. }
  264. }
  265. }
  266. @VisibleForTesting
  267. protected static Properties toProperties(File propertyFile) {
  268. Properties propsFromFile = new Properties();
  269. FileInputStream fileInputStream = null;
  270. try {
  271. fileInputStream = new FileInputStream(propertyFile);
  272. propsFromFile.load(fileInputStream);
  273. } catch (IOException e) {
  274. throw new RunnerException("Impossible to read the property file: " + propertyFile.getAbsolutePath(), e);
  275. } finally {
  276. IOUtils.closeQuietly(fileInputStream);
  277. }
  278. return propsFromFile;
  279. }
  280. @VisibleForTesting
  281. protected static void setProjectKeyAndNameIfNotDefined(Properties childProps, String moduleId) {
  282. if (!childProps.containsKey(PROPERTY_PROJECT_KEY)) {
  283. childProps.put(PROPERTY_PROJECT_KEY, moduleId);
  284. }
  285. if (!childProps.containsKey(PROPERTY_PROJECT_NAME)) {
  286. childProps.put(PROPERTY_PROJECT_NAME, moduleId);
  287. }
  288. }
  289. @VisibleForTesting
  290. protected static void checkUniquenessOfChildKey(ProjectDefinition childProject, ProjectDefinition parentProject) {
  291. for (ProjectDefinition definition : parentProject.getSubProjects()) {
  292. if (definition.getKey().equals(childProject.getKey())) {
  293. throw new RunnerException("Project '" + parentProject.getKey() + "' can't have 2 modules with the following key: " + childProject.getKey());
  294. }
  295. }
  296. }
  297. @VisibleForTesting
  298. protected static void prefixProjectKeyWithParentKey(Properties childProps, String parentKey) {
  299. String childKey = childProps.getProperty(PROPERTY_PROJECT_KEY);
  300. childProps.put(PROPERTY_PROJECT_KEY, parentKey + ":" + childKey);
  301. }
  302. private static void setProjectBaseDir(File baseDir, Properties childProps, String moduleId) {
  303. if (!baseDir.isDirectory()) {
  304. throw new RunnerException("The base directory of the module '" + moduleId + "' does not exist: " + baseDir.getAbsolutePath());
  305. }
  306. childProps.put(PROPERTY_PROJECT_BASEDIR, baseDir.getAbsolutePath());
  307. }
  308. @VisibleForTesting
  309. protected static void checkMandatoryProperties(Properties props, String[] mandatoryProps) {
  310. replaceDeprecatedProperties(props);
  311. StringBuilder missing = new StringBuilder();
  312. for (String mandatoryProperty : mandatoryProps) {
  313. if (!props.containsKey(mandatoryProperty)) {
  314. if (missing.length() > 0) {
  315. missing.append(", ");
  316. }
  317. missing.append(mandatoryProperty);
  318. }
  319. }
  320. if (missing.length() != 0) {
  321. String projectKey = props.getProperty(PROPERTY_PROJECT_KEY);
  322. throw new RunnerException("You must define the following mandatory properties for '" + (projectKey == null ? "Unknown" : projectKey) + "': " + missing);
  323. }
  324. }
  325. @VisibleForTesting
  326. protected static void cleanAndCheckProjectDefinitions(ProjectDefinition project) {
  327. if (project.getSubProjects().isEmpty()) {
  328. cleanAndCheckModuleProperties(project);
  329. } else {
  330. cleanAndCheckAggregatorProjectProperties(project);
  331. // clean modules properties as well
  332. for (ProjectDefinition module : project.getSubProjects()) {
  333. cleanAndCheckProjectDefinitions(module);
  334. }
  335. }
  336. }
  337. @VisibleForTesting
  338. protected static void cleanAndCheckModuleProperties(ProjectDefinition project) {
  339. Properties properties = project.getProperties();
  340. // We need to check the existence of source directories
  341. String[] sourceDirs = SonarRunnerUtils.getListFromProperty(properties, PROPERTY_SOURCES);
  342. checkExistenceOfDirectories(project.getKey(), project.getBaseDir(), sourceDirs, PROPERTY_SOURCES);
  343. // And we need to resolve patterns that may have been used in "sonar.libraries"
  344. List<String> libPaths = Lists.newArrayList();
  345. for (String pattern : SonarRunnerUtils.getListFromProperty(properties, PROPERTY_LIBRARIES)) {
  346. for (File file : getLibraries(project.getBaseDir(), pattern)) {
  347. libPaths.add(file.getAbsolutePath());
  348. }
  349. }
  350. properties.remove(PROPERTY_LIBRARIES);
  351. properties.put(PROPERTY_LIBRARIES, StringUtils.join(libPaths, ","));
  352. }
  353. @VisibleForTesting
  354. protected static void cleanAndCheckAggregatorProjectProperties(ProjectDefinition project) {
  355. Properties properties = project.getProperties();
  356. // SONARPLUGINS-2295
  357. String[] sourceDirs = SonarRunnerUtils.getListFromProperty(properties, PROPERTY_SOURCES);
  358. for (String path : sourceDirs) {
  359. File sourceFolder = getFileFromPath(path, project.getBaseDir());
  360. if (sourceFolder.isDirectory()) {
  361. LOG.warn("/!\\ A multi-module project can't have source folders, so '{}' won't be used for the analysis. " +
  362. "If you want to analyse files of this folder, you should create another sub-module and move them inside it.",
  363. sourceFolder.toString());
  364. }
  365. }
  366. // "aggregator" project must not have the following properties:
  367. properties.remove(PROPERTY_SOURCES);
  368. properties.remove(PROPERTY_TESTS);
  369. properties.remove(PROPERTY_BINARIES);
  370. properties.remove(PROPERTY_LIBRARIES);
  371. // and they don't need properties related to their modules either
  372. Properties clone = (Properties) properties.clone();
  373. List<String> moduleIds = Lists.newArrayList(SonarRunnerUtils.getListFromProperty(properties, PROPERTY_MODULES));
  374. for (Entry<Object, Object> entry : clone.entrySet()) {
  375. String key = (String) entry.getKey();
  376. if (isKeyPrefixedByModuleId(key, moduleIds)) {
  377. properties.remove(key);
  378. }
  379. }
  380. }
  381. /**
  382. * Replaces the deprecated properties by the new ones, and logs a message to warn the users.
  383. */
  384. @VisibleForTesting
  385. protected static void replaceDeprecatedProperties(Properties props) {
  386. for (Entry<String, String> entry : DEPRECATED_PROPS_TO_NEW_PROPS.entrySet()) {
  387. String key = entry.getKey();
  388. if (props.containsKey(key)) {
  389. String newKey = entry.getValue();
  390. LOG.warn("/!\\ The '{}' property is deprecated and is replaced by '{}'. Don't forget to update your files.", key, newKey);
  391. String value = props.getProperty(key);
  392. props.remove(key);
  393. props.put(newKey, value);
  394. }
  395. }
  396. }
  397. @VisibleForTesting
  398. protected static void mergeParentProperties(Properties childProps, Properties parentProps) {
  399. List<String> moduleIds = Lists.newArrayList(SonarRunnerUtils.getListFromProperty(parentProps, PROPERTY_MODULES));
  400. for (Map.Entry<Object, Object> entry : parentProps.entrySet()) {
  401. String key = (String) entry.getKey();
  402. if (!childProps.containsKey(key)
  403. && !NON_HERITED_PROPERTIES_FOR_CHILD.contains(key)
  404. && !isKeyPrefixedByModuleId(key, moduleIds)) {
  405. childProps.put(entry.getKey(), entry.getValue());
  406. }
  407. }
  408. }
  409. private static boolean isKeyPrefixedByModuleId(String key, List<String> moduleIds) {
  410. for (String moduleId : moduleIds) {
  411. if (key.startsWith(moduleId + ".")) {
  412. return true;
  413. }
  414. }
  415. return false;
  416. }
  417. @VisibleForTesting
  418. protected static Properties extractModuleProperties(String module, Properties properties) {
  419. Properties moduleProps = new Properties();
  420. String propertyPrefix = module + ".";
  421. int prefixLength = propertyPrefix.length();
  422. for (Map.Entry<Object, Object> entry : properties.entrySet()) {
  423. String key = (String) entry.getKey();
  424. if (key.startsWith(propertyPrefix)) {
  425. moduleProps.put(key.substring(prefixLength), entry.getValue());
  426. }
  427. }
  428. return moduleProps;
  429. }
  430. @VisibleForTesting
  431. protected static void checkExistenceOfDirectories(String moduleRef, File baseDir, String[] sourceDirs, String propName) {
  432. for (String path : sourceDirs) {
  433. File sourceFolder = getFileFromPath(path, baseDir);
  434. if (!sourceFolder.isDirectory()) {
  435. LOG.error("Invalid value of " + propName + " for " + moduleRef);
  436. throw new RunnerException("The folder '" + path + "' does not exist for '" + moduleRef +
  437. "' (base directory = " + baseDir.getAbsolutePath() + ")");
  438. }
  439. }
  440. }
  441. /**
  442. * Returns files matching specified pattern.
  443. */
  444. @VisibleForTesting
  445. protected static File[] getLibraries(File baseDir, String pattern) {
  446. final int i = Math.max(pattern.lastIndexOf('/'), pattern.lastIndexOf('\\'));
  447. final String dirPath, filePattern;
  448. if (i == -1) {
  449. dirPath = ".";
  450. filePattern = pattern;
  451. } else {
  452. dirPath = pattern.substring(0, i);
  453. filePattern = pattern.substring(i + 1);
  454. }
  455. FileFilter fileFilter = new AndFileFilter(FileFileFilter.FILE, new WildcardFileFilter(filePattern));
  456. File dir = resolvePath(baseDir, dirPath);
  457. File[] files = dir.listFiles(fileFilter);
  458. if (files == null) {
  459. files = new File[0];
  460. }
  461. return files;
  462. }
  463. private static File resolvePath(File baseDir, String path) {
  464. File file = new File(path);
  465. if (!file.isAbsolute()) {
  466. try {
  467. file = new File(baseDir, path).getCanonicalFile();
  468. } catch (IOException e) {
  469. throw new RunnerException("Unable to resolve path \"" + path + "\"", e);
  470. }
  471. }
  472. return file;
  473. }
  474. /**
  475. * Returns the file denoted by the given path, may this path be relative to "baseDir" or absolute.
  476. */
  477. @VisibleForTesting
  478. protected static File getFileFromPath(String path, File baseDir) {
  479. File propertyFile = new File(path.trim());
  480. if (!propertyFile.isAbsolute()) {
  481. propertyFile = new File(baseDir, propertyFile.getPath());
  482. }
  483. return propertyFile;
  484. }
  485. }