diff --git a/pom.xml b/pom.xml
index b91ca94..b63a7a7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
properties-maven-plugin
- 1.2.2-SNAPSHOT
+ 1.3.0-SNAPSHOT
maven-plugin
diff --git a/src/main/java/org/codehaus/mojo/properties/AbstractWritePropertiesMojo.java b/src/main/java/org/codehaus/mojo/properties/AbstractWritePropertiesMojo.java
index 7385806..50fcfbb 100644
--- a/src/main/java/org/codehaus/mojo/properties/AbstractWritePropertiesMojo.java
+++ b/src/main/java/org/codehaus/mojo/properties/AbstractWritePropertiesMojo.java
@@ -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;
+ }
}
diff --git a/src/main/java/org/codehaus/mojo/properties/WriteProjectProperties.java b/src/main/java/org/codehaus/mojo/properties/WriteProjectProperties.java
index 2fa37e5..56b394c 100644
--- a/src/main/java/org/codehaus/mojo/properties/WriteProjectProperties.java
+++ b/src/main/java/org/codehaus/mojo/properties/WriteProjectProperties.java
@@ -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;
/**
@@ -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 excludedPropertyKeys;
+
+ /**
+ * Property keys to include.
+ * @since 1.3.0
+ */
+ @Parameter(property = "properties.includedPropertyKeys")
+ private Set includedPropertyKeys;
+
/**
* Default constructor
*
@@ -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 excludedPropertyKeys) {
+ this.excludedPropertyKeys = excludedPropertyKeys;
+ }
+
+ /**
+ * Default scope for test access.
+ */
+ void setIncludedPropertyKeys(Set includedPropertyKeys) {
+ this.includedPropertyKeys = includedPropertyKeys;
+ }
}
diff --git a/src/test/java/org/codehaus/mojo/properties/WritePropertiesMojoTest.java b/src/test/java/org/codehaus/mojo/properties/WritePropertiesMojoTest.java
new file mode 100644
index 0000000..7ff8648
--- /dev/null
+++ b/src/test/java/org/codehaus/mojo/properties/WritePropertiesMojoTest.java
@@ -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));
+ }
+ }
+}