io.codemodder.plugins.maven.operator.ProjectModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of codemodder-plugin-maven Show documentation
Show all versions of codemodder-plugin-maven Show documentation
Plugin for providing Maven dependency management functions to codemods.
package io.codemodder.plugins.maven.operator;
import java.nio.file.Path;
import java.util.*;
import org.dom4j.Element;
import org.dom4j.Node;
/** ProjectModel represents the input parameters for the chain */
public class ProjectModel {
private POMDocument pomFile;
private List parentPomFiles;
private Dependency dependency;
private boolean skipIfNewer;
private boolean useProperties;
private Set activeProfiles;
private boolean overrideIfAlreadyExists;
private QueryType queryType;
private Path repositoryPath;
private String finishedByClass;
private boolean modifiedByCommand;
/**
* Constructs a new ProjectModel instance with the specified parameters.
*
* @param pomFile The POMDocument representing the main POM file.
* @param parentPomFiles A list of POMDocuments representing parent POM files.
* @param dependency The Dependency object to operate on.
* @param skipIfNewer Whether to skip the operation if the dependency is newer.
* @param useProperties Whether to use properties during the operation.
* @param activeProfiles A set of active profiles to consider during property resolution.
* @param overrideIfAlreadyExists Whether to override the dependency if it already exists.
* @param queryType The type of query operation to perform.
* @param repositoryPath The path to the repository.
* @param finishedByClass The name of the class that finished the operation.
*/
public ProjectModel(
POMDocument pomFile,
List parentPomFiles,
Dependency dependency,
boolean skipIfNewer,
boolean useProperties,
Set activeProfiles,
boolean overrideIfAlreadyExists,
QueryType queryType,
Path repositoryPath,
String finishedByClass) {
this.pomFile = pomFile;
this.parentPomFiles = parentPomFiles != null ? parentPomFiles : Collections.emptyList();
this.dependency = dependency;
this.skipIfNewer = skipIfNewer;
this.useProperties = useProperties;
this.activeProfiles = activeProfiles;
this.overrideIfAlreadyExists = overrideIfAlreadyExists;
this.queryType = queryType != null ? queryType : QueryType.NONE;
this.repositoryPath = repositoryPath;
this.finishedByClass = finishedByClass;
this.modifiedByCommand = false;
}
/**
* Returns a map of properties defined on the root of the given POMDocument.
*
* @param pomFile The POMDocument to extract properties from.
* @return A map of property names and their values.
*/
public static Map propertiesDefinedOnPomDocument(POMDocument pomFile) {
Map rootProperties = new HashMap<>();
List propertyElements =
pomFile.getPomDocument().getRootElement().elements("properties");
for (Element element : propertyElements) {
List elements = element.elements();
for (Element propertyElement : elements) {
rootProperties.put(propertyElement.getName(), propertyElement.getText());
}
}
return rootProperties;
}
private Map getPropertiesFromProfile(String profileName, POMDocument pomFile) {
String expression =
"/m:project/m:profiles/m:profile[./m:id[text()='" + profileName + "']]/m:properties";
List propertiesElements = Util.selectXPathNodes(pomFile.getPomDocument(), expression);
Map newPropertiesToAppend = new HashMap<>();
for (Node element : propertiesElements) {
if (element instanceof Element) {
List elements = ((Element) element).elements();
for (Element propertyElement : elements) {
newPropertiesToAppend.put(propertyElement.getName(), propertyElement.getText());
}
}
}
return newPropertiesToAppend;
}
/**
* Returns a map of properties defined in various POM files based on their names.
*
* @return A map where keys are property names, and values are lists of pairs containing the
* property value and the corresponding POMDocument.
*/
public Map>> propertiesDefinedByFile() {
Map>> result = new LinkedHashMap<>();
List allPomFiles = allPomFiles();
for (POMDocument pomFile : allPomFiles) {
Map rootProperties = propertiesDefinedOnPomDocument(pomFile);
Map tempProperties = new LinkedHashMap<>(rootProperties);
List activatedProfiles = new ArrayList<>();
for (String profile : activeProfiles) {
if (!profile.startsWith("!")) {
activatedProfiles.add(profile);
}
}
List