Skip to content

Commit

Permalink
Implemented external property files as patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
antonio-petricca committed Nov 13, 2023
1 parent 45d84fb commit 93160e0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public class MavenFileMergerMojo extends AbstractMojo {
@Component(role = DefaultMavenReaderFilter.class)
protected DefaultMavenReaderFilter defaultMavenReaderFilter;

@Component(role = MavenProject.class)
@Parameter(defaultValue = "${project}", readonly = true)
MavenProject mavenProject;

@Component(role = MavenSession.class)
@Parameter(defaultValue = "${session}", readonly = true)
MavenSession mavenSession;

@Parameter(required = false)
Expand Down Expand Up @@ -206,8 +206,12 @@ private Properties getPropertiesConfiguration(String setId)
.getProperties();

if (propertiesSet.hasPropertyFiles()) {
for (File file : propertiesSet.getPropertyFiles()) {
Properties fileProperties = loadProperties(file);
String[] propertyFiles = scanForFiles(
propertiesSet.getPropertyFilePatterns()
);

for (String propertyFile : propertyFiles) {
Properties fileProperties = loadProperties(propertyFile);
properties.putAll(fileProperties);
}
}
Expand Down Expand Up @@ -244,9 +248,15 @@ private SourceFile[] getSourceFilesConfiguration(String setId)
.getSourceFiles();
}

private Properties loadProperties(File file)
private Properties loadProperties(String propertyFile)
throws IOException, MavenFilteringException
{
log.info(String.format(
"Loading property file \"%s\"...",
propertyFile
));

File file = new File(propertyFile);
String fileExt = FilenameUtils.getExtension(file.getName());
FileInputStream inputStream = new FileInputStream(file);
Properties properties = new Properties();
Expand Down Expand Up @@ -378,30 +388,17 @@ private void mergeTargetFile(
{
String[] templateFilePatterns = targetFileConfiguration.getTemplateFilePatterns();

log.info(String.format(
"Merging template file(s) \"%s\"...",
templateFilePatterns.toString()
));
log.info("Merging template file(s)...");

targetFileConfiguration.validate();

boolean copyPermissions = targetFileConfiguration.isCopyPermissions();
String indentation = getIndentation(targetFileConfiguration);
Properties propertiesBackup = setProperties(properties);
Charset targetCharset = getCharset(targetFileConfiguration);
String targetFolderName = targetFileConfiguration.getTargetFolder();
File targetFolder = new File(targetFolderName);

DirectoryScanner directoryScanner = new DirectoryScanner();

directoryScanner.setBasedir(".");
directoryScanner.setCaseSensitive(true);
directoryScanner.setFollowSymlinks(false);
directoryScanner.setIncludes(templateFilePatterns);

directoryScanner.scan();

String[] resolvedTemplateFiles = directoryScanner.getIncludedFiles();
boolean copyPermissions = targetFileConfiguration.isCopyPermissions();
String indentation = getIndentation(targetFileConfiguration);
Properties propertiesBackup = setProperties(properties);
Charset targetCharset = getCharset(targetFileConfiguration);
String targetFolderName = targetFileConfiguration.getTargetFolder();
File targetFolder = new File(targetFolderName);
String[] resolvedTemplateFiles = scanForFiles(templateFilePatterns);

if ((null == templateFilePatterns) || (0 == templateFilePatterns.length)) {
log.warn("No template files found.");
Expand All @@ -424,6 +421,19 @@ private void mergeTargetFile(
setProperties(propertiesBackup);
}

private String[] scanForFiles(String[] filePatterns) {
DirectoryScanner directoryScanner = new DirectoryScanner();

directoryScanner.setBasedir(".");
directoryScanner.setCaseSensitive(true);
directoryScanner.setFollowSymlinks(false);
directoryScanner.setIncludes(filePatterns);

directoryScanner.scan();

return directoryScanner.getIncludedFiles();
}

private Properties setProperties(Properties newProperties) {
Properties currentProperties = mavenProject.getProperties();
Properties backupProperties = (Properties)currentProperties.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
public class PropertiesSet extends AbstractSet {

private Properties properties;
private File[] propertyFiles;
private String[] propertyFilePatterns;

public Properties getProperties() {
return properties;
}

public File[] getPropertyFiles() {
return propertyFiles;
public String[] getPropertyFilePatterns() {
return propertyFilePatterns;
}

public boolean hasPropertyFiles() {
return ((null != propertyFiles) && (propertyFiles.length > 0));
return ((null != propertyFilePatterns ) && ( propertyFilePatterns.length > 0));
}

@Override
Expand All @@ -28,13 +28,13 @@ public void validate()
{
super.validate();

if ((null != propertyFiles) && (propertyFiles.length > 0)) {
for (File propertyFile : propertyFiles) {
if ((null != propertyFilePatterns ) && ( propertyFilePatterns.length > 0)) {
for (String propertyFilePattern : propertyFilePatterns) {
validate(
(propertyFile.exists() && propertyFile.isFile()),
((null != propertyFilePattern) && !propertyFilePattern.isEmpty()),
String.format(
"File \"%s\" not found",
propertyFile.getAbsolutePath()
"Property file pattern \"%s\" null or empty.",
propertyFilePattern
)
);
}
Expand Down
8 changes: 4 additions & 4 deletions tester/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
<plugin.tester.environment>development</plugin.tester.environment>
</properties>

<propertyFiles>
<propertyFile>${property-files-path}/external.properties</propertyFile>
<propertyFile>${property-files-path}/external.xml</propertyFile>
</propertyFiles>
<propertyFilePatterns>
<propertyFilePattern>${property-files-path}/*.properties</propertyFilePattern>
<propertyFilePattern>${property-files-path}/*.xml</propertyFilePattern>
</propertyFilePatterns>

</propertiesSet>
</propertiesSets>
Expand Down

0 comments on commit 93160e0

Please sign in to comment.