Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new mode: 'replace' #368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/org/codehaus/mojo/flatten/ElementHandling.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@ public enum ElementHandling {
keep,

/** Remove the element entirely so it will not be present in flattened POM. */
remove
remove,

/** Replace the element with a replacement value that can be set in the configuration. */
replace
}
68 changes: 43 additions & 25 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -357,6 +361,14 @@ public class FlattenMojo extends AbstractFlattenMojo {
@Parameter(property = "flatten.dependency.keepComments", required = false, defaultValue = "false")
private boolean keepCommentsInPom;

/**
* A map of replacement values for <code>replace</code> {@link ElementHandling} elements.
*
* @since 1.5.1
*/
@Parameter
private Map<String, String> replacementValues = new HashMap<>();

@Inject
private DirectDependenciesInheritanceAssembler inheritanceAssembler;

Expand Down Expand Up @@ -561,12 +573,23 @@ protected Model createFlattenedPom(File pomFile) throws MojoExecutionException,
Model resolvedPom = this.project.getModel();
Model interpolatedPom = createResolvedPom(buildingRequest);

final Model replacementPom = createReplacementPom();

Model[] poms = new Model[] {
cleanPom, // ElementHandling.flatten
effectivePom, // ElementHandling.expand
resolvedPom, // ElementHandling.resolve
interpolatedPom, // ElementHandling.interpolate
originalPom, // ElementHandling.keep
null, // ElementHandling.remove
replacementPom // ElementHandling.flatten
};

// copy the configured additional POM elements...

for (PomProperty<?> property : PomProperty.getPomProperties()) {
if (property.isElement()) {
Model sourceModel = getSourceModel(
descriptor, property, effectivePom, originalPom, resolvedPom, interpolatedPom, cleanPom);
Model sourceModel = getSourceModel(descriptor, property, poms);
if (sourceModel == null) {
if (property.isRequired()) {
throw new MojoFailureException(
Expand All @@ -581,6 +604,19 @@ protected Model createFlattenedPom(File pomFile) throws MojoExecutionException,
return flattenedPom;
}

private Model createReplacementPom() throws MojoExecutionException {
Model replacementPom = new Model();
for (Map.Entry<String, String> entry : replacementValues.entrySet()) {
try {
Method setter = new PropertyDescriptor(entry.getKey(), Model.class).getWriteMethod();
setter.invoke(replacementPom, entry.getValue());
} catch (IntrospectionException | ReflectiveOperationException e) {
throw new MojoExecutionException("could not set replacement value for '" + entry.getKey() + "'", e);
}
}
return replacementPom;
}

private Model createResolvedPom(ModelBuildingRequest buildingRequest) throws MojoExecutionException {
LoggingModelProblemCollector problems = new LoggingModelProblemCollector(getLog());
Model originalModel = getOriginalModel();
Expand Down Expand Up @@ -697,33 +733,15 @@ protected Model createCleanPom(Model effectivePom) throws MojoExecutionException
return cleanPom;
}

private Model getSourceModel(
FlattenDescriptor descriptor,
PomProperty<?> property,
Model effectivePom,
Model originalPom,
Model resolvedPom,
Model interpolatedPom,
Model cleanPom) {
private Model getSourceModel(FlattenDescriptor descriptor, PomProperty<?> property, Model[] poms) {

ElementHandling handling = descriptor.getHandling(property);
getLog().debug("Property " + property.getName() + " will be handled using " + handling + " in flattened POM.");
switch (handling) {
case expand:
return effectivePom;
case keep:
return originalPom;
case resolve:
return resolvedPom;
case interpolate:
return interpolatedPom;
case flatten:
return cleanPom;
case remove:
return null;
default:
throw new IllegalStateException(handling.toString());

if (handling.ordinal() >= poms.length) {
throw new IllegalStateException(handling.toString());
}
return poms[handling.ordinal()];
}

/**
Expand Down