Skip to content

Commit

Permalink
Avoid NPE on outputFile checking
Browse files Browse the repository at this point in the history
Should not occur as parameter outputFile is defined as required
  • Loading branch information
slawekjaranowski committed Oct 22, 2023
1 parent 1ef42e0 commit 62ddbea
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/it/read-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.mojo.properties.it</groupId>
<artifactId>write-project</artifactId>
<artifactId>read-project</artifactId>
<version>0.0.1-SNAPSHOT</version>

<build>
Expand Down
7 changes: 7 additions & 0 deletions src/it/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ under the License.
</snapshots>
</pluginRepository>
</pluginRepositories>

<properties>
<!-- properties for IT write-active-profile -->
<settings-properties-test1>value1</settings-properties-test1>
<settings-properties-test2>value2</settings-properties-test2>
</properties>
</profile>

</profiles>
<activeProfiles>
<activeProfile>it-repo</activeProfile>
Expand Down
1 change: 1 addition & 0 deletions src/it/write-active-profile/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = clean generate-resources
44 changes: 44 additions & 0 deletions src/it/write-active-profile/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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-active-profilet</artifactId>
<version>0.0.1-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-active-profile-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/project.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>test-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<project-properties-test1>value21</project-properties-test1>
<project-properties-test2>value22</project-properties-test2>
</properties>
</profile>
</profiles>

</project>
18 changes: 18 additions & 0 deletions src/it/write-active-profile/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.nio.file.Files

File prop = new File(basedir, 'target/classes/project.properties')
assert prop.exists()

def properties = new Properties()
try (InputStream input = Files.newInputStream(prop.toPath())) {
properties.load(input)
}

def expected = [
'project-properties-test1' : 'value21',
'project-properties-test2' : 'value22',
'settings-properties-test1': 'value1',
'settings-properties-test2': 'value2'
]

assert properties.equals(expected)
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ protected void writeProperties(Properties properties) throws MojoExecutionExcept
* @throws MojoExecutionException {@link MojoExecutionException}
*/
protected void validateOutputFile() throws MojoExecutionException {
if (outputFile == null) {
throw new MojoExecutionException("outputFile parameter is missing");
}

if (outputFile.isDirectory()) {
throw new MojoExecutionException("outputFile must be a file and not a directory");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ protected WriteActiveProfileProperties(List<PropertiesManager> propertiesManager
@Override
public void execute() throws MojoExecutionException {
validateOutputFile();
List<Profile> list = getProject().getActiveProfiles();
if (getLog().isInfoEnabled()) {
getLog().debug(list.size() + " profile(s) active");
}

List<Profile> profiles = getProject().getActiveProfiles();
getLog().debug("Active profile: " + profiles);

Properties properties = new Properties();
for (Profile profile : list) {
for (Profile profile : profiles) {
if (profile.getProperties() != null) {
properties.putAll(profile.getProperties());
}
Expand Down

0 comments on commit 62ddbea

Please sign in to comment.