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

excludes configuration was developed #68

Open
wants to merge 2 commits 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: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
<name>Tim Gordon</name>
<email>timothygordon32@gmail.com</email>
</developer>
<developer>
<id>peter@verhas.com</id>
<name>Peter Verhas</name>
<email>peter@verhas.com</email>
</developer>
</developers>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,103 +19,108 @@
* under the License.
*/

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 java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;

/*
* 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.
*/

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 java.util.regex.Pattern;

/**
* @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
*/
public abstract class AbstractWritePropertiesMojo
extends AbstractMojo
{
extends AbstractMojo {

@Parameter( defaultValue = "${project}", required = true, readonly = true )
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;

@Parameter( required = true )
@Parameter(required = true, property = "properties.outputFile")
private File outputFile;

@Parameter()
private List<String> excludes;

@Parameter(property = "properties.exclusionRegex")
private String exclusionRegex;

@Parameter(defaultValue = "true", property = "properties.generateCommentHeader")
private boolean generateCommentHeader;

/**
* @param properties {@link Properties}
* @param file {@link File}
* @throws MojoExecutionException {@link MojoExecutionException}
* Filter the properties out, which are listed in the exclude list.
*
* @param properties all the properties
* @return the filtered properties
*/
protected void writeProperties( Properties properties, File file )
throws MojoExecutionException
{
try ( FileOutputStream fos = new FileOutputStream( file ) )
{
properties.store( fos, "Properties" );
// this whole method was written by GitHub CoPilot, I only wrote the method header
protected Properties filterProperties(Properties properties) {
final Properties filteredProperties = new Properties();
final Pattern exclusionPattern;
if (exclusionRegex != null) {
exclusionPattern = Pattern.compile(exclusionRegex);

} else {
exclusionPattern = null;
}
catch ( FileNotFoundException e )
{
getLog().error( "Could not create FileOutputStream: " + file );
throw new MojoExecutionException( e.getMessage(), e );
for (String key : properties.stringPropertyNames()) {
if ((excludes == null || !excludes.contains(key)) && (exclusionPattern == null || !exclusionPattern.matcher(key).find())) {
filteredProperties.put(key, properties.getProperty(key));
}
}
catch ( IOException e )
{
getLog().error( "Error writing properties: " + file );
throw new MojoExecutionException( e.getMessage(), e );
return filteredProperties;
}

/**
* @param properties {@link Properties}
* @param file {@link File}
* @throws MojoExecutionException {@link MojoExecutionException}
*/
protected void writeProperties(Properties properties, File file)
throws MojoExecutionException {
try (OutputStream fos = generateCommentHeader ? new FileOutputStream(file) : new SkipFileOutputStream(new FileOutputStream(file))) {
properties.store(fos, "Properties");
} catch (FileNotFoundException e) {
getLog().error("Could not create FileOutputStream: " + file);
throw new MojoExecutionException(e.getMessage(), e);
} catch (IOException e) {
getLog().error("Error writing properties: " + file);
throw new MojoExecutionException(e.getMessage(), e);
}
}

/**
* @throws MojoExecutionException {@link MojoExecutionException}
*/
protected void validateOutputFile()
throws MojoExecutionException
{
if ( outputFile.isDirectory() )
{
throw new MojoExecutionException( "outputFile must be a file and not a directory" );
throws MojoExecutionException {
if (outputFile.isDirectory()) {
throw new MojoExecutionException("outputFile must be a file and not a directory");
}
// ensure path exists
if ( outputFile.getParentFile() != null )
{
if (outputFile.getParentFile() != null) {
outputFile.getParentFile().mkdirs();
}
}

/**
* @return {@link MavenProject}
*/
public MavenProject getProject()
{
public MavenProject getProject() {
return project;
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.codehaus.mojo.properties;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class SkipFileOutputStream extends FilterOutputStream {

private boolean filter = true;
private boolean headerLineJustEnded = false;
private StringBuilder sb = new StringBuilder();

public SkipFileOutputStream(final OutputStream out) {
super(out);
}

@Override
public void write(final int b) throws IOException {
if (filter) {
switch (b) {
case '\n':
case '\r':
headerLineJustEnded = true;
sb.setLength(0);
break;
case '#':
if (headerLineJustEnded) {
headerLineJustEnded = false;
sb.setLength(0);
}
case ' ':
case '\t':
sb.append(b);
break;
default:
if (headerLineJustEnded) {
filter = false;
super.write(sb.toString().getBytes(StandardCharsets.UTF_8));
sb.setLength(0);
super.write(b);
}
}
} else {
super.write(b);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public void execute()
}
}

writeProperties( properties, getOutputFile() );
writeProperties( filterProperties(properties), getOutputFile() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ public void execute()

}

writeProperties( projProperties, getOutputFile() );
writeProperties( filterProperties(projProperties), getOutputFile() );
}
}
6 changes: 6 additions & 0 deletions src/site/apt/usage.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ Usage
<outputFile>
\${project.build.outputDirectory}/app.properties
</outputFile>
<excludes>
<exclude>gpg.password</exclude>
</excludes>
</configuration>
</execution>
</executions>
Expand Down Expand Up @@ -126,6 +129,9 @@ Usage
<outputFile>
\${project.build.outputDirectory}/app.properties
</outputFile>
<excludes>
<exclude>gpg.password</exclude>
</excludes>
</configuration>
</execution>
</executions>
Expand Down