Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.paypal.butterfly.utilities.operations.pom;
import com.paypal.butterfly.extensions.api.TOExecutionResult;
import com.paypal.butterfly.extensions.api.TransformationUtility;
import com.paypal.butterfly.extensions.api.exception.TransformationOperationException;
import com.paypal.butterfly.extensions.api.operations.ChangeOrRemoveElement;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
/**
* Replaces a dependency by another one in a POM file.
* If no new scope is defined, the scope set in the replacement dependency
* will be the same used in the dependency to be replaced.
* If the POM file does not have the specified dependency to be replaced, the operation will return an error.
* That behavior can be changed though, see {@link ChangeOrRemoveElement} for further details.
*
* @author facarvalho
*/
public class PomReplaceDependency extends AbstractArtifactPomOperation implements ChangeOrRemoveElement {
// TODO
// Add pre-validation to check, in case newVersion was not set, if dependency
// is managed or not. If not, fail if no version has been set!
private static final String DESCRIPTION = "Replace dependency %s:%s by %s:%s in POM file %s";
private String newGroupId;
private String newArtifactId;
private String newVersion;
private String newScope;
private IfNotPresent ifNotPresent = IfNotPresent.Fail;
public PomReplaceDependency() {
}
/**
* Operation to replace a dependency by another one in a POM file
*
* @param groupId group id of the dependency to be replaced
* @param artifactId artifact id of the dependency to be replaced
* @param newGroupId group id of the new dependency
* @param newArtifactId artifact id of the new dependency
*/
public PomReplaceDependency(String groupId, String artifactId, String newGroupId, String newArtifactId) {
setGroupId(groupId);
setArtifactId(artifactId);
setNewGroupId(newGroupId);
setNewArtifactId(newArtifactId);
}
/**
* Operation to replace a dependency by another one in a POM file
*
* @param groupId group id of the dependency to be replaced
* @param artifactId artifact id of the dependency to be replaced
* @param newGroupId group id of the new dependency
* @param newArtifactId artifact id of the new dependency
* @param newVersion version of the new dependency
*/
public PomReplaceDependency(String groupId, String artifactId, String newGroupId, String newArtifactId, String newVersion) {
this(groupId, artifactId, newGroupId, newArtifactId);
setNewVersion(newVersion);
}
/**
* Operation to replace a dependency by another one in a POM file
*
* @param groupId group id of the dependency to be replaced
* @param artifactId artifact id of the dependency to be replaced
* @param newGroupId group id of the new dependency
* @param newArtifactId artifact id of the new dependency
* @param newVersion version of the new dependency
* @param newScope version of the new dependency
*/
public PomReplaceDependency(String groupId, String artifactId, String newGroupId, String newArtifactId, String newVersion, String newScope) {
this(groupId, artifactId, newGroupId, newArtifactId, newVersion);
setNewScope(newScope);
}
public PomReplaceDependency setNewGroupId(String newGroupId) {
TransformationUtility.checkForBlankString("newGroupId", newGroupId);
this.newGroupId = newGroupId;
return this;
}
public PomReplaceDependency setNewArtifactId(String newArtifactId) {
TransformationUtility.checkForBlankString("newArtifactId", newArtifactId);
this.newArtifactId = newArtifactId;
return this;
}
public PomReplaceDependency setNewVersion(String newVersion) {
TransformationUtility.checkForEmptyString("newVersion", newVersion);
this.newVersion = newVersion;
return this;
}
public PomReplaceDependency setNewScope(String newScope) {
TransformationUtility.checkForEmptyString("newScope", newScope);
this.newScope = newScope;
return this;
}
@Override
public PomReplaceDependency failIfNotPresent() {
ifNotPresent = IfNotPresent.Fail;
return this;
}
@Override
public PomReplaceDependency warnIfNotPresent() {
ifNotPresent = IfNotPresent.Warn;
return this;
}
@Override
public PomReplaceDependency noOpIfNotPresent() {
ifNotPresent = IfNotPresent.NoOp;
return this;
}
public String getNewGroupId() {
return newGroupId;
}
public String getNewArtifactId() {
return newArtifactId;
}
public String getNewVersion() {
return newVersion;
}
public String getNewScope() {
return newScope;
}
@Override
public String getDescription() {
return String.format(DESCRIPTION, groupId, artifactId, newGroupId, newArtifactId, getRelativePath());
}
@Override
protected TOExecutionResult pomExecution(String relativePomFile, Model model) {
TOExecutionResult result = null;
String details;
Dependency oldDependency = getDependency(model, groupId, artifactId);
if (oldDependency != null) {
model.removeDependency(oldDependency);
Dependency newDependency = new Dependency();
newDependency.setGroupId(newGroupId);
newDependency.setArtifactId(newArtifactId);
if (newVersion != null) {
newDependency.setVersion(newVersion);
}
if (newScope != null) {
newDependency.setScope(newScope);
} else {
newDependency.setScope(oldDependency.getScope());
}
model.addDependency(newDependency);
details = String.format("Dependency %s:%s has been replaced by %s:%s in POM file %s", groupId, artifactId, newGroupId, newArtifactId, relativePomFile);
result = TOExecutionResult.success(this, details);
} else {
details = String.format("Dependency %s:%s has not been replaced by %s:%s in POM file %s because it is not present", groupId, artifactId, newGroupId, newArtifactId, relativePomFile);
switch (ifNotPresent) {
case Warn:
result = TOExecutionResult.warning(this, new TransformationOperationException(details));
break;
case NoOp:
result = TOExecutionResult.noOp(this, details);
break;
case Fail:
// Fail is the default
default:
result = TOExecutionResult.error(this, new TransformationOperationException(details));
break;
}
}
return result;
}
}