Search in sources :

Example 6 with InterpolationException

use of org.codehaus.plexus.interpolation.InterpolationException in project maven-plugins by apache.

the class DocumentDescriptorReader method readAndFilterDocumentDescriptor.

/**
 * Read and filter the <code>docDescriptor</code> file.
 *
 * @param docDescriptor not null, corresponding to non-localized descriptor file.
 * @return a DocumentModel instance.
 * @throws XmlPullParserException if an error occurs during parsing.
 * @throws IOException if an error occurs during reading.
 */
public DocumentModel readAndFilterDocumentDescriptor(File docDescriptor) throws XmlPullParserException, IOException {
    if (locale != null) {
        String descriptorFilename = docDescriptor.getName();
        String localized = FileUtils.removeExtension(descriptorFilename) + '_' + locale.getLanguage() + '.' + FileUtils.getExtension(descriptorFilename);
        File localizedDocDescriptor = new File(docDescriptor.getParentFile(), localized);
        if (localizedDocDescriptor.exists()) {
            docDescriptor = localizedDocDescriptor;
        }
    }
    Reader reader = null;
    try {
        // System properties
        final Properties filterProperties = System.getProperties();
        // Project properties
        if (project != null && project.getProperties() != null) {
            filterProperties.putAll(project.getProperties());
        }
        final Interpolator interpolator = new RegexBasedInterpolator();
        interpolator.addValueSource(new MapBasedValueSource(filterProperties));
        interpolator.addValueSource(new EnvarBasedValueSource());
        interpolator.addValueSource(new ObjectBasedValueSource(project) {

            /**
             * {@inheritDoc}
             */
            public Object getValue(final String expression) {
                try {
                    return ReflectionValueExtractor.evaluate(expression, project);
                } catch (Exception e) {
                    addFeedback("Failed to extract \'" + expression + "\' from: " + project, e);
                }
                return null;
            }
        });
        final DateBean bean = new DateBean();
        interpolator.addValueSource(new ObjectBasedValueSource(bean));
        reader = ReaderFactory.newXmlReader(docDescriptor);
        final String interpolatedDoc = interpolator.interpolate(IOUtil.toString(reader));
        reader.close();
        reader = null;
        if (log != null && log.isDebugEnabled()) {
            log.debug("Interpolated document descriptor (" + docDescriptor.getAbsolutePath() + ")\n" + interpolatedDoc);
        }
        // No Strict
        return new DocumentXpp3Reader().read(new StringReader(interpolatedDoc), false);
    } catch (InterpolationException e) {
        final IOException io = new IOException("Error interpolating document descriptor");
        io.initCause(e);
        throw io;
    } finally {
        IOUtil.close(reader);
    }
}
Also used : RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) DocumentXpp3Reader(org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader) Reader(java.io.Reader) StringReader(java.io.StringReader) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) IOException(java.io.IOException) Properties(java.util.Properties) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) DocumentXpp3Reader(org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader) StringReader(java.io.StringReader) Interpolator(org.codehaus.plexus.interpolation.Interpolator) RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) ObjectBasedValueSource(org.codehaus.plexus.interpolation.ObjectBasedValueSource) File(java.io.File) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource) EnvarBasedValueSource(org.codehaus.plexus.interpolation.EnvarBasedValueSource)

Example 7 with InterpolationException

use of org.codehaus.plexus.interpolation.InterpolationException in project maven-plugins by apache.

the class ArtifactsPackagingTask method performPackaging.

/**
 * {@inheritDoc}
 */
public void performPackaging(WarPackagingContext context) throws MojoExecutionException {
    try {
        final ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
        final List<String> duplicates = findDuplicates(context, artifacts);
        for (Artifact artifact : artifacts) {
            String targetFileName = getArtifactFinalName(context, artifact);
            context.getLog().debug("Processing: " + targetFileName);
            if (duplicates.contains(targetFileName)) {
                context.getLog().debug("Duplicate found: " + targetFileName);
                targetFileName = artifact.getGroupId() + "-" + targetFileName;
                context.getLog().debug("Renamed to: " + targetFileName);
            }
            context.getWebappStructure().registerTargetFileName(artifact, targetFileName);
            if (!artifact.isOptional() && filter.include(artifact)) {
                try {
                    String type = artifact.getType();
                    if ("tld".equals(type)) {
                        copyFile(id, context, artifact.getFile(), TLD_PATH + targetFileName);
                    } else if ("aar".equals(type)) {
                        copyFile(id, context, artifact.getFile(), SERVICES_PATH + targetFileName);
                    } else if ("mar".equals(type)) {
                        copyFile(id, context, artifact.getFile(), MODULES_PATH + targetFileName);
                    } else if ("xar".equals(type)) {
                        copyFile(id, context, artifact.getFile(), EXTENSIONS_PATH + targetFileName);
                    } else if ("jar".equals(type) || "ejb".equals(type) || "ejb-client".equals(type) || "test-jar".equals(type) || "bundle".equals(type)) {
                        copyFile(id, context, artifact.getFile(), LIB_PATH + targetFileName);
                    } else if ("par".equals(type)) {
                        targetFileName = targetFileName.substring(0, targetFileName.lastIndexOf('.')) + ".jar";
                        copyFile(id, context, artifact.getFile(), LIB_PATH + targetFileName);
                    } else if ("war".equals(type)) {
                        // Nothing to do here, it is an overlay and it's already handled
                        context.getLog().debug("war artifacts are handled as overlays, ignoring [" + artifact + "]");
                    } else if ("zip".equals(type)) {
                        // Nothing to do here, it is an overlay and it's already handled
                        context.getLog().debug("zip artifacts are handled as overlays, ignoring [" + artifact + "]");
                    } else {
                        context.getLog().debug("Artifact of type [" + type + "] is not supported, ignoring [" + artifact + "]");
                    }
                } catch (IOException e) {
                    throw new MojoExecutionException("Failed to copy file for artifact [" + artifact + "]", e);
                }
            }
        }
    } catch (InterpolationException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) Artifact(org.apache.maven.artifact.Artifact)

Example 8 with InterpolationException

use of org.codehaus.plexus.interpolation.InterpolationException in project maven-plugins by apache.

the class DoapUtil method interpolate.

/**
 * Interpolate a string with project and settings.
 *
 * @param value could be null
 * @param project not null
 * @param settings could be null
 * @return the value trimmed and interpolated or null if the interpolation doesn't work.
 * @since 1.1
 */
public static String interpolate(String value, final MavenProject project, Settings settings) {
    if (project == null) {
        throw new IllegalArgumentException("project is required");
    }
    if (value == null) {
        return value;
    }
    if (!value.contains("${")) {
        return value.trim();
    }
    RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
    try {
        interpolator.addValueSource(new EnvarBasedValueSource());
    } catch (IOException e) {
    // ignore
    }
    interpolator.addValueSource(new PropertiesBasedValueSource(System.getProperties()));
    interpolator.addValueSource(new PropertiesBasedValueSource(project.getProperties()));
    interpolator.addValueSource(new PrefixedObjectValueSource("project", project));
    interpolator.addValueSource(new PrefixedObjectValueSource("pom", project));
    interpolator.addValueSource(new ObjectBasedValueSource(project) {

        @Override
        public Object getValue(String expression) {
            try {
                return ReflectionValueExtractor.evaluate(expression, project, true);
            } catch (Exception e) {
                addFeedback("Failed to extract \'" + expression + "\' from: " + project, e);
            }
            return null;
        }
    });
    if (settings != null) {
        interpolator.addValueSource(new PrefixedObjectValueSource("settings", settings));
    }
    String interpolatedValue = value;
    try {
        interpolatedValue = interpolator.interpolate(value).trim();
    } catch (InterpolationException e) {
    // ignore
    }
    if (interpolatedValue.startsWith("${")) {
        return null;
    }
    return interpolatedValue;
}
Also used : RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) ObjectBasedValueSource(org.codehaus.plexus.interpolation.ObjectBasedValueSource) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) EnvarBasedValueSource(org.codehaus.plexus.interpolation.EnvarBasedValueSource) PropertiesBasedValueSource(org.codehaus.plexus.interpolation.PropertiesBasedValueSource) FileNotFoundException(java.io.FileNotFoundException) SocketTimeoutException(java.net.SocketTimeoutException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) PrefixedObjectValueSource(org.codehaus.plexus.interpolation.PrefixedObjectValueSource)

Example 9 with InterpolationException

use of org.codehaus.plexus.interpolation.InterpolationException in project maven-plugins by apache.

the class GenerateApplicationXmlMojo method buildEnvEntries.

/**
 * Builds the env-entries based on the configuration.
 *
 * @return a list of EnvEntry object(s)
 * @throws EarPluginException if the configuration is invalid
 */
private List<EnvEntry> buildEnvEntries() throws EarPluginException {
    final List<EnvEntry> result = new ArrayList<EnvEntry>();
    if (envEntries == null) {
        return result;
    }
    try {
        StringSearchInterpolator ssi = new StringSearchInterpolator();
        ValueSource vs = new MapBasedValueSource(project.getProperties());
        ssi.addValueSource(vs);
        final PlexusConfiguration[] allEnvEntries = envEntries.getChildren(EnvEntry.ENV_ENTRY);
        getLog().debug("buildEnvEntries: allEnvEntries:" + allEnvEntries);
        getLog().debug("buildEnvEntries: allEnvEntries size:" + allEnvEntries.length);
        for (PlexusConfiguration envEntry : allEnvEntries) {
            // CHECKSTYLE_OFF: LineLength
            final String childDescription = interpolate(ssi, envEntry.getChild(EnvEntry.DESCRIPTION).getValue());
            final String childEnvEntryName = interpolate(ssi, envEntry.getChild(EnvEntry.ENV_ENTRY_NAME).getValue());
            final String childEnvEntryType = interpolate(ssi, envEntry.getChild(EnvEntry.ENV_ENTRY_TYPE).getValue());
            final String childEnvEntryValue = interpolate(ssi, envEntry.getChild(EnvEntry.ENV_ENTRY_VALUE).getValue());
            final String childEnvLookupNameValue = interpolate(ssi, envEntry.getChild(EnvEntry.ENV_LOOKUP_NAME).getValue());
            try {
                result.add(new EnvEntry(childDescription, childEnvEntryName, childEnvEntryType, childEnvEntryValue, childEnvLookupNameValue));
            } catch (IllegalArgumentException e) {
                throw new EarPluginException("Invalid env-entry [" + envEntry + "]", e);
            }
        }
        return result;
    } catch (InterpolationException e) {
        throw new EarPluginException("Interpolation exception:", e);
    }
}
Also used : PlexusConfiguration(org.codehaus.plexus.configuration.PlexusConfiguration) StringSearchInterpolator(org.codehaus.plexus.interpolation.StringSearchInterpolator) ValueSource(org.codehaus.plexus.interpolation.ValueSource) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource) ArrayList(java.util.ArrayList) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource)

Example 10 with InterpolationException

use of org.codehaus.plexus.interpolation.InterpolationException in project indy by Commonjava.

the class KojiUtils method getRepositoryName.

public String getRepositoryName(final KojiBuildInfo build) {
    StringSearchInterpolator interpolator = new StringSearchInterpolator();
    interpolator.addValueSource(new ObjectBasedValueSource(build));
    try {
        return interpolator.interpolate(isBinaryBuild(build) ? config.getBinayNamingFormat() : config.getNamingFormat());
    } catch (InterpolationException e) {
        throw new RuntimeException("Cannot resolve expressions in Koji configuration.", e);
    }
}
Also used : StringSearchInterpolator(org.codehaus.plexus.interpolation.StringSearchInterpolator) ObjectBasedValueSource(org.codehaus.plexus.interpolation.ObjectBasedValueSource) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException)

Aggregations

InterpolationException (org.codehaus.plexus.interpolation.InterpolationException)19 IOException (java.io.IOException)11 StringSearchInterpolator (org.codehaus.plexus.interpolation.StringSearchInterpolator)10 MapBasedValueSource (org.codehaus.plexus.interpolation.MapBasedValueSource)7 File (java.io.File)6 RegexBasedInterpolator (org.codehaus.plexus.interpolation.RegexBasedInterpolator)5 ArrayList (java.util.ArrayList)4 Properties (java.util.Properties)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 EnvarBasedValueSource (org.codehaus.plexus.interpolation.EnvarBasedValueSource)4 ObjectBasedValueSource (org.codehaus.plexus.interpolation.ObjectBasedValueSource)4 PropertiesBasedValueSource (org.codehaus.plexus.interpolation.PropertiesBasedValueSource)4 PlexusConfiguration (org.codehaus.plexus.configuration.PlexusConfiguration)3 Interpolator (org.codehaus.plexus.interpolation.Interpolator)3 ValueSource (org.codehaus.plexus.interpolation.ValueSource)3 InputStream (java.io.InputStream)2 Artifact (org.apache.maven.artifact.Artifact)2 Activation (org.apache.maven.model.Activation)2 ActivationFile (org.apache.maven.model.ActivationFile)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1