-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce properties manager for easy implements other properties for…
…mats
- Loading branch information
1 parent
8f4e6be
commit cd9eb53
Showing
17 changed files
with
470 additions
and
109 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
invoker.goals = clean process-resources |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
# test properties for IT | ||
|
||
props1 = value1 | ||
props2 = value2 | ||
props3 = value3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# test properties for IT | ||
|
||
props3 = value3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.codehaus.mojo.properties.it</groupId> | ||
<artifactId>write-project</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<build> | ||
|
||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
|
||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>properties-maven-plugin</artifactId> | ||
<version>@project.version@</version> | ||
<executions> | ||
<execution> | ||
<phase>generate-resources</phase> | ||
<goals> | ||
<goal>read-project-properties</goal> | ||
</goals> | ||
<configuration> | ||
<files> | ||
<file>main.properties</file> | ||
<!-- default manager for properties should be used --> | ||
<file>main2.txt</file> | ||
</files> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Test resource using properties from external files | ||
|
||
props1 has value ${props1} | ||
props2 has value ${props2} | ||
props3 has value ${props3} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
File testFile = new File( basedir, 'target/classes/test.txt' ) | ||
assert testFile.exists() | ||
|
||
String content = testFile.getText() | ||
assert content.contains('props1 has value value1') | ||
assert content.contains('props2 has value value2') | ||
assert content.contains('props3 has value value3') | ||
|
||
File buildLog = new File( basedir, 'build.log' ) | ||
assert buildLog.exists() | ||
assert buildLog.getText().contains("[WARNING] Unknown properties resource extension: 'txt' assume as: 'properties'") | ||
|
44 changes: 44 additions & 0 deletions
44
src/main/java/org/codehaus/mojo/properties/AbstractPropertiesMojo.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.codehaus.mojo.properties; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Optional; | ||
|
||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.codehaus.mojo.properties.managers.PropertiesManager; | ||
|
||
/** | ||
* Abstract Mojo with Properties managers support. | ||
*/ | ||
public abstract class AbstractPropertiesMojo extends AbstractMojo { | ||
|
||
private final List<PropertiesManager> propertiesManagers; | ||
|
||
protected AbstractPropertiesMojo(List<PropertiesManager> propertiesManagers) { | ||
this.propertiesManagers = propertiesManagers; | ||
} | ||
|
||
protected PropertiesManager getPropertiesManager(String resourceExtension) { | ||
getLog().debug("Available properties managers: " + propertiesManagers); | ||
|
||
String resourceExtensionLowerCase = resourceExtension.toLowerCase(Locale.ROOT); | ||
Optional<PropertiesManager> propertiesStore = propertiesManagers.stream() | ||
.filter(manager -> manager.isExtensionSupport(resourceExtensionLowerCase)) | ||
.findFirst(); | ||
|
||
if (!propertiesStore.isPresent()) { | ||
getLog().warn("Unknown properties resource extension: '" + resourceExtension + "' assume as: '" | ||
+ PropertiesManager.DEFAULT_MANAGER_EXTENSION + "'"); | ||
return getDefaultPropertiesManager(); | ||
} else { | ||
return propertiesStore.get(); | ||
} | ||
} | ||
|
||
private PropertiesManager getDefaultPropertiesManager() { | ||
return propertiesManagers.stream() | ||
.filter(manager -> manager.isExtensionSupport(PropertiesManager.DEFAULT_MANAGER_EXTENSION)) | ||
.findFirst() | ||
.orElseThrow(() -> new RuntimeException("Default properties manager not exist")); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,75 +19,56 @@ | |
* under the License. | ||
*/ | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.apache.maven.project.MavenProject; | ||
import org.codehaus.mojo.properties.managers.PropertiesManager; | ||
import org.codehaus.plexus.util.FileUtils; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Zarar Siddiqi</a> | ||
*/ | ||
public abstract class AbstractWritePropertiesMojo extends AbstractMojo { | ||
public abstract class AbstractWritePropertiesMojo extends AbstractPropertiesMojo { | ||
|
||
@Parameter(defaultValue = "${project}", required = true, readonly = true) | ||
private MavenProject project; | ||
|
||
/** | ||
* Output file for storing properties. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
@Parameter(required = true, property = "properties.outputFile") | ||
private File outputFile; | ||
|
||
protected AbstractWritePropertiesMojo(List<PropertiesManager> propertiesManagers) { | ||
super(propertiesManagers); | ||
} | ||
|
||
/** | ||
* @param properties {@link Properties} | ||
* @param file {@link File} | ||
* @throws MojoExecutionException {@link MojoExecutionException} | ||
*/ | ||
protected void writeProperties(Properties properties, File file) throws MojoExecutionException { | ||
protected void writeProperties(Properties properties) throws MojoExecutionException { | ||
try { | ||
storeWithoutTimestamp(properties, file, "Properties"); | ||
PropertiesManager manager = getPropertiesManager(FileUtils.extension(outputFile.getName())); | ||
manager.save(properties, Files.newOutputStream(outputFile.toPath()), "Properties"); | ||
} catch (FileNotFoundException e) { | ||
getLog().error("Could not create FileOutputStream: " + file); | ||
getLog().error("Could not create FileOutputStream: " + outputFile); | ||
throw new MojoExecutionException(e.getMessage(), e); | ||
} catch (IOException e) { | ||
getLog().error("Error writing properties: " + file); | ||
getLog().error("Error writing properties: " + outputFile); | ||
throw new MojoExecutionException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
// https://github.com/apache/maven-archiver/blob/master/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java#L81 | ||
private void storeWithoutTimestamp(Properties properties, File outputFile, String comments) throws IOException { | ||
try (PrintWriter pw = new PrintWriter(outputFile, "ISO-8859-1"); | ||
StringWriter sw = new StringWriter()) { | ||
properties.store(sw, comments); | ||
comments = '#' + comments; | ||
|
||
List<String> lines = new ArrayList<>(); | ||
try (BufferedReader r = new BufferedReader(new StringReader(sw.toString()))) { | ||
String line; | ||
while ((line = r.readLine()) != null) { | ||
if (!line.startsWith("#") || line.equals(comments)) { | ||
lines.add(line); | ||
} | ||
} | ||
} | ||
|
||
Collections.sort(lines); | ||
for (String l : lines) { | ||
pw.println(l); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @throws MojoExecutionException {@link MojoExecutionException} | ||
*/ | ||
|
@@ -107,11 +88,4 @@ protected void validateOutputFile() throws MojoExecutionException { | |
public MavenProject getProject() { | ||
return project; | ||
} | ||
|
||
/** | ||
* @return {@link #outputFile} | ||
*/ | ||
public File getOutputFile() { | ||
return outputFile; | ||
} | ||
} |
Oops, something went wrong.