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

com.marvelution.maven.components.jar.identification.exposer.EnhancedEmbeddedMavenModelExposer Maven / Gradle / Ivy

/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution 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.
 */

package com.marvelution.maven.components.jar.identification.exposer;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.regex.Pattern;

import org.apache.maven.model.Model;
import org.apache.maven.model.Organization;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.shared.jar.JarAnalyzer;
import org.apache.maven.shared.jar.identification.JarIdentification;
import org.apache.maven.shared.jar.identification.JarIdentificationExposer;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import com.marvelution.utils.StringUtils;

/**
 * Enhanced Embedded Mavel Model exposer for {@link JarIdentificationAnalysis}
 * 

* Note: If not using Plexus, the following must be set before executing * {@link #expose(JarIdentification, JarAnalyzer)}: *

    *
  • the {@link Logger} implementation using method {@link #enableLogging(org.codehaus.plexus.logging.Logger)}
  • *
*

* * @author Mark Rekveld */ public class EnhancedEmbeddedMavenModelExposer extends AbstractLogEnabled implements JarIdentificationExposer { /** * pom.properties pattern */ private static final Pattern MAVEN_POM_PROPERTIES_FILTER = Pattern.compile("META-INF/maven/.*/pom\\.properties$"); /** * {@inheritDoc} */ public void expose(JarIdentification identification, JarAnalyzer jarAnalyzer) { List entries = jarAnalyzer.getMavenPomEntries(); if (!entries.isEmpty()) { getLogger().debug("Exposing Jar Identification using Enhanced Embedded Maven Model Exposer"); exposeByXml(identification, jarAnalyzer); } entries = jarAnalyzer.filterEntries(MAVEN_POM_PROPERTIES_FILTER); if (!entries.isEmpty()) { getLogger().debug("Exposing Jar Identification using Enhanced Embedded Maven Model Properties Exposer"); exposeByProperties(identification, jarAnalyzer); } } /** * Exposer that will read the pom.xml file in the JarFile * * @param identification the {@link JarIdentification} * @param jarAnalyzer the {@link JarAnalyzer} */ private void exposeByXml(JarIdentification identification, JarAnalyzer jarAnalyzer) { final List entries = jarAnalyzer.getMavenPomEntries(); if (entries.size() > 1) { getLogger().warn( "More than one Maven model entry was found in the JAR, using only the first of: " + entries); } final JarEntry pom = (JarEntry) entries.get(0); final MavenXpp3Reader pomreader = new MavenXpp3Reader(); InputStream inputStream = null; try { inputStream = jarAnalyzer.getEntryInputStream(pom); final InputStreamReader isreader = new InputStreamReader(inputStream); final Model model = pomreader.read(isreader); identification.addAndSetGroupId(model.getGroupId()); if (StringUtils.isEmpty(model.getGroupId()) && model.getParent() != null && StringUtils.isNotEmpty(model.getParent().getGroupId())) { identification.addAndSetGroupId(model.getParent().getGroupId()); } identification.addAndSetArtifactId(model.getArtifactId()); identification.addAndSetVersion(model.getVersion()); identification.addAndSetName(model.getName()); if (StringUtils.isEmpty(model.getName())) { identification.addAndSetName(model.getArtifactId()); } final Organization org = model.getOrganization(); if (org != null) { identification.addAndSetVendor(org.getName()); } } catch (IOException e) { getLogger().error("Unable to read model " + pom.getName() + " in " + jarAnalyzer.getFile() + ".", e); } catch (XmlPullParserException e) { getLogger().error("Unable to parse model " + pom.getName() + " in " + jarAnalyzer.getFile() + ".", e); } finally { IOUtil.close(inputStream); } } /** * Exposer that will read the pom.properties file in the JarFile * * @param identification the {@link JarIdentification} * @param jarAnalyzer the {@link JarAnalyzer} */ private void exposeByProperties(JarIdentification identification, JarAnalyzer jarAnalyzer) { final List entries = jarAnalyzer.filterEntries(MAVEN_POM_PROPERTIES_FILTER); if (entries.size() > 1) { getLogger().warn( "More than one Maven model properties entry was found in the JAR, using only the first of: " + entries); } final JarEntry pom = (JarEntry) entries.get(0); InputStream inputStream = null; try { inputStream = jarAnalyzer.getEntryInputStream(pom); final Properties props = new Properties(); props.load(inputStream); identification.addAndSetGroupId(props.getProperty("groupId")); identification.addAndSetArtifactId(props.getProperty("artifactId")); identification.addAndSetVersion(props.getProperty("version")); } catch (IOException e) { getLogger().error( "Unable to read model properties " + pom.getName() + " in " + jarAnalyzer.getFile() + ".", e); } finally { IOUtil.close(inputStream); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy