Created
November 21, 2012 10:19
-
-
Save khmarbaise/4124126 to your computer and use it in GitHub Desktop.
Own Maven resource filtering plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Mojo(name = "configurator", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresProject = true) | |
public class ConfiguratorMojo extends AbstractMojo { | |
@Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}") | |
protected String encoding; | |
/** | |
* The current Maven project. | |
*/ | |
@Component | |
private MavenProject project; | |
/** | |
* The output directory into which to copy the resources. | |
*/ | |
@Parameter(defaultValue = "${project.build.outputDirectory}", required = true) | |
private File outputDirectory; | |
/** | |
* The list of resources we want to transfer. | |
*/ | |
@Parameter(required = true) | |
private List<Resource> resources; | |
@Component | |
private MavenSession mavenSession; | |
@Parameter(defaultValue = "${project.build.filters}", readonly = true) | |
protected List<String> buildFilters; | |
@Parameter | |
private List<String> filters; | |
@Parameter | |
private List<String> nonFilteredFileExtensions; | |
/** | |
* <p> | |
* Set of delimiters for expressions to filter within the resources. These | |
* delimiters are specified in the form 'beginToken*endToken'. If no '*' is | |
* given, the delimiter is assumed to be the same for start and end. | |
* </p> | |
* <p> | |
* So, the default filtering delimiters might be specified as: | |
* </p> | |
* | |
* <pre> | |
* <delimiters> | |
* <delimiter>${*}</delimiter> | |
* <delimiter>@</delimiter> | |
* </delimiters> | |
* </pre> | |
* <p> | |
* Since the '@' delimiter is the same on both ends, we don't need to | |
* specify '@*@' (though we can). | |
* </p> | |
* | |
* @since 2.4 | |
*/ | |
@Parameter | |
protected List<String> delimiters; | |
/** | |
* <p> | |
* List of plexus components hint which implements | |
* {@link MavenResourcesFiltering#filterResources(MavenResourcesExecution)}. | |
* They will be executed after the resources copying/filtering. | |
* </p> | |
* | |
* @since 2.4 | |
*/ | |
@Parameter | |
private List<String> mavenFilteringHints; | |
private List<MavenResourcesFiltering> mavenFilteringComponents = new ArrayList<MavenResourcesFiltering>(); | |
/** | |
* The configuration for the configuration file which will be read. | |
* | |
*/ | |
@Parameter(defaultValue = "${project.build.outputDirectory}", required = true) | |
private String configuration; | |
@Parameter(defaultValue = "WILDCARD") | |
private String scope; | |
public String getScope() { | |
return scope; | |
} | |
@Component(role = org.apache.maven.shared.filtering.MavenResourcesFiltering.class) | |
private MavenResourcesFiltering mavenResourcesFiltering; | |
/** | |
* Determines whether filtering has been enabled for any resource. | |
* | |
* @param resources | |
* The set of resources to check for filtering, may be | |
* <code>null</code>. | |
* @return <code>true</code> if at least one resource uses filtering, | |
* <code>false</code> otherwise. | |
*/ | |
private boolean isFilteringEnabled(Collection<Resource> resources) { | |
if (resources != null) { | |
for (Resource resource : resources) { | |
if (resource.isFiltering()) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see org.apache.maven.plugin.Mojo#execute() | |
*/ | |
public void execute() throws MojoExecutionException { | |
if (StringUtils.isEmpty(encoding) && isFilteringEnabled(getResources())) { | |
getLog().warn( | |
"File encoding has not been set, using platform encoding " | |
+ ReaderFactory.FILE_ENCODING | |
+ ", i.e. build is platform dependent!"); | |
} | |
Scope scope = convertToScope(getScope()); | |
getLog().info("Hallo welt. (" + scope + ")"); | |
if (getResources() != null) { | |
for (Resource item : getResources()) { | |
getLog().info(" --| Resource: " + item.getFiltering() + "|" + item.getDirectory()); | |
} | |
} | |
try { | |
MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution( | |
resources, outputDirectory, project, encoding, null, | |
nonFilteredFileExtensions, mavenSession); | |
ValueSource valueSource = new ValueSource() { | |
@Override | |
public Object getValue(String expression) { | |
getLog().info("Expression: " + expression); | |
return "XXX"; | |
} | |
@Override | |
public List getFeedback() { | |
getLog().info("getFeedback()"); | |
return Collections.EMPTY_LIST; | |
} | |
@Override | |
public void clearFeedback() { | |
// TODO Auto-generated method stub | |
getLog().info("clearFeedback()"); | |
} | |
}; | |
mavenResourcesExecution.addFilerWrapperWithEscaping(valueSource, | |
"\\@", "\\@", "@", false); | |
mavenResourcesExecution.setUseDefaultFilterWrappers(false); | |
mavenResourcesFiltering.filterResources(mavenResourcesExecution); | |
} catch (MavenFilteringException e) { | |
throw new MojoExecutionException(e.getMessage(), e); | |
} | |
} | |
public String getEncoding() { | |
return encoding; | |
} | |
public MavenProject getProject() { | |
return project; | |
} | |
public File getOutputDirectory() { | |
return outputDirectory; | |
} | |
public List<Resource> getResources() { | |
return resources; | |
} | |
public MavenSession getMavenSession() { | |
return mavenSession; | |
} | |
public List<String> getFilters() { | |
return filters; | |
} | |
public List<String> getNonFilteredFileExtensions() { | |
return nonFilteredFileExtensions; | |
} | |
public List<String> getDelimiters() { | |
return delimiters; | |
} | |
public String getConfiguration() { | |
return configuration; | |
} | |
public MavenResourcesFiltering getMavenResourcesFiltering() { | |
return mavenResourcesFiltering; | |
} | |
private Scope convertToScope(String value) { | |
if ("WILDCARD".equalsIgnoreCase(value)) { | |
return Scope.WILDCARD; | |
} else { | |
return new Scope(value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment