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

Enable spotless and checkstyle #105

Merged
merged 2 commits into from
Jul 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 51 additions & 35 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?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
Expand All @@ -18,32 +17,29 @@
specific language governing permissions and limitations
under the License.
-->

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

<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>mojo-parent</artifactId>
<version>76</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>properties-maven-plugin</artifactId>
<version>1.2.0-SNAPSHOT</version>

<name>Properties Maven Plugin</name>
<description>
The Properties Maven Plugin is here to make life a little easier when dealing
with properties. It provides goals to read and write properties from files.
</description>

<inceptionYear>2009</inceptionYear>

<packaging>maven-plugin</packaging>

<name>Properties Maven Plugin</name>
<description>The Properties Maven Plugin is here to make life a little easier when dealing
with properties. It provides goals to read and write properties from files.</description>

<url>https://www.mojohaus.org/properties-maven-plugin</url>

<inceptionYear>2009</inceptionYear>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
Expand All @@ -52,28 +48,6 @@
</license>
</licenses>

<properties>
<mavenVersion>3.5.4</mavenVersion>
<scmpublish.content>target/staging/${project.artifactId}</scmpublish.content>
<project.build.outputTimestamp>2022-03-19T15:53:05Z</project.build.outputTimestamp>
</properties>


<scm>
<connection>scm:git:https://github.com/mojohaus/properties-maven-plugin.git</connection>
<developerConnection>scm:git:ssh://git@github.com/mojohaus/properties-maven-plugin.git</developerConnection>
<url>https://github.com/mojohaus/properties-maven-plugin/tree/master</url>
<tag>master</tag>
</scm>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/mojohaus/properties-maven-plugin/issues/</url>
</issueManagement>
<ciManagement>
<system>GitHub</system>
<url>https://github.com/mojohaus/properties-maven-plugin/actions/</url>
</ciManagement>

<developers>
<developer>
<id>arsenalist</id>
Expand All @@ -93,6 +67,28 @@
</developer>
</developers>

<scm>
<connection>scm:git:https://github.com/mojohaus/properties-maven-plugin.git</connection>
<developerConnection>scm:git:ssh://git@github.com/mojohaus/properties-maven-plugin.git</developerConnection>
<tag>master</tag>
<url>https://github.com/mojohaus/properties-maven-plugin/tree/master</url>
</scm>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/mojohaus/properties-maven-plugin/issues/</url>
</issueManagement>
<ciManagement>
<system>GitHub</system>
<url>https://github.com/mojohaus/properties-maven-plugin/actions/</url>
</ciManagement>

<properties>
<mavenVersion>3.5.4</mavenVersion>
<recommendedJavaBuildVersion>11</recommendedJavaBuildVersion>
<scmpublish.content>target/staging/${project.artifactId}</scmpublish.content>
<project.build.outputTimestamp>2022-03-19T15:53:05Z</project.build.outputTimestamp>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down Expand Up @@ -128,7 +124,6 @@
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>

<profiles>
Expand Down Expand Up @@ -169,5 +164,26 @@
</plugins>
</build>
</profile>
<profile>
<id>java11+</id>
<activation>
<jdk>[11,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>${checkstyle.spotless.config}</configLocation>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,123 +19,99 @@
* 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.BufferedReader;
import java.io.File;
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;
import java.io.BufferedReader;

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;

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

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

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

/**
* @param properties {@link Properties}
* @param file {@link File}
* @throws MojoExecutionException {@link MojoExecutionException}
*/
protected void writeProperties( Properties properties, File file )
throws MojoExecutionException
{
try
{
storeWithoutTimestamp( properties, file, "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 );
protected void writeProperties(Properties properties, File file) throws MojoExecutionException {
try {
storeWithoutTimestamp(properties, file, "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);
}
}

// 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 );
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() ) ) )
{
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 );
while ((line = r.readLine()) != null) {
if (!line.startsWith("#") || line.equals(comments)) {
lines.add(line);
}
}
}

Collections.sort( lines );
for ( String l : lines )
{
pw.println( l );
Collections.sort(lines);
for (String l : lines) {
pw.println(l);
}
}
}

/**
* @throws MojoExecutionException {@link MojoExecutionException}
*/
protected void validateOutputFile()
throws MojoExecutionException
{
if ( outputFile.isDirectory() )
{
throw new MojoExecutionException( "outputFile must be a file and not a directory" );
protected void validateOutputFile() 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;
}

}