/** Populates list of projects from hierarchy. */ private void collectProjects(ProjectDefinition def) { if (byKey.containsKey(def.getKeyWithBranch())) { throw new IllegalStateException("Duplicate module key in reactor: " + def.getKeyWithBranch()); } byKey.put(def.getKeyWithBranch(), def); for (ProjectDefinition child : def.getSubProjects()) { collectProjects(child); } }
@Override protected void build(ProjectReactor reactor) { if (!settings.getBoolean("sonar.enableProjectBuilder")) { return; } System.out.println("---> Renaming project"); // change name of root project ProjectDefinition root = reactor.getRoot(); root.setName("Name changed by plugin"); }
private void addModule(Project parent, Project module) { ProjectDefinition parentDefinition = projectTree.getProjectDefinition(parent); java.io.File parentBaseDir = parentDefinition.getBaseDir(); ProjectDefinition moduleDefinition = projectTree.getProjectDefinition(module); java.io.File moduleBaseDir = moduleDefinition.getBaseDir(); module.setPath(new PathResolver().relativePath(parentBaseDir, moduleBaseDir)); addResource(module); for (Project submodule : module.getModules()) { addModule(module, submodule); } }
@VisibleForTesting protected static void cleanAndCheckProjectDefinitions(ProjectDefinition project) { if (project.getSubProjects().isEmpty()) { cleanAndCheckModuleProperties(project); } else { cleanAndCheckAggregatorProjectProperties(project); // clean modules properties as well for (ProjectDefinition module : project.getSubProjects()) { cleanAndCheckProjectDefinitions(module); } } }
public ProjectReactor execute() { Profiler profiler = Profiler.create(LOG).startInfo("Process project properties"); Map<String, Map<String, String>> propertiesByModuleId = new HashMap<>(); extractPropertiesByModule(propertiesByModuleId, "", taskProps.properties()); ProjectDefinition rootProject = defineRootProject(propertiesByModuleId.get(""), null); rootProjectWorkDir = rootProject.getWorkDir(); defineChildren(rootProject, propertiesByModuleId); cleanAndCheckProjectDefinitions(rootProject); // Since task properties are now empty we should add root module properties taskProps.properties().putAll(propertiesByModuleId.get("")); profiler.stopDebug(); return new ProjectReactor(rootProject); }
@Override public String getSource(Resource reference) { Resource resource = getResource(reference); if (resource instanceof File) { File file = (File) resource; Project module = currentProject; ProjectDefinition def = projectTree.getProjectDefinition(module); try { return FileUtils.readFileToString(new java.io.File(def.getBaseDir(), file.getPath())); } catch (IOException e) { throw new IllegalStateException("Unable to read file content " + reference, e); } } return null; }
@Override protected List<File> createElements() { if (pom != null) { return super.createElements(); } else { List<File> elements = Lists.newArrayList(); for (String path : def.getBinaries()) { elements.add(projectFileSystem.resolvePath(path)); } for (String path : def.getLibraries()) { elements.add(projectFileSystem.resolvePath(path)); } return elements; } }
public ImmutableProjectReactor(ProjectDefinition root) { if (root.getParent() != null) { throw new IllegalArgumentException("Not a root project: " + root); } this.root = root; collectProjects(root); }
@Test public void test_loading_of_module_settings() { BatchSettings batchSettings = mock(BatchSettings.class); when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions()); when(batchSettings.getProperties()) .thenReturn( ImmutableMap.of( "overridding", "batch", "on-batch", "true")); when(batchSettings.getModuleProperties("struts-core")) .thenReturn( ImmutableMap.of( "on-module", "true", "overridding", "module")); ProjectDefinition module = ProjectDefinition.create().setKey("struts-core"); Configuration deprecatedConf = new PropertiesConfiguration(); ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, deprecatedConf); assertThat(moduleSettings.getString("overridding")).isEqualTo("module"); assertThat(moduleSettings.getString("on-batch")).isEqualTo("true"); assertThat(moduleSettings.getString("on-module")).isEqualTo("true"); assertThat(deprecatedConf.getString("overridding")).isEqualTo("module"); assertThat(deprecatedConf.getString("on-batch")).isEqualTo("true"); assertThat(deprecatedConf.getString("on-module")).isEqualTo("true"); }
private void defineChildren( ProjectDefinition parentProject, Map<String, Map<String, String>> propertiesByModuleId) { Map<String, String> parentProps = parentProject.properties(); if (parentProps.containsKey(PROPERTY_MODULES)) { for (String moduleId : getListFromProperty(parentProps, PROPERTY_MODULES)) { Map<String, String> moduleProps = propertiesByModuleId.get(moduleId); ProjectDefinition childProject = loadChildProject(parentProject, moduleProps, moduleId); // check the uniqueness of the child key checkUniquenessOfChildKey(childProject, parentProject); // the child project may have children as well defineChildren(childProject, propertiesByModuleId); // and finally add this child project to its parent parentProject.addSubProject(childProject); } } }
protected ProjectDefinition defineRootProject( Map<String, String> rootProperties, @Nullable ProjectDefinition parent) { prepareNonAssociatedProject(rootProperties, analysisMode); if (rootProperties.containsKey(PROPERTY_MODULES)) { checkMandatoryProperties(rootProperties, MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT); } else { checkMandatoryProperties(rootProperties, MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT); } File baseDir = new File(rootProperties.get(PROPERTY_PROJECT_BASEDIR)); final String projectKey = rootProperties.get(CoreProperties.PROJECT_KEY_PROPERTY); File workDir; if (parent == null) { validateDirectories(rootProperties, baseDir, projectKey); workDir = initRootProjectWorkDir(baseDir, rootProperties); } else { workDir = initModuleWorkDir(baseDir, rootProperties); } return ProjectDefinition.create() .setProperties(rootProperties) .setBaseDir(baseDir) .setWorkDir(workDir) .setBuildDir(initModuleBuildDir(baseDir, rootProperties)); }
private Bucket getBucket(@Nullable Resource reference) { if (reference == null) { return null; } if (StringUtils.isNotBlank(reference.getKey())) { return buckets.get(reference); } String relativePathFromSourceDir = null; boolean isTest = false; boolean isDir = false; if (reference instanceof File) { File referenceFile = (File) reference; isTest = Qualifiers.UNIT_TEST_FILE.equals(referenceFile.getQualifier()); relativePathFromSourceDir = referenceFile.relativePathFromSourceDir(); } else if (reference instanceof Directory) { isDir = true; Directory referenceDir = (Directory) reference; relativePathFromSourceDir = referenceDir.relativePathFromSourceDir(); if (Directory.ROOT.equals(relativePathFromSourceDir)) { relativePathFromSourceDir = ""; } } if (relativePathFromSourceDir != null) { // Resolve using deprecated key List<String> dirs; ProjectDefinition projectDef = projectTree.getProjectDefinition(getProject()); if (isTest) { dirs = projectDef.getTestDirs(); } else { dirs = projectDef.getSourceDirs(); } for (String src : dirs) { java.io.File dirOrFile = pathResolver.relativeFile(projectDef.getBaseDir(), src); java.io.File abs = new java.io.File(dirOrFile, relativePathFromSourceDir); Bucket b = getBucket( isDir ? Directory.fromIOFile(abs, getProject()) : File.fromIOFile(abs, getProject())); if (b != null) { return b; } } } return null; }
@VisibleForTesting protected static void cleanAndCheckModuleProperties(ProjectDefinition project) { Map<String, String> properties = project.properties(); // We need to check the existence of source directories String[] sourcePaths = getListFromProperty(properties, PROPERTY_SOURCES); checkExistenceOfPaths(project.getKey(), project.getBaseDir(), sourcePaths, PROPERTY_SOURCES); // And we need to resolve patterns that may have been used in "sonar.libraries" List<String> libPaths = Lists.newArrayList(); for (String pattern : getListFromProperty(properties, PROPERTY_LIBRARIES)) { for (File file : getLibraries(project.getBaseDir(), pattern)) { libPaths.add(file.getAbsolutePath()); } } properties.remove(PROPERTY_LIBRARIES); properties.put(PROPERTY_LIBRARIES, StringUtils.join(libPaths, ",")); }
protected ProjectDefinition loadChildProject( ProjectDefinition parentProject, Map<String, String> moduleProps, String moduleId) { final File baseDir; if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) { baseDir = resolvePath(parentProject.getBaseDir(), moduleProps.get(PROPERTY_PROJECT_BASEDIR)); setProjectBaseDir(baseDir, moduleProps, moduleId); } else { baseDir = new File(parentProject.getBaseDir(), moduleId); setProjectBaseDir(baseDir, moduleProps, moduleId); } setModuleKeyAndNameIfNotDefined(moduleProps, moduleId, parentProject.getKey()); // and finish checkMandatoryProperties(moduleProps, MANDATORY_PROPERTIES_FOR_CHILD); validateDirectories(moduleProps, baseDir, moduleId); mergeParentProperties(moduleProps, parentProject.properties()); return defineRootProject(moduleProps, parentProject); }
@VisibleForTesting protected static void cleanAndCheckAggregatorProjectProperties(ProjectDefinition project) { Map<String, String> properties = project.properties(); // SONARPLUGINS-2295 String[] sourceDirs = getListFromProperty(properties, PROPERTY_SOURCES); for (String path : sourceDirs) { File sourceFolder = resolvePath(project.getBaseDir(), path); if (sourceFolder.isDirectory()) { LOG.warn( "/!\\ A multi-module project can't have source folders, so '{}' won't be used for the analysis. " + "If you want to analyse files of this folder, you should create another sub-module and move them inside it.", sourceFolder.toString()); } } // "aggregator" project must not have the following properties: properties.remove(PROPERTY_SOURCES); properties.remove(PROPERTY_TESTS); properties.remove(PROPERTY_BINARIES); properties.remove(PROPERTY_LIBRARIES); }
void doStart(List<ProjectDefinition> definitions) { projects = Lists.newArrayList(); projectsByDef = Maps.newHashMap(); for (ProjectDefinition def : definitions) { Project project = configurator.create(def); projectsByDef.put(def, project); projects.add(project); } for (Map.Entry<ProjectDefinition, Project> entry : projectsByDef.entrySet()) { ProjectDefinition def = entry.getKey(); Project project = entry.getValue(); for (ProjectDefinition module : def.getSubProjects()) { projectsByDef.get(module).setParent(project); } } // Configure for (Project project : projects) { configurator.configure(project); } }
@Test public void testOrderedProjects() { ProjectDefinition grandParent = ProjectDefinition.create(); ProjectDefinition parent = ProjectDefinition.create(); ProjectDefinition child = ProjectDefinition.create(); grandParent.addSubProject(parent); parent.addSubProject(child); List<ProjectDefinition> hierarchy = ModuleSettings.getTopDownParentProjects(child); assertThat(hierarchy.get(0)).isEqualTo(grandParent); assertThat(hierarchy.get(1)).isEqualTo(parent); assertThat(hierarchy.get(2)).isEqualTo(child); }
@VisibleForTesting protected static void checkUniquenessOfChildKey( ProjectDefinition childProject, ProjectDefinition parentProject) { for (ProjectDefinition definition : parentProject.getSubProjects()) { if (definition.getKey().equals(childProject.getKey())) { throw new IllegalStateException( "Project '" + parentProject.getKey() + "' can't have 2 modules with the following key: " + childProject.getKey()); } } }
@Before public void setUp() { mode = mock(DefaultAnalysisMode.class); reactor = mock(ImmutableProjectReactor.class); when(reactor.getRoot()).thenReturn(ProjectDefinition.create().setKey("struts")); }
@Before public void setUp() { root = ProjectDefinition.create().setKey("struts").setWorkDir(temp.getRoot()); when(reactor.getRoot()).thenReturn(root); when(server.getPublicRootUrl()).thenReturn("https://localhost"); }