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

Include/exclude project properties #126

Merged
merged 5 commits into from
Apr 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</parent>

<artifactId>properties-maven-plugin</artifactId>
<version>1.2.2-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>

<packaging>maven-plugin</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,20 @@ protected void validateOutputFile() throws MojoExecutionException {
public MavenProject getProject() {
return project;
}

/**
* Default scope for test access.
*
* @param project The test project.
*/
void setProject(MavenProject project) {
this.project = project;
}

/**
* Default scope for test access.
*/
void setOutputFile(File outputFile) {
this.outputFile = outputFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@

import javax.inject.Inject;

import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.properties.managers.PropertiesManager;

/**
Expand All @@ -40,6 +44,20 @@
@Mojo(name = "write-project-properties", defaultPhase = LifecyclePhase.NONE, threadSafe = true)
public class WriteProjectProperties extends AbstractWritePropertiesMojo {

/**
* Property keys to exclude.
* @since 1.3.0
*/
@Parameter(property = "properties.excludedPropertyKeys")
private Set<String> excludedPropertyKeys;

/**
* Property keys to include.
* @since 1.3.0
*/
@Parameter(property = "properties.includedPropertyKeys")
private Set<String> includedPropertyKeys;

/**
* Default constructor
*
Expand Down Expand Up @@ -68,6 +86,28 @@ public void execute() throws MojoExecutionException {
}
}

Optional.ofNullable(excludedPropertyKeys)
.orElseGet(Collections::emptySet)
.forEach(projProperties::remove);

if (includedPropertyKeys != null && !includedPropertyKeys.isEmpty()) {
projProperties.keySet().removeIf(key -> !includedPropertyKeys.contains(String.valueOf(key)));
}

writeProperties(projProperties);
}

/**
* Default scope for test access.
*/
void setExcludedPropertyKeys(Set<String> excludedPropertyKeys) {
this.excludedPropertyKeys = excludedPropertyKeys;
}

/**
* Default scope for test access.
*/
void setIncludedPropertyKeys(Set<String> includedPropertyKeys) {
this.includedPropertyKeys = includedPropertyKeys;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.codehaus.mojo.properties;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.Properties;
import java.util.UUID;

import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.properties.managers.JdkPropertiesManager;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class WritePropertiesMojoTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private MavenProject projectStub;
private WriteProjectProperties writeProjectProperties;
private File outputFile;

@Before
public void setUp() throws IOException {
projectStub = new MavenProject();
writeProjectProperties = new WriteProjectProperties(Collections.singletonList(new JdkPropertiesManager()));
writeProjectProperties.setProject(projectStub);
outputFile = temporaryFolder.newFile();
writeProjectProperties.setOutputFile(outputFile);
}

@Test
public void projectPropertiesAreWritten() throws Exception {
String propertyKey = UUID.randomUUID().toString();
projectStub.getProperties().put(propertyKey, "foo");

writeProjectProperties.execute();

try (FileReader fr = new FileReader(outputFile)) {
Properties writtenProperties = new Properties();
writtenProperties.load(fr);

assertEquals("foo", writtenProperties.getProperty(propertyKey));
}
}

@Test
public void onlyIncludedPropertiesAreWritten() throws Exception {
String includedKey = UUID.randomUUID().toString();
projectStub.getProperties().put(includedKey, "foo");
String excludedKey = UUID.randomUUID().toString();
projectStub.getProperties().put(excludedKey, "foo");

writeProjectProperties.setIncludedPropertyKeys(Collections.singleton(includedKey));
writeProjectProperties.execute();

try (FileReader fr = new FileReader(outputFile)) {
Properties writtenProperties = new Properties();
writtenProperties.load(fr);

assertEquals("foo", writtenProperties.getProperty(includedKey));
assertFalse(writtenProperties.contains(excludedKey));
}
}

@Test
public void onlyNonExcludedPropertiesAreWritten() throws Exception {
String includedKey = UUID.randomUUID().toString();
projectStub.getProperties().put(includedKey, "foo");
String excludedKey = UUID.randomUUID().toString();
projectStub.getProperties().put(excludedKey, "foo");

writeProjectProperties.setExcludedPropertyKeys(Collections.singleton(excludedKey));
writeProjectProperties.execute();

try (FileReader fr = new FileReader(outputFile)) {
Properties writtenProperties = new Properties();
writtenProperties.load(fr);

assertEquals("foo", writtenProperties.getProperty(includedKey));
assertFalse(writtenProperties.contains(excludedKey));
}
}
}