Migrated the configprops parsing to Java due to constant issues with Groovy versions

This commit is contained in:
Marcin Grzejszczak
2020-05-26 13:45:03 +02:00
parent 95688d5c8e
commit bdd2ae3ed3
3 changed files with 137 additions and 304 deletions

View File

@@ -17,10 +17,6 @@
<!-- Comma separated list of whitelisted branches -->
<docs.whitelisted.branches>2.1.x,2.2.x</docs.whitelisted.branches>
<main.basedir>${basedir}/..</main.basedir>
<gmavenplus-plugin.version>1.8.1</gmavenplus-plugin.version>
<groovy.version>2.5.10</groovy.version>
<!-- For the Groovy plugin -->
<groovyVersion>${groovy.version}</groovyVersion>
<!-- We don't want to downloaded sources of our own project-->
<maven-dependency-plugin-for-docs.phase>none</maven-dependency-plugin-for-docs.phase>
<docs.resources.dir>${project.basedir}/src/main/</docs.resources.dir>
@@ -40,143 +36,10 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-xml</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-datetime</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-nio</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-json</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-jsr223</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-macro</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-sql</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-swing</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-cli-picocli</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-ant</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<!-- <dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-astbuilder</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency> -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-console</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-docgenerator</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-groovydoc</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-groovysh</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-jmx</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-servlet</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-templates</artifactId>
<version>${groovy.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-test</artifactId>
<version>${groovy.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-test-junit5</artifactId>
<version>${groovy.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-testng</artifactId>
<version>${groovy.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@@ -184,60 +47,7 @@
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main</directory>
<excludes>
<exclude>groovy/**/*.*</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/groovy</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>addSources</goal>
<goal>generateStubs</goal>
<goal>generateTestStubs</goal>
<goal>groovydoc-jar</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<!-- any version of Groovy \>= 1.5.0 should work here -->
<version>${groovy.version}</version>
<scope>runtime</scope>
<type>pom</type>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>

View File

@@ -1,112 +0,0 @@
package org.springframework.cloud.internal
import java.util.regex.Pattern
import groovy.json.JsonSlurper
import groovy.transform.CompileStatic
import org.springframework.core.io.Resource
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
/**
* @author Marcin Grzejszczak
*/
class Main {
@CompileStatic
static void main(String... args) {
String outputFile = args[0]
String inclusionPattern = args.length > 1 ? args[1] : ".*"
File parent = new File(outputFile).parentFile
if (!parent.exists()) {
println "No parent directory [${parent.toString()}] found. Won't generate the configuration properties file"
return
}
new Generator().generate(outputFile, inclusionPattern)
}
static class Generator {
void generate(String outputFile, String inclusionPattern) {
println "Parsing all configuration metadata"
Resource[] resources = getResources()
println "Found [${resources.length}] configuration metadata jsons"
TreeSet names = new TreeSet()
def descriptions = [:]
int count = 0
int matchingPropertyCount = 0
int propertyCount = 0
Pattern pattern = Pattern.compile(inclusionPattern)
resources.each { Resource resource ->
if (resourceNameContainsPattern(resource)) {
count++
def slurper = new JsonSlurper()
slurper.parseText(resource.inputStream.text).properties.each { val ->
propertyCount++
if (!pattern.matcher(val.name).matches()) {
return
}
matchingPropertyCount++
names.add val.name
descriptions[val.name] = new ConfigValue(val.name, val.description, val.defaultValue)
}
}
}
println "Found [${count}] Cloud projects configuration metadata jsons. [${matchingPropertyCount}/${propertyCount}] were matching the pattern [${inclusionPattern}]"
println "Successfully built the description table"
if (names.empty) {
println("Will not update the table, since no configuration properties were found!")
return
}
new File(outputFile).text = """\
|===
|Name | Default | Description
${names.collect { it -> return descriptions[it] }.join("\n")}
|===
"""
println "Successfully stored the output file"
}
protected boolean resourceNameContainsPattern(Resource resource) {
try {
return resource.getURL().toString().contains("cloud")
}
catch (Exception e) {
println("Exception [${e}] for resource [${resource}] occurred while trying to retrieve its URL")
return false
}
}
protected Resource[] getResources() {
return new PathMatchingResourcePatternResolver()
.getResources("classpath*:/META-INF/spring-configuration-metadata.json")
}
}
@CompileStatic
static class ConfigValue {
String name
String description
Object defaultValue
ConfigValue() {}
ConfigValue(String name, String description, Object defaultValue) {
this.name = name
this.description = escapedValue(description)
this.defaultValue = escapedValue(defaultValue)
}
private String escapedValue(Object value) {
return value != null ?
value.toString().replaceAll('\\|', '\\\\|') : ''
}
String toString() {
"|${name} | ${defaultValue} | ${description}"
}
}
}

View File

@@ -0,0 +1,135 @@
package org.springframework.cloud.internal;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.StreamUtils;
/**
* @author Marcin Grzejszczak
*/
public class Main {
static void main(String... args) {
String outputFile = args[0];
String inclusionPattern = args.length > 1 ? args[1] : ".*";
File parent = new File(outputFile).getParentFile();
if (!parent.exists()) {
System.out.println("No parent directory [" + parent.toString()
+ "] found. Will not generate the configuration properties file");
return;
}
new Generator().generate(outputFile, inclusionPattern);
}
static class Generator {
void generate(String outputFile, String inclusionPattern) {
try {
System.out.println("Parsing all configuration metadata");
Resource[] resources = getResources();
System.out.println("Found [" + resources.length + "] configuration metadata jsons");
TreeSet<String> names = new TreeSet<>();
Map<String, ConfigValue> descriptions = new HashMap<>();
final AtomicInteger count = new AtomicInteger();
final AtomicInteger matchingPropertyCount = new AtomicInteger();
final AtomicInteger propertyCount = new AtomicInteger();
Pattern pattern = Pattern.compile(inclusionPattern);
for (Resource resource : resources) {
if (resourceNameContainsPattern(resource)) {
count.incrementAndGet();
byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
Map<String, Object> response = new ObjectMapper().readValue(bytes, HashMap.class);
List<Map<String, Object>> properties = (List<Map<String, Object>>) response.get("properties");
properties.forEach(val -> {
propertyCount.incrementAndGet();
String name = (String) val.get("name");
if (!pattern.matcher(name).matches()) {
return;
}
String description = (String) val.get("description");
String defaultValue = (String) val.get("defaultValue");
matchingPropertyCount.incrementAndGet();
names.add(name);
descriptions.put(name, new ConfigValue(name, description, defaultValue));
});
}
}
System.out.println(
"Found [" + count + "] Cloud projects configuration metadata jsons. [" + matchingPropertyCount
+ "/" + propertyCount + "] were matching the pattern [" + inclusionPattern + "]");
System.out.println("Successfully built the description table");
if (names.isEmpty()) {
System.out.println("Will not update the table, since no configuration properties were found!");
return;
}
Files.write(new File(outputFile).toPath(),
("|===\n\n"
+ "|Name | Default | Description\n" + names.stream()
.map(it -> descriptions.get(it).toString()).collect(Collectors.joining("\n"))
+ "\n\n" + "|===").getBytes());
System.out.println("Successfully stored the output file");
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
protected boolean resourceNameContainsPattern(Resource resource) {
try {
return resource.getURL().toString().contains("cloud");
}
catch (Exception e) {
System.out.println("Exception [" + e + "] for resource [" + resource
+ "] occurred while trying to retrieve its URL");
return false;
}
}
protected Resource[] getResources() throws IOException {
return new PathMatchingResourcePatternResolver()
.getResources("classpath*:/META-INF/spring-configuration-metadata.json");
}
}
static class ConfigValue {
public String name;
public String description;
public Object defaultValue;
ConfigValue() {
}
ConfigValue(String name, String description, Object defaultValue) {
this.name = name;
this.description = escapedValue(description);
this.defaultValue = escapedValue(defaultValue);
}
private String escapedValue(Object value) {
return value != null ? value.toString().replaceAll("\\|", "\\\\|") : "";
}
public String toString() {
return "|" + name + " | " + defaultValue + " | " + description;
}
}
}