Mặc dù tôi phần nào đồng ý với Peter Tilemans, tôi cũng bị cám dỗ bởi vấn đề này và tôi đã tấn công một giải pháp sử dụng groovy và GMaven-Plugin. EDIT: Điều tuyệt vời về GMaven là bạn có thể truy cập trực tiếp vào mô hình đối tượng maven mà không cần tạo plugin trước và vẫn có khả năng lập trình đầy đủ của Groovy.
Những gì tôi làm trong trường hợp như thế này là tạo thư mục nguồn được gọi là src/main/groovy không phải là một phần của quá trình xây dựng thực tế (và do đó sẽ không đóng góp vào jar/war etc). Ở đó tôi có thể đặt các tập tin nguồn groovy, do đó cho phép nhật thực để sử dụng chúng như các thư mục nguồn groovy cho tự động hoàn thành vv mà không thay đổi xây dựng.
Vì vậy, trong thư mục này tôi có ba tập tin: EnumGenerator.groovy, enumTemplate.txt và enum.properties (Tôi đã làm điều này để đơn giản, bạn có thể sẽ nhận được các thuộc tính tập tin từ một nơi khác)
Ở đây họ :
EnumGenerator.groovy
import java.util.Arrays;
import java.util.HashMap;
import java.util.TreeMap;
import java.io.File;
import java.util.Properties;
class EnumGenerator{
public EnumGenerator(
File targetDir,
File propfile,
File templateFile,
String pkgName,
String clsName
) {
def properties = new Properties();
properties.load(propfile.newInputStream());
def bodyText = generateBody(new TreeMap(properties));
def enumCode = templateFile.getText();
def templateMap = [ body:bodyText, packageName:pkgName, className: clsName ];
templateMap.each{ key, value ->
enumCode = enumCode.replace("\${$key}", value) }
writeToFile(enumCode, targetDir, pkgName, clsName)
}
void writeToFile(code, dir, pkg, cls) {
def parentDir = new File(dir, pkg.replace('.','/'))
parentDir.mkdirs();
def enumFile = new File (parentDir, cls + '.java')
enumFile.write(code)
System.out.println("Wrote file $enumFile successfully")
}
String generateBody(values) {
// create constructor call PROPERTY_KEY("value")
// from property.key=value
def body = "";
values.eachWithIndex{
key, value, index ->
body +=
(
(index > 0 ? ",\n\t" : "\t")
+ toConstantCase(key) + '("' + value + '")'
)
}
body += ";";
return body;
}
String toConstantCase(value) {
// split camelCase and dot.notation to CAMEL_CASE and DOT_NOTATION
return Arrays.asList(
value.split("(?:(?=\\p{Upper})|\\.)")
).join('_').toUpperCase();
}
}
enumTemplate.txt
package ${packageName};
public enum ${className} {
${body}
private ${className}(String value){
this.value = value;
}
private String value;
public String getValue(){
return this.value;
}
}
enum.tính
simple=value
not.so.simple=secondvalue
propertyWithCamelCase=thirdvalue
Dưới đây là cấu hình pom:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>create-enum</id>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scriptpath>
<element>${pom.basedir}/src/main/groovy</element>
</scriptpath>
<source>
import java.io.File
import EnumGenerator
File groovyDir = new File(pom.basedir,
"src/main/groovy")
new EnumGenerator(
new File(pom.build.directory,
"generated-sources/enums"),
new File(groovyDir,
"enum.properties"),
new File(groovyDir,
"enumTemplate.txt"),
"com.mycompany.enums",
"ServiceProperty"
);
</source>
</configuration>
</execution>
</executions>
</plugin>
Và đây là kết quả:
package com.mycompany.enums;
public enum ServiceProperty {
NOT_SO_SIMPLE("secondvalue"),
PROPERTY_WITH_CAMEL_CASE("thirdvalue"),
SIMPLE("value");
private ServiceProperty(String value){
this.value = value;
}
private String value;
public String getValue(){
return this.value;
}
}
bằng cách sử dụng mẫu, bạn có thể tùy chỉnh các enum cho phù hợp với nhu cầu của bạn . và kể từ khi gmaven nhúng groovy vào maven, bạn không phải cài đặt bất kỳ thứ gì hoặc thay đổi cấu hình xây dựng của mình.
Điều duy nhất cần nhớ là bạn sẽ cần phải sử dụng plugin buildhelper để add the generated source folder để tạo.
thực sự bạn nên chỉnh sửa tiêu đề câu hỏi, vì môi trường của bạn là con quạ và kiến chỉ là một lựa chọn có thể –
Tôi biết điều này là cũ, nhưng tại sao không chỉ vứt bỏ các tập tin thuộc tính và chỉ sử dụng enums? –