All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.mojo.jaxb2.schemageneration.SchemaGenerationMojo Maven / Gradle / Ivy

Go to download

Mojo's JAXB-2 Maven plugin is used to create an object graph from XSDs based on the JAXB 2.x implementation and to generate XSDs from JAXB annotated Java classes.

There is a newer version: 3.2.0
Show newest version
package org.codehaus.mojo.jaxb2.schemageneration;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.codehaus.mojo.jaxb2.AbstractJaxbMojo;
import org.codehaus.mojo.jaxb2.shared.FileSystemUtilities;
import org.codehaus.mojo.jaxb2.shared.filters.Filter;
import org.codehaus.mojo.jaxb2.shared.filters.Filters;
import org.codehaus.mojo.jaxb2.shared.filters.pattern.PatternFileFilter;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 

Mojo that creates XML schema(s) from compile-scope Java sources or binaries * by invoking the JAXB SchemaGenerator. This implementation is tailored to use the * JAXB Reference Implementation from project Kenai.

*

Note that the SchemaGenerationMojo was completely re-implemented for the 2.x versions. * Its configuration semantics and parameter set is not necessarily * backwards compatible with the 1.x plugin versions. If you are * upgrading from version 1.x of the plugin, read the documentation carefully.

* * @author Lennart Jörelid * @see The JAXB Reference Implementation */ @Mojo(name = "schemagen", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true) public class SchemaGenerationMojo extends AbstractXsdGeneratorMojo { /** * The last part of the stale fileName for this SchemaGenerationMojo. */ public static final String STALE_FILENAME = "schemaGenerationStaleFlag"; /** *

Default exclude file name suffixes for sources, used unless overridden by an * explicit configuration in the {@code sourceExcludeSuffixes} parameter. * The default values are found as follows:

*
     *     
     *         final List<Filter<File>> srcTemp = new ArrayList<Filter<File>>();
     *         srcTemp.addAll(AbstractJaxbMojo.STANDARD_EXCLUDE_FILTERS);
     *         srcTemp.add(new PatternFileFilter(Arrays.asList("\\.xjb", "\\.xsd", "\\.properties"), true));
     *         STANDARD_SOURCE_EXCLUDE_FILTERS = Collections.unmodifiableList(srcTemp);
     *     
     * 
* * @see #STANDARD_EXCLUDE_FILTERS */ public static final List> STANDARD_SOURCE_EXCLUDE_FILTERS; static { final List> srcTemp = new ArrayList>(); srcTemp.addAll(AbstractJaxbMojo.STANDARD_EXCLUDE_FILTERS); srcTemp.add(new PatternFileFilter(Arrays.asList("\\.xjb", "\\.xsd", "\\.properties"), true)); STANDARD_SOURCE_EXCLUDE_FILTERS = Collections.unmodifiableList(srcTemp); } /** *

Parameter holding List of paths to files and/or directories which should be recursively searched * for Java source files. Only files or directories that actually exist will be included (in the case of files) * or recursively searched for source files to include (in the case of directories or JARs). * Configure using standard Maven structure for Lists:

*
     * 
     *   <configuration>
     *   ...
     *       <sources>
     *          <source>/a/full/absolute/path/to/a/SourceFile.java</source>
     *          <source>target/some/sourceJar.jar</source>
     *          <source>src/main/java</source>
     *      </sources>
     *   </configuration>
     * 
     * 
*

Note: if configured, the sources parameters replace the default * value, which is a List containing the paths to the directories defined by * {@code getProject().getCompileSourceRoots()}.

* * @since 2.0 */ @Parameter(required = false) private List sources; /** *

Parameter holding a List of Filters, used to match all files under the {@code sources} directories * which should not be considered SchemaGenerator source files. (The filters identify files to * exclude, and hence this parameter is called {@code schemaSourceExcludeFilters}). If a file under any of the * source directories matches at least one of the Filters supplied in the {@code schemaSourceExcludeFilters}, * it is not considered an XJC source file, and therefore excluded from processing.

*

If not explicitly provided, the Mojo uses the value within {@code STANDARD_SOURCE_EXCLUDE_FILTERS}. * The algorithm for finding XJC sources is as follows:

*
    *
  1. Find all files given in the sources List. Any Directories provided are searched for files * recursively.
  2. *
  3. Exclude any found files matching any of the supplied {@code schemaSourceExcludeFilters} List.
  4. *
  5. The remaining Files are submitted for processing by the XJC tool.
  6. *
*

Example: The following configuration would exclude any sources whose names end with * {@code .txt} or {@code .foo}:

*
     *     
     *         <configuration>
     *         ...
     *              <schemaSourceExcludeFilters>
     *                  <filter implementation="org.codehaus.mojo.jaxb2.shared.filters.pattern.PatternFileFilter">
     *                      <patterns>
     *                          <pattern>\.txt</pattern>
     *                          <pattern>\.foo</pattern>
     *                      </patterns>
     *                  </filter>
     *              </schemaSourceExcludeFilters>
     *         </configuration>
     *     
     * 
*

Note that inner workings of the Dependency Injection mechanism used by Maven Plugins (i.e. the DI from * the Plexus container) requires that the full class name to the Filter implementation should be supplied for * each filter, as is illustrated in the sample above. This is true also if you implement custom Filters.

* * @see #STANDARD_SOURCE_EXCLUDE_FILTERS * @see org.codehaus.mojo.jaxb2.shared.filters.pattern.PatternFileFilter * @see org.codehaus.mojo.jaxb2.shared.filters.pattern.AbstractPatternFilter * @see org.codehaus.mojo.jaxb2.shared.filters.AbstractFilter * @since 2.0 */ @Parameter(required = false) private List> schemaSourceExcludeFilters; /** *

The directory where the generated XML Schema file(s) will be * placed, after all transformations are done.

*/ @Parameter(defaultValue = "${project.build.directory}/generated-resources/schemagen", required = true) private File outputDirectory; /** *

The directory where the {@code schemagen} tool will output XSDs, episode files - and intermediary bytecode * files. From this directory the XSDs and the episode file (but not the bytecode files) will be copied to the * outputDirectory for further processing.

* * @see #outputDirectory */ @Parameter(defaultValue = "${project.build.directory}/schemagen-work/compile_scope", required = true) private File workDirectory; /** * {@inheritDoc} */ @Override protected List getCompiledClassNames() { List> excludeFilters = schemaSourceExcludeFilters == null ? STANDARD_BYTECODE_EXCLUDE_FILTERS : schemaSourceExcludeFilters; Filters.initialize(getLog(), excludeFilters); try { return FileSystemUtilities.filterFiles( getProject().getBasedir(), null, getProject().getCompileClasspathElements(), getLog(), "compiled bytecode", excludeFilters); } catch (DependencyResolutionRequiredException e) { throw new IllegalStateException("Could not resolve dependencies.", e); } } /** * {@inheritDoc} */ @Override protected List getSources() { final List> sourceExcludes = schemaSourceExcludeFilters == null ? STANDARD_SOURCE_EXCLUDE_FILTERS : schemaSourceExcludeFilters; Filters.initialize(getLog(), sourceExcludes); final List defaultSources = getProject().getCompileSourceRoots(); // All Done. return FileSystemUtilities.filterFiles( getProject().getBasedir(), (sources == null) ? defaultSources : sources, defaultSources, getLog(), "sources", sourceExcludes); } /** * {@inheritDoc} */ @Override protected File getOutputDirectory() { return outputDirectory; } /** * {@inheritDoc} */ @Override protected List getClasspath() throws MojoExecutionException { List toReturn = null; try { toReturn = getProject().getCompileClasspathElements(); } catch (DependencyResolutionRequiredException e) { throw new MojoExecutionException("Could not acquire compile classpath elements from MavenProject", e); } // All done. return toReturn; } /** * {@inheritDoc} */ @Override protected String getStaleFileName() { return STALE_FILENAME; } /** * {@inheritDoc} */ @Override protected File getWorkDirectory() { return workDirectory; } /** * {@inheritDoc} */ @Override protected void addResource(final Resource resource) { if(resource != null) { final String newDirectory = resource.getDirectory(); // Is the supplied resource already added? final List currentResources = getProject().getResources(); if(getLog().isDebugEnabled()) { getLog().debug("Candidate Resource Directory [" + newDirectory + "]"); getLog().debug("Found [" + currentResources.size() + "] current Resources: " + currentResources); } for (Resource current : currentResources) { // Is the resource already added? if(current.getDirectory() != null && current.getDirectory().equalsIgnoreCase(newDirectory)) { getLog().debug("Resource already added [" + newDirectory + "]. Not adding again."); return; } } // Add the new Resource currentResources.add(resource); if(getLog().isDebugEnabled()) { getLog().debug("Added resource [" + newDirectory + "] to existing resources."); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy