com.soento.generator.BaseGeneratorMojo Maven / Gradle / Ivy
package com.soento.generator;
import com.soento.core.base.util.StringUtil;
import com.soento.devtools.config.Settings;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.FileReader;
/**
* @author soento
*/
public abstract class BaseGeneratorMojo extends AbstractMojo {
@Parameter(property = "project", required = true, readonly = true)
protected MavenProject project;
@Parameter(property = "soento.outputDirectory", defaultValue = "${project.build.directory}/generated-sources/soento-generator", required = true)
protected File outputDirectory;
@Parameter(property = "soento.configurationFile", defaultValue = "${project.basedir}/src/test/resources/soento-generator-config.xml", required = true)
protected File configurationFile;
protected Log log;
public BaseGeneratorMojo() {
this.log = getLog();
}
public Settings getSettings() {
if (configurationFile == null) {
throw new RuntimeException("找不到配置文件");
}
if (!configurationFile.exists()) {
throw new RuntimeException("配置文件不存在,路径:" + configurationFile.getPath());
}
if (outputDirectory == null) {
throw new RuntimeException("找不到输出目录");
}
if (!outputDirectory.exists()) {
outputDirectory.mkdirs();
}
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Settings.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Settings settings = (Settings) unmarshaller.unmarshal(new FileReader(configurationFile));
if (StringUtil.isBlank(settings.getOutDir())) {
settings.setOutDir(outputDirectory.getPath() + File.separator);
}
return settings;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}