org.sonar.maven.MavenProjectHierarchy Maven / Gradle / Ivy
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2009 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.maven;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.Transformer;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class MavenProjectHierarchy {
private static Class extends ModulePomFileResolver> resolverImpl = DefaultModuleFileResolver.class;
private MavenProjectHierarchy parent;
private MavenProject project;
private List modules = new ArrayList();
private MavenProjectHierarchy(MavenProject project) {
this.project = project;
}
public boolean isRoot() {
return parent == null;
}
public static void setResolverImpl(Class extends ModulePomFileResolver> resolverImpl) {
MavenProjectHierarchy.resolverImpl = resolverImpl;
}
public MavenProjectHierarchy getModule(MavenProject project) {
if (this.getProject().equals(project)) {
return this;
}
for (MavenProjectHierarchy hierarchy : modules) {
MavenProjectHierarchy moduleChild = hierarchy.getModule(project);
if (moduleChild != null) {
return moduleChild;
}
}
return null;
}
public Collection getChildrensProjects() {
Collection childrens = getChildrensHierarchy();
return CollectionUtils.collect(childrens, new Transformer() {
public Object transform(Object o) {
return ((MavenProjectHierarchy) o).getProject();
}
});
}
public Collection getChildrensHierarchy() {
List childrens = new ArrayList();
addChildrens(childrens);
return childrens;
}
private Collection addChildrens(Collection h) {
h.addAll(modules);
for (MavenProjectHierarchy hierarchy : modules) {
hierarchy.addChildrens(h);
}
return h;
}
public List getModules() {
return modules;
}
public MavenProject getParent() {
return parent.getProject();
}
public MavenProject getProject() {
return project;
}
public static MavenProjectHierarchy getProjectHierarchy(List projects) throws MojoExecutionException {
List hierarchy = new ArrayList();
for (MavenProject project : projects) {
hierarchy.add(new MavenProjectHierarchy(project));
}
for (MavenProject project : projects) {
MavenProjectHierarchy h = getHierarchy(hierarchy, project);
List modules = getProjectModules(project, projects);
if (!modules.isEmpty()) {
List modulesHierarchy = new ArrayList();
for (MavenProject module : modules) {
MavenProjectHierarchy moduleHierarchy = getHierarchy(hierarchy, module);
moduleHierarchy.parent = h;
modulesHierarchy.add(moduleHierarchy);
}
h.getModules().addAll(modulesHierarchy);
}
}
return (MavenProjectHierarchy) CollectionUtils.find(hierarchy, new Predicate() {
public boolean evaluate(Object o) {
return ((MavenProjectHierarchy) o).isRoot();
}
});
}
private static MavenProjectHierarchy getHierarchy(List hierarchy, MavenProject project) throws MojoExecutionException {
final MavenProject p = project;
return (MavenProjectHierarchy) CollectionUtils.find(hierarchy,
new Predicate() {
public boolean evaluate(Object o) {
return ((MavenProjectHierarchy) o).getProject().equals(p);
}
}
);
}
private static List getProjectModules(MavenProject project, List projects) throws MojoExecutionException {
List modules = new ArrayList();
ModulePomFileResolver resolver = null;
try {
resolver = resolverImpl.newInstance();
} catch (Exception e) {
throw new RuntimeException("Unable to instanciate pom module file resolver", e);
}
for (Object moduleNames : project.getModules()) {
String moduleName = (String) moduleNames;
File moduleFile = resolver.resolve(project, moduleName);
MavenProject module = null;
for (MavenProject potentialModule : projects) {
try {
if (potentialModule.getFile().getCanonicalFile().compareTo(moduleFile.getCanonicalFile()) == 0) {
module = potentialModule;
break;
}
} catch (IOException e) {
throw new MojoExecutionException("Unable to resolve canonical path of pom.xml", e);
}
}
if (module == null) {
throw new MojoExecutionException("Unable to find module " + moduleName + " pom.xml");
} else {
modules.add(module);
}
}
return modules;
}
public interface ModulePomFileResolver {
File resolve(MavenProject project, String moduleName);
}
public static class DefaultModuleFileResolver implements ModulePomFileResolver {
public File resolve(MavenProject project, String moduleName) {
return new File(project.getBasedir() + "/" + moduleName + "/pom.xml");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy