Search in sources :

Example 1 with FileLocatorStrategy

use of org.apache.maven.shared.io.location.FileLocatorStrategy in project maven-plugins by apache.

the class DefaultAssemblyReader method readAssemblies.

@Override
public List<Assembly> readAssemblies(final AssemblerConfigurationSource configSource) throws AssemblyReadException, InvalidAssemblerConfigurationException {
    final Locator locator = new Locator();
    final List<LocatorStrategy> strategies = new ArrayList<LocatorStrategy>();
    strategies.add(new RelativeFileLocatorStrategy(configSource.getBasedir()));
    strategies.add(new FileLocatorStrategy());
    final List<LocatorStrategy> refStrategies = new ArrayList<LocatorStrategy>();
    refStrategies.add(new PrefixedClasspathLocatorStrategy("/assemblies/"));
    final List<Assembly> assemblies = new ArrayList<Assembly>();
    final String[] descriptors = configSource.getDescriptors();
    final String[] descriptorRefs = configSource.getDescriptorReferences();
    final File descriptorSourceDirectory = configSource.getDescriptorSourceDirectory();
    if ((descriptors != null) && (descriptors.length > 0)) {
        locator.setStrategies(strategies);
        for (String descriptor1 : descriptors) {
            getLogger().info("Reading assembly descriptor: " + descriptor1);
            addAssemblyFromDescriptor(descriptor1, locator, configSource, assemblies);
        }
    }
    if ((descriptorRefs != null) && (descriptorRefs.length > 0)) {
        locator.setStrategies(refStrategies);
        for (String descriptorRef : descriptorRefs) {
            addAssemblyForDescriptorReference(descriptorRef, configSource, assemblies);
        }
    }
    if ((descriptorSourceDirectory != null) && descriptorSourceDirectory.isDirectory()) {
        // CHECKSTYLE_OFF: LineLength
        locator.setStrategies(Collections.<LocatorStrategy>singletonList(new RelativeFileLocatorStrategy(descriptorSourceDirectory)));
        // CHECKSTYLE_ON: LineLength
        final DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(descriptorSourceDirectory);
        scanner.setIncludes(new String[] { "**/*.xml" });
        scanner.addDefaultExcludes();
        scanner.scan();
        final String[] paths = scanner.getIncludedFiles();
        for (String path : paths) {
            addAssemblyFromDescriptor(path, locator, configSource, assemblies);
        }
    }
    if (assemblies.isEmpty()) {
        if (configSource.isIgnoreMissingDescriptor()) {
            getLogger().debug("Ignoring missing assembly descriptors per configuration. " + "See messages above for specifics.");
        } else {
            throw new AssemblyReadException("No assembly descriptors found.");
        }
    }
    // check unique IDs
    final Set<String> ids = new HashSet<String>();
    for (final Assembly assembly : assemblies) {
        if (!ids.add(assembly.getId())) {
            getLogger().warn("The assembly id " + assembly.getId() + " is used more than once.");
        }
    }
    return assemblies;
}
Also used : FileLocatorStrategy(org.apache.maven.shared.io.location.FileLocatorStrategy) ArrayList(java.util.ArrayList) ClasspathResourceLocatorStrategy(org.apache.maven.shared.io.location.ClasspathResourceLocatorStrategy) LocatorStrategy(org.apache.maven.shared.io.location.LocatorStrategy) FileLocatorStrategy(org.apache.maven.shared.io.location.FileLocatorStrategy) Locator(org.apache.maven.shared.io.location.Locator) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) File(java.io.File) Assembly(org.apache.maven.plugins.assembly.model.Assembly) HashSet(java.util.HashSet)

Example 2 with FileLocatorStrategy

use of org.apache.maven.shared.io.location.FileLocatorStrategy in project maven-plugins by apache.

the class DefaultAssemblyReader method mergeComponentsWithMainAssembly.

/**
 * Add the contents of all included components to main assembly
 *
 * @param assembly The assembly
 * @param assemblyDir The assembly directory
 * @param transformer The component interpolator
 * @throws AssemblyReadException .
 */
protected void mergeComponentsWithMainAssembly(final Assembly assembly, final File assemblyDir, final AssemblerConfigurationSource configSource, ComponentXpp3Reader.ContentTransformer transformer) throws AssemblyReadException {
    final Locator locator = new Locator();
    if (assemblyDir != null && assemblyDir.exists() && assemblyDir.isDirectory()) {
        locator.addStrategy(new RelativeFileLocatorStrategy(assemblyDir));
    }
    // allow absolute paths in componentDescriptor... MASSEMBLY-486
    locator.addStrategy(new RelativeFileLocatorStrategy(configSource.getBasedir()));
    locator.addStrategy(new FileLocatorStrategy());
    locator.addStrategy(new ClasspathResourceLocatorStrategy());
    final AssemblyExpressionEvaluator aee = new AssemblyExpressionEvaluator(configSource);
    final List<String> componentLocations = assembly.getComponentDescriptors();
    for (String location : componentLocations) {
        // allow expressions in path to component descriptor... MASSEMBLY-486
        try {
            location = aee.evaluate(location).toString();
        } catch (final Exception eee) {
            getLogger().error("Error interpolating componentDescriptor: " + location, eee);
        }
        final Location resolvedLocation = locator.resolve(location);
        if (resolvedLocation == null) {
            throw new AssemblyReadException("Failed to locate component descriptor: " + location);
        }
        Component component = null;
        Reader reader = null;
        try {
            reader = new InputStreamReader(resolvedLocation.getInputStream());
            component = new ComponentXpp3Reader(transformer).read(reader);
        } catch (final IOException e) {
            throw new AssemblyReadException("Error reading component descriptor: " + location + " (resolved to: " + resolvedLocation.getSpecification() + ")", e);
        } catch (final XmlPullParserException e) {
            throw new AssemblyReadException("Error reading component descriptor: " + location + " (resolved to: " + resolvedLocation.getSpecification() + ")", e);
        } finally {
            IOUtil.close(reader);
        }
        mergeComponentWithAssembly(component, assembly);
    }
}
Also used : AssemblyExpressionEvaluator(org.apache.maven.plugins.assembly.interpolation.AssemblyExpressionEvaluator) InputStreamReader(java.io.InputStreamReader) FileLocatorStrategy(org.apache.maven.shared.io.location.FileLocatorStrategy) ComponentXpp3Reader(org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Reader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) AssemblyXpp3Reader(org.apache.maven.plugins.assembly.model.io.xpp3.AssemblyXpp3Reader) IOException(java.io.IOException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) InvalidAssemblerConfigurationException(org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException) Locator(org.apache.maven.shared.io.location.Locator) ComponentXpp3Reader(org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Reader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) Component(org.apache.maven.plugins.assembly.model.Component) ClasspathResourceLocatorStrategy(org.apache.maven.shared.io.location.ClasspathResourceLocatorStrategy) Location(org.apache.maven.shared.io.location.Location)

Aggregations

ClasspathResourceLocatorStrategy (org.apache.maven.shared.io.location.ClasspathResourceLocatorStrategy)2 FileLocatorStrategy (org.apache.maven.shared.io.location.FileLocatorStrategy)2 Locator (org.apache.maven.shared.io.location.Locator)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 InvalidAssemblerConfigurationException (org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException)1 AssemblyExpressionEvaluator (org.apache.maven.plugins.assembly.interpolation.AssemblyExpressionEvaluator)1 Assembly (org.apache.maven.plugins.assembly.model.Assembly)1 Component (org.apache.maven.plugins.assembly.model.Component)1 AssemblyXpp3Reader (org.apache.maven.plugins.assembly.model.io.xpp3.AssemblyXpp3Reader)1 ComponentXpp3Reader (org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Reader)1 Location (org.apache.maven.shared.io.location.Location)1 LocatorStrategy (org.apache.maven.shared.io.location.LocatorStrategy)1 DirectoryScanner (org.codehaus.plexus.util.DirectoryScanner)1 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)1