Skip to content

Commit

Permalink
make write-*-properties output reproducible: sort & drop timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Mar 19, 2022
1 parent a63b95c commit 46d3ae4
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ target
.classpath
*.iml
/.idea/
/.factorypath
39 changes: 39 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,43 @@

</dependencies>

<profiles>
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<configuration>
<addTestClassPath>true</addTestClassPath>
<debug>true</debug>
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<projectsDirectory>src/it</projectsDirectory>
<showVersion>true</showVersion>
<setupIncludes>
<setupInclude>setup/pom.xml</setupInclude>
</setupIncludes>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
<preBuildHookScript>setup</preBuildHookScript>
<postBuildHookScript>verify</postBuildHookScript>
<settingsFile>src/it/settings.xml</settingsFile>
</configuration>
<executions>
<execution>
<goals>
<goal>install</goal>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
63 changes: 63 additions & 0 deletions src/it/settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

<settings>
<mirrors>
<mirror>
<id>it-repo</id>
<name>RPM</name>
<url>@localRepositoryUrl@</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>it-repo</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>local.central</id>
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>local.central</id>
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>
1 change: 1 addition & 0 deletions src/it/write-project/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = clean generate-resources
37 changes: 37 additions & 0 deletions src/it/write-project/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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>

<properties>
<g>${groupId}</g>
<a>${artifactId}</a>
<v>${version}</v>
<properties-maven-plugin.version>@project.version@</properties-maven-plugin.version>
</properties>

<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-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/project.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
24 changes: 24 additions & 0 deletions src/it/write-project/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 prop = new File( basedir, 'target/classes/project.properties' )
assert prop.exists()

String content = prop.getText()
assert content.startsWith( "#Properties" )
assert !content.substring( 1 ).contains( '#' ) // check no timestamp comment, that starts with #
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.codehaus.mojo.properties;

import java.io.BufferedReader;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand All @@ -23,6 +25,13 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/*
Expand Down Expand Up @@ -70,9 +79,9 @@ public abstract class AbstractWritePropertiesMojo
protected void writeProperties( Properties properties, File file )
throws MojoExecutionException
{
try ( FileOutputStream fos = new FileOutputStream( file ) )
try
{
properties.store( fos, "Properties" );
storeWithoutTimestamp( properties, file, "Properties" );
}
catch ( FileNotFoundException e )
{
Expand All @@ -86,6 +95,36 @@ protected void writeProperties( Properties properties, File file )
}
}

// 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.util.Properties;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

Expand Down

0 comments on commit 46d3ae4

Please sign in to comment.