
fr.ird.observe.maven.plugins.toolbox.ToolboxMojoSupport Maven / Gradle / Ivy
package fr.ird.observe.maven.plugins.toolbox;
/*-
* #%L
* ObServe Toolkit :: Maven plugin
* %%
* Copyright (C) 2008 - 2017 IRD, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Date;
import java.util.Set;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.nuiton.plugin.AbstractPlugin;
/**
* Created on 31/08/16.
*
* @author Tony Chemit - [email protected]
*/
public abstract class ToolboxMojoSupport extends AbstractPlugin {
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;
@Parameter(defaultValue = "${project.basedir}/src/main/resources", required = true)
private File sourceRoot;
private Path outputFile;
@Override
protected void init() throws Exception {
if (isSkip()) {
return;
}
if (getLog().isDebugEnabled()) {
setVerbose(true);
}
outputFile = createOutputFile();
}
protected abstract Path createOutputFile() throws IOException;
protected abstract boolean isSkip();
@Override
public MavenProject getProject() {
return project;
}
@Override
public void setProject(MavenProject project) {
this.project = project;
}
public File getSourceRoot() {
return sourceRoot;
}
public Path getOutputFile() {
return outputFile;
}
public Path getSourceRootPath() {
return getSourceRoot().toPath();
}
void generate(String packageName, String className, String prefix, Set fields, Path outputFile) throws IOException {
try (BufferedWriter writer = Files.newBufferedWriter(outputFile, StandardCharsets.UTF_8)) {
writer.write("// Generated by " + getClass().getName() + " at " + new Date() + "\n");
writer.write("package " + packageName + ";\n");
writer.write("\n");
writer.write("\n");
writer.write("import static org.nuiton.i18n.I18n.n;\n");
writer.write("\n");
writer.write("\n");
writer.write("public class " + className + " {\n");
writer.write("\n static {");
writer.write("\n");
for (String typeName : fields) {
writer.write(" n(\"" + prefix + typeName + "\");\n");
}
writer.write("\n");
writer.write(" }\n");
writer.write("\n");
writer.write("}\n");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy