Skip to content

Commit

Permalink
Introduce properties manager for easy implements other properties for…
Browse files Browse the repository at this point in the history
…mats
  • Loading branch information
slawekjaranowski committed Oct 22, 2023
1 parent 8f4e6be commit cd9eb53
Show file tree
Hide file tree
Showing 17 changed files with 470 additions and 109 deletions.
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.inject</artifactId>
<version>0.9.0.M2</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<profiles>
<profile>
<id>run-its</id>
Expand Down
1 change: 1 addition & 0 deletions src/it/read-project/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = clean process-resources
6 changes: 6 additions & 0 deletions src/it/read-project/main.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# test properties for IT

props1 = value1
props2 = value2
props3 = value3
4 changes: 4 additions & 0 deletions src/it/read-project/main2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# test properties for IT

props3 = value3
42 changes: 42 additions & 0 deletions src/it/read-project/pom.xml
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>
5 changes: 5 additions & 0 deletions src/it/read-project/src/main/resources/test.txt
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}
30 changes: 30 additions & 0 deletions src/it/read-project/verify.groovy
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'")

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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -107,11 +88,4 @@ protected void validateOutputFile() throws MojoExecutionException {
public MavenProject getProject() {
return project;
}

/**
* @return {@link #outputFile}
*/
public File getOutputFile() {
return outputFile;
}
}
Loading

0 comments on commit cd9eb53

Please sign in to comment.