use of com.webcohesion.enunciate.module.ProjectExtensionModule in project enunciate by stoicflame.
the class ConfigMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
if (skipEnunciate) {
getLog().info("[ENUNCIATE] Skipping enunciate per configuration.");
return;
}
Enunciate enunciate = new Enunciate();
// set up the logger.
enunciate.setLogger(new MavenEnunciateLogger());
// set the build dir.
enunciate.setBuildDir(this.buildDir);
// load the config.
EnunciateConfiguration config = enunciate.getConfiguration();
File configFile = this.configFile;
if (configFile == null) {
configFile = new File(project.getBasedir(), "enunciate.xml");
}
if (configFile.exists()) {
getLog().info("[ENUNCIATE] Using enunciate configuration at " + configFile.getAbsolutePath());
try {
loadConfig(enunciate, configFile);
config.setBase(configFile.getParentFile());
} catch (Exception e) {
throw new MojoExecutionException("Problem with enunciate config file " + configFile, e);
}
} else if (this.configFile != null) {
throw new MojoFailureException("Enunciate config file \"" + this.configFile + "\" does not exist");
} else {
getLog().debug("[ENUNCIATE] Default config file does not exist");
}
// set the default configured label.
config.setDefaultSlug(project.getArtifactId());
if (StringUtils.isNotBlank(title)) {
config.setDefaultTitle(title);
}
String defaultDescription = getDefaultDescription();
if (defaultDescription != null) {
config.setDefaultDescription(defaultDescription);
}
if (project.getVersion() != null && !"".equals(project.getVersion().trim())) {
config.setDefaultVersion(project.getVersion());
}
List contributors = project.getContributors();
if (contributors != null && !contributors.isEmpty()) {
List<EnunciateConfiguration.Contact> contacts = new ArrayList<EnunciateConfiguration.Contact>(contributors.size());
for (Object c : contributors) {
Contributor contributor = (Contributor) c;
contacts.add(new EnunciateConfiguration.Contact(contributor.getName(), contributor.getUrl(), contributor.getEmail()));
}
config.setDefaultContacts(contacts);
}
List licenses = project.getLicenses();
if (licenses != null && !licenses.isEmpty()) {
License license = (License) licenses.get(0);
config.setDefaultApiLicense(new EnunciateConfiguration.License(license.getName(), license.getUrl(), null, null));
}
// set the class paths.
setClasspathAndSourcepath(enunciate);
// load any modules on the classpath.
List<URL> pluginClasspath = buildPluginClasspath();
ServiceLoader<EnunciateModule> moduleLoader = ServiceLoader.load(EnunciateModule.class, new URLClassLoader(pluginClasspath.toArray(new URL[pluginClasspath.size()]), Thread.currentThread().getContextClassLoader()));
for (EnunciateModule module : moduleLoader) {
enunciate.addModule(module);
}
// set the compiler arguments.
List<String> compilerArgs = new ArrayList<String>();
String sourceVersion = findSourceVersion();
if (sourceVersion != null) {
compilerArgs.add("-source");
compilerArgs.add(sourceVersion);
}
String targetVersion = findTargetVersion();
if (targetVersion != null) {
compilerArgs.add("-target");
compilerArgs.add(targetVersion);
}
String sourceEncoding = this.encoding;
if (sourceEncoding != null) {
compilerArgs.add("-encoding");
compilerArgs.add(sourceEncoding);
}
if (this.compilerArgs != null) {
compilerArgs.addAll(Arrays.asList(this.compilerArgs));
}
enunciate.getCompilerArgs().addAll(compilerArgs);
// includes.
if (this.includes != null) {
for (String include : this.includes) {
enunciate.addInclude(include);
}
}
// excludes.
if (this.excludes != null) {
for (String exclude : this.excludes) {
enunciate.addExclude(exclude);
}
}
// exports.
if (this.exports != null) {
for (String exportId : this.exports.keySet()) {
String filename = this.exports.get(exportId);
if (filename == null || "".equals(filename)) {
throw new MojoExecutionException("Invalid (empty or null) filename for export " + exportId + ".");
}
File exportFile = new File(filename);
if (!exportFile.isAbsolute()) {
exportFile = new File(this.exportsDir, filename);
}
enunciate.addExport(exportId, exportFile);
}
}
Set<String> enunciateAddedSourceDirs = new TreeSet<String>();
List<EnunciateModule> modules = enunciate.getModules();
if (modules != null) {
Set<String> projectExtensions = new TreeSet<String>(this.projectExtensions == null ? Collections.<String>emptyList() : Arrays.asList(this.projectExtensions));
for (EnunciateModule module : modules) {
// configure the project with the module project extensions.
if (projectExtensions.contains(module.getName()) && module instanceof ProjectExtensionModule) {
ProjectExtensionModule extensions = (ProjectExtensionModule) module;
for (File projectSource : extensions.getProjectSources()) {
String sourceDir = projectSource.getAbsolutePath();
enunciateAddedSourceDirs.add(sourceDir);
if (!project.getCompileSourceRoots().contains(sourceDir)) {
getLog().debug("[ENUNCIATE] Adding '" + sourceDir + "' to the compile source roots.");
project.addCompileSourceRoot(sourceDir);
}
}
for (File testSource : extensions.getProjectTestSources()) {
project.addTestCompileSourceRoot(testSource.getAbsolutePath());
}
for (File resourceDir : extensions.getProjectResourceDirectories()) {
Resource restResource = new Resource();
restResource.setDirectory(resourceDir.getAbsolutePath());
project.addResource(restResource);
}
for (File resourceDir : extensions.getProjectTestResourceDirectories()) {
Resource resource = new Resource();
resource.setDirectory(resourceDir.getAbsolutePath());
project.addTestResource(resource);
}
}
applyAdditionalConfiguration(module);
}
}
// add any new source directories to the project.
Set<File> sourceDirs = new HashSet<File>();
Collection<String> sourcePaths = this.sources == null || this.sources.length == 0 ? (Collection<String>) project.getCompileSourceRoots() : Arrays.asList(this.sources);
for (String sourcePath : sourcePaths) {
File sourceDir = new File(sourcePath);
if (!enunciateAddedSourceDirs.contains(sourceDir.getAbsolutePath())) {
sourceDirs.add(sourceDir);
} else {
getLog().info("[ENUNCIATE] " + sourceDir + " appears to be added to the source roots by Enunciate. Excluding from original source roots....");
}
}
for (File sourceDir : sourceDirs) {
enunciate.addSourceDir(sourceDir);
}
postProcessConfig(enunciate);
try {
enunciate.run();
} catch (Exception e) {
Throwable t = unwrap(e);
if (t instanceof EnunciateException) {
throw new MojoExecutionException(t.getMessage(), t);
}
throw new MojoExecutionException("Error invoking Enunciate.", e);
}
if (this.artifacts != null) {
for (Artifact projectArtifact : artifacts) {
if (projectArtifact.getEnunciateArtifactId() == null) {
getLog().warn("[ENUNCIATE] No enunciate export id specified. Skipping project artifact...");
continue;
}
com.webcohesion.enunciate.artifacts.Artifact artifact = null;
for (com.webcohesion.enunciate.artifacts.Artifact enunciateArtifact : enunciate.getArtifacts()) {
if (projectArtifact.getEnunciateArtifactId().equals(enunciateArtifact.getId()) || enunciateArtifact.getAliases().contains(projectArtifact.getEnunciateArtifactId())) {
artifact = enunciateArtifact;
break;
}
}
if (artifact != null) {
try {
File tempExportFile = enunciate.createTempFile(project.getArtifactId() + "-" + projectArtifact.getClassifier(), projectArtifact.getArtifactType());
artifact.exportTo(tempExportFile, enunciate);
projectHelper.attachArtifact(project, projectArtifact.getArtifactType(), projectArtifact.getClassifier(), tempExportFile);
} catch (IOException e) {
throw new MojoExecutionException("Error exporting Enunciate artifact.", e);
}
} else {
getLog().warn("[ENUNCIATE] Enunciate artifact '" + projectArtifact.getEnunciateArtifactId() + "' not found in the project...");
}
}
}
postProcess(enunciate);
getPluginContext().put(ConfigMojo.ENUNCIATE_PROPERTY, enunciate);
}
Aggregations