Search in sources :

Example 6 with Enunciate

use of com.webcohesion.enunciate.Enunciate in project enunciate by stoicflame.

the class TestGeneratedJsonTypeSerialization method testCompile.

@Test
public void testCompile() throws Exception {
    assertTrue(this.sourceDir.exists());
    assertTrue(this.outDir.exists() || this.outDir.mkdirs());
    Enunciate enunciate = new Enunciate();
    final ArrayList<File> javaFiles = new ArrayList<File>();
    enunciate.visitFiles(sourceDir, Enunciate.JAVA_FILTER, new Enunciate.FileVisitor() {

        @Override
        public void visit(File file) {
            javaFiles.add(file);
        }
    });
    String classpath = System.getProperty("java.class.path");
    JavaCompiler compiler = JavacTool.create();
    List<String> options = Arrays.asList("-source", "1.6", "-target", "1.6", "-encoding", "UTF-8", "-cp", classpath, "-d", this.outDir.getAbsolutePath());
    JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, options, null, compiler.getStandardFileManager(null, null, null).getJavaFileObjectsFromFiles(javaFiles));
    assertTrue(task.call());
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) File(java.io.File) Enunciate(com.webcohesion.enunciate.Enunciate) Test(org.junit.Test)

Example 7 with Enunciate

use of com.webcohesion.enunciate.Enunciate in project enunciate by stoicflame.

the class DeployArtifactBaseMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    if (this.enunciateArtifactId == null) {
        throw new MojoExecutionException("An id of the enunciate artifact to be deployed must be configured.");
    }
    Enunciate enunciate = (Enunciate) getPluginContext().get(ConfigMojo.ENUNCIATE_PROPERTY);
    if (enunciate == null) {
        throw new MojoExecutionException("No enunciate mechanism found in the project!");
    }
    com.webcohesion.enunciate.artifacts.Artifact enunciateArtifact = enunciate.findArtifact(this.enunciateArtifactId);
    if (enunciateArtifact == null) {
        throw new MojoExecutionException("Unknown Enunciate artifact: " + this.enunciateArtifactId + ".");
    }
    File mainArtifact = null;
    File sources = null;
    File javadocs = null;
    if (enunciateArtifact instanceof ClientLibraryArtifact) {
        for (com.webcohesion.enunciate.artifacts.Artifact childArtifact : ((ClientLibraryArtifact) enunciateArtifact).getArtifacts()) {
            if (childArtifact instanceof FileArtifact) {
                ArtifactType artifactType = ((FileArtifact) childArtifact).getArtifactType();
                if (artifactType != null) {
                    switch(artifactType) {
                        case binaries:
                            mainArtifact = ((FileArtifact) childArtifact).getFile();
                            break;
                        case sources:
                            sources = ((FileArtifact) childArtifact).getFile();
                            break;
                        case javadocs:
                            javadocs = ((FileArtifact) childArtifact).getFile();
                            break;
                    }
                }
            }
        }
    } else if (enunciateArtifact instanceof FileArtifact) {
        mainArtifact = ((FileArtifact) enunciateArtifact).getFile();
    } else {
        try {
            mainArtifact = enunciate.createTempFile(this.enunciateArtifactId, "artifact");
            enunciateArtifact.exportTo(mainArtifact, enunciate);
        } catch (IOException e) {
            throw new MojoExecutionException("Unable to create a temp file.", e);
        }
    }
    if (mainArtifact == null) {
        if (sources != null) {
            mainArtifact = sources;
            sources = null;
        }
    }
    if (mainArtifact == null) {
        throw new MojoExecutionException("Unable to determine the file to deploy from enunciate artifact " + enunciateArtifactId + ".");
    }
    // Process the supplied POM (if there is one)
    Model model = null;
    if (pomFile != null) {
        generatePom = false;
        model = readModel(pomFile);
        processModel(model);
    }
    if (this.packaging == null) {
        String artifactName = mainArtifact.getName();
        int dotIndex = artifactName.indexOf('.');
        if (dotIndex > 0 && (dotIndex + 1 < artifactName.length())) {
            this.packaging = artifactName.substring(dotIndex + 1);
        }
    }
    if (this.packaging == null) {
        throw new MojoExecutionException("Unable to determine the packaging of enunciate artifact " + enunciateArtifactId + ". Please specify it in the configuration.");
    }
    if (this.version == null) {
        throw new MojoExecutionException("Null version.");
    }
    if (model == null) {
        model = new Model();
        model.setModelVersion("4.0.0");
        model.setGroupId(this.groupId);
        model.setArtifactId(this.artifactId);
        model.setVersion(this.version);
        model.setPackaging(this.packaging);
        model.setDescription(this.description);
    }
    ArtifactRepository repo = getDeploymentRepository();
    String protocol = repo.getProtocol();
    if (protocol.equals("scp")) {
        File sshFile = new File(System.getProperty("user.home"), ".ssh");
        if (!sshFile.exists()) {
            sshFile.mkdirs();
        }
    }
    Artifact artifact = this.artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, packaging, classifier);
    // Upload the POM if requested, generating one if need be
    if (generatePom) {
        ArtifactMetadata metadata = new ProjectArtifactMetadata(artifact, generatePomFile(model));
        artifact.addMetadata(metadata);
    } else {
        ArtifactMetadata metadata = new ProjectArtifactMetadata(artifact, pomFile);
        artifact.addMetadata(metadata);
    }
    try {
        getDeployer().deploy(mainArtifact, artifact, repo, this.localRepository);
        if (sources != null || javadocs != null) {
            MavenProject project = new MavenProject(model);
            project.setArtifact(artifact);
            if (sources != null) {
                // we have to do it this way because of classloading issues.
                this.projectHelper.attachArtifact(project, artifact.getType(), "sources", sources);
                getDeployer().deploy(sources, (Artifact) project.getAttachedArtifacts().get(0), repo, this.localRepository);
            }
            if (javadocs != null) {
                // we have to do it this way because of classloading issues.
                this.projectHelper.attachArtifact(project, artifact.getType(), "javadoc", javadocs);
                getDeployer().deploy(javadocs, (Artifact) project.getAttachedArtifacts().get(0), repo, this.localRepository);
            }
        }
    } catch (ArtifactDeploymentException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactDeploymentException(org.apache.maven.artifact.deployer.ArtifactDeploymentException) ClientLibraryArtifact(com.webcohesion.enunciate.artifacts.ClientLibraryArtifact) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) IOException(java.io.IOException) FileArtifact(com.webcohesion.enunciate.artifacts.FileArtifact) Artifact(org.apache.maven.artifact.Artifact) ClientLibraryArtifact(com.webcohesion.enunciate.artifacts.ClientLibraryArtifact) Enunciate(com.webcohesion.enunciate.Enunciate) MavenProject(org.apache.maven.project.MavenProject) FileArtifact(com.webcohesion.enunciate.artifacts.FileArtifact) ArtifactType(com.webcohesion.enunciate.artifacts.ArtifactType) Model(org.apache.maven.model.Model) File(java.io.File) ArtifactMetadata(org.apache.maven.artifact.metadata.ArtifactMetadata) ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata)

Example 8 with Enunciate

use of com.webcohesion.enunciate.Enunciate in project enunciate by stoicflame.

the class DocsBaseMojo method generate.

public void generate(Sink sink, Locale locale) throws MavenReportException {
    if (this.siteError != null) {
        throw new MavenReportException("Unable to generate Enunciate documentation.", this.siteError);
    }
    // first get rid of the empty page the the site plugin puts there, in order to make room for the documentation.
    new File(getReportOutputDirectory(), this.indexPageName == null ? "index.html" : this.indexPageName).delete();
    Enunciate enunciate = (Enunciate) getPluginContext().get(ConfigMojo.ENUNCIATE_PROPERTY);
    try {
        enunciate.copyDir(getReportStagingDirectory(), getReportOutputDirectory());
    } catch (IOException e) {
        throw new MavenReportException("Unable to copy Enunciate documentation from the staging area to the report directory.", e);
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException) Enunciate(com.webcohesion.enunciate.Enunciate)

Example 9 with Enunciate

use of com.webcohesion.enunciate.Enunciate 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);
}
Also used : EnunciateConfiguration(com.webcohesion.enunciate.EnunciateConfiguration) License(org.apache.maven.model.License) Contributor(org.apache.maven.model.Contributor) ProjectExtensionModule(com.webcohesion.enunciate.module.ProjectExtensionModule) URL(java.net.URL) Enunciate(com.webcohesion.enunciate.Enunciate) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Resource(org.apache.maven.model.Resource) IOException(java.io.IOException) EnunciateException(com.webcohesion.enunciate.EnunciateException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SAXException(org.xml.sax.SAXException) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Aggregations

Enunciate (com.webcohesion.enunciate.Enunciate)9 File (java.io.File)9 IOException (java.io.IOException)5 EnunciateConfiguration (com.webcohesion.enunciate.EnunciateConfiguration)4 ArrayList (java.util.ArrayList)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 ArtifactType (com.webcohesion.enunciate.artifacts.ArtifactType)2 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)2 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)2 EnunciateModule (com.webcohesion.enunciate.module.EnunciateModule)2 JAXPParser (com.sun.xml.xsom.parser.JAXPParser)1 XSOMParser (com.sun.xml.xsom.parser.XSOMParser)1 EnunciateException (com.webcohesion.enunciate.EnunciateException)1 ProjectExtensionModule (com.webcohesion.enunciate.module.ProjectExtensionModule)1 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)1 WsdlInfo (com.webcohesion.enunciate.modules.jaxws.WsdlInfo)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1