externalize message plugin

This commit is contained in:
f43nd1r 2019-12-05 01:03:21 +01:00
parent eeb41ca79e
commit a872a8fa64
4 changed files with 43 additions and 163 deletions

View file

@ -5,10 +5,10 @@
<parent> <parent>
<groupId>com.faendir</groupId> <groupId>com.faendir</groupId>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<version>0.9.20-SNAPSHOT</version> <version>${revision}</version>
</parent> </parent>
<artifactId>acrarium</artifactId> <artifactId>acrarium</artifactId>
<version>0.9.20-SNAPSHOT</version> <version>${revision}</version>
<packaging>war</packaging> <packaging>war</packaging>
<name>Acrarium</name> <name>Acrarium</name>
<properties> <properties>
@ -234,6 +234,14 @@
<id>central</id> <id>central</id>
<url>https://repo1.maven.org/maven2/</url> <url>https://repo1.maven.org/maven2/</url>
</pluginRepository> </pluginRepository>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-f43nd1r-maven</id>
<name>bintray-plugins</name>
<url>https://dl.bintray.com/f43nd1r/maven</url>
</pluginRepository>
</pluginRepositories> </pluginRepositories>
<build> <build>
<defaultGoal>spring-boot:run</defaultGoal> <defaultGoal>spring-boot:run</defaultGoal>
@ -321,9 +329,9 @@
</executions> </executions>
</plugin> </plugin>
<plugin> <plugin>
<groupId>com.faendir</groupId> <groupId>com.faendir.maven</groupId>
<artifactId>message-generator-maven-plugin</artifactId> <artifactId>resource-class-generator</artifactId>
<version>0.9.20-SNAPSHOT</version> <version>1.0.2</version>
<executions> <executions>
<execution> <execution>
<goals> <goals>

View file

@ -1,69 +0,0 @@
<?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>com.faendir</groupId>
<artifactId>message-generator-maven-plugin</artifactId>
<version>0.9.20-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>message-generator-maven-plugin</name>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>26.0-jre</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<doclint>none</doclint>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -1,87 +0,0 @@
package com.faendir;
/*
* Copyright 2019 Lukas Morawietz (https://github.com/F43nd1r)
*
* Licensed 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.
*/
import com.google.common.base.CaseFormat;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import org.apache.maven.plugin.AbstractMojo;
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 javax.lang.model.element.Modifier;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Mojo which generates a class with constants from message resource bundles
*/
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class MessageGenerator extends AbstractMojo {
@Parameter(property = "inputDirectory", defaultValue = "src/main/resources")
private File inputDirectory;
@Parameter(property = "outputDirectory", defaultValue = "target/generated-sources/java")
private File outputDirectory;
@Parameter(property = "packageName", defaultValue = "com.faendir.i18n")
private String packageName;
@Parameter(property = "className", defaultValue = "Messages")
private String className;
public void execute() throws MojoExecutionException {
if (!outputDirectory.exists()) {
outputDirectory.mkdirs();
}
Set<String> keys = new HashSet<>();
try {
Files.walk(inputDirectory.toPath())
.peek(file -> System.out.println("Walking " + file))
.filter(file -> file.toString().endsWith(".properties"))
.peek(file -> System.out.println("Found " + file))
.forEach(file -> {
try {
Properties properties = new Properties();
properties.load(new FileReader(file.toFile()));
keys.addAll(Collections.list(properties.keys()).stream().map(Object::toString).collect(Collectors.toList()));
} catch (Exception ignored) {
}
});
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
for (String key : keys) {
classBuilder.addField(FieldSpec.builder(String.class, CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, key), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("$S", key)
.build());
}
JavaFile.builder(packageName, classBuilder.build())
.skipJavaLangImports(true)
.indent(" ")
.build()
.writeTo(outputDirectory);
} catch (IOException e) {
e.printStackTrace();
}
}
}

32
pom.xml
View file

@ -3,15 +3,17 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.faendir</groupId> <groupId>com.faendir</groupId>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<version>0.9.20-SNAPSHOT</version> <version>${revision}</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version> <version>2.2.1.RELEASE</version>
</parent> </parent>
<properties>
<revision>0.9.20-SNAPSHOT</revision>
</properties>
<modules> <modules>
<module>message-generator-maven-plugin</module>
<module>acrarium</module> <module>acrarium</module>
</modules> </modules>
<scm> <scm>
@ -27,6 +29,7 @@
<version>2.5.3</version> <version>2.5.3</version>
<configuration> <configuration>
<goals>install</goals> <goals>install</goals>
<autoVersionSubmodules>true</autoVersionSubmodules>
<tagNameFormat>v@{project.version}</tagNameFormat> <tagNameFormat>v@{project.version}</tagNameFormat>
</configuration> </configuration>
</plugin> </plugin>
@ -45,6 +48,31 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
</configuration>
<executions>
<!-- enable flattening -->
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<!-- ensure proper cleanup -->
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>