Search in sources :

Example 1 with PomDependencyData

use of org.apache.ivy.plugins.parser.m2.PomReader.PomDependencyData in project ant-ivy by apache.

the class PomModuleDescriptorParser method parseDescriptor.

public ModuleDescriptor parseDescriptor(ParserSettings ivySettings, URL descriptorURL, Resource res, boolean validate) throws ParseException, IOException {
    PomModuleDescriptorBuilder mdBuilder = new PomModuleDescriptorBuilder(this, res, ivySettings);
    try {
        final IvyContext ivyContext = IvyContext.pushNewCopyContext();
        HashSet<ModuleRevisionId> parents = ivyContext.get(PARENT_MAP_KEY);
        if (parents == null) {
            parents = new LinkedHashSet<>();
            ivyContext.set(PARENT_MAP_KEY, parents);
        }
        PomReader domReader = new PomReader(descriptorURL, res);
        domReader.setProperty("parent.version", domReader.getParentVersion());
        domReader.setProperty("parent.groupId", domReader.getParentGroupId());
        domReader.setProperty("project.parent.version", domReader.getParentVersion());
        domReader.setProperty("project.parent.groupId", domReader.getParentGroupId());
        Message.debug("parent.groupId: " + domReader.getParentGroupId());
        Message.debug("parent.artifactId: " + domReader.getParentArtifactId());
        Message.debug("parent.version: " + domReader.getParentVersion());
        for (final Map.Entry<String, String> prop : domReader.getPomProperties().entrySet()) {
            domReader.setProperty(prop.getKey(), prop.getValue());
            mdBuilder.addProperty(prop.getKey(), prop.getValue());
        }
        final List<PomProfileElement> activeProfiles = new ArrayList<>();
        // add profile specific properties
        for (final PomProfileElement profile : domReader.getProfiles()) {
            if (!profile.isActive()) {
                continue;
            }
            // keep track of this active profile for later use
            activeProfiles.add(profile);
            final Map<String, String> profileProps = profile.getProfileProperties();
            if (profileProps.isEmpty()) {
                continue;
            }
            for (final Map.Entry<String, String> entry : profileProps.entrySet()) {
                domReader.setProperty(entry.getKey(), entry.getValue());
                mdBuilder.addProperty(entry.getKey(), entry.getValue());
            }
        }
        ModuleDescriptor parentDescr = null;
        if (domReader.hasParent()) {
            // Is there any other parent properties?
            ModuleRevisionId parentModRevID = ModuleRevisionId.newInstance(domReader.getParentGroupId(), domReader.getParentArtifactId(), domReader.getParentVersion());
            // check for cycles
            if (parents.contains(parentModRevID)) {
                throw new CircularDependencyException(parents);
            } else {
                parents.add(parentModRevID);
            }
            ResolvedModuleRevision parentModule = parseOtherPom(ivySettings, parentModRevID);
            if (parentModule == null) {
                throw new IOException("Impossible to load parent for " + res.getName() + ". Parent=" + parentModRevID);
            }
            parentDescr = parentModule.getDescriptor();
            if (parentDescr != null) {
                for (Map.Entry<String, String> prop : extractPomProperties(parentDescr.getExtraInfos()).entrySet()) {
                    domReader.setProperty(prop.getKey(), prop.getValue());
                }
            }
        }
        String groupId = domReader.getGroupId();
        String artifactId = domReader.getArtifactId();
        String version = domReader.getVersion();
        mdBuilder.setModuleRevId(groupId, artifactId, version);
        mdBuilder.setHomePage(domReader.getHomePage());
        mdBuilder.setDescription(domReader.getDescription());
        // if this module doesn't have an explicit license, use the parent's license (if any)
        final License[] licenses = domReader.getLicenses();
        if (licenses != null && licenses.length > 0) {
            mdBuilder.setLicenses(licenses);
        } else if (parentDescr != null) {
            mdBuilder.setLicenses(parentDescr.getLicenses());
        }
        ModuleRevisionId relocation = domReader.getRelocation();
        if (relocation != null) {
            if (groupId != null && artifactId != null && artifactId.equals(relocation.getName()) && groupId.equals(relocation.getOrganisation())) {
                Message.error("Relocation to an other version number not supported in ivy : " + mdBuilder.getModuleDescriptor().getModuleRevisionId() + " relocated to " + relocation + ". Please update your dependency to directly use the right version.");
                Message.warn("Resolution will only pick dependencies of the relocated element." + "  Artifact and other metadata will be ignored.");
                ResolvedModuleRevision relocatedModule = parseOtherPom(ivySettings, relocation);
                if (relocatedModule == null) {
                    throw new ParseException("impossible to load module " + relocation + " to which " + mdBuilder.getModuleDescriptor().getModuleRevisionId() + " has been relocated", 0);
                }
                for (DependencyDescriptor dd : relocatedModule.getDescriptor().getDependencies()) {
                    mdBuilder.addDependency(dd);
                }
            } else {
                Message.info(mdBuilder.getModuleDescriptor().getModuleRevisionId() + " is relocated to " + relocation + ". Please update your dependencies.");
                Message.verbose("Relocated module will be considered as a dependency");
                DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(mdBuilder.getModuleDescriptor(), relocation, true, false, true);
                /* Map all public dependencies */
                for (Configuration m2Conf : MAVEN2_CONFIGURATIONS) {
                    if (PUBLIC.equals(m2Conf.getVisibility())) {
                        dd.addDependencyConfiguration(m2Conf.getName(), m2Conf.getName());
                    }
                }
                mdBuilder.addDependency(dd);
            }
        } else {
            domReader.setProperty("project.groupId", groupId);
            domReader.setProperty("pom.groupId", groupId);
            domReader.setProperty("groupId", groupId);
            domReader.setProperty("project.artifactId", artifactId);
            domReader.setProperty("pom.artifactId", artifactId);
            domReader.setProperty("artifactId", artifactId);
            domReader.setProperty("project.version", version);
            domReader.setProperty("pom.version", version);
            domReader.setProperty("version", version);
            if (parentDescr != null) {
                mdBuilder.addExtraInfos(parentDescr.getExtraInfos());
                // add dependency management info from parent
                for (PomDependencyMgt dep : getDependencyManagements(parentDescr)) {
                    if (dep instanceof PomDependencyMgtElement) {
                        dep = domReader.new PomDependencyMgtElement((PomDependencyMgtElement) dep);
                    }
                    mdBuilder.addDependencyMgt(dep);
                }
                // add plugins from parent
                for (PomDependencyMgt pomDependencyMgt : getPlugins(parentDescr)) {
                    mdBuilder.addPlugin(pomDependencyMgt);
                }
            }
            for (PomDependencyMgt dep : domReader.getDependencyMgt()) {
                addTo(mdBuilder, dep, ivySettings);
            }
            for (PomDependencyData dep : domReader.getDependencies()) {
                mdBuilder.addDependency(res, dep);
            }
            for (PomPluginElement plugin : domReader.getPlugins()) {
                mdBuilder.addPlugin(plugin);
            }
            // consult active profiles:
            for (final PomProfileElement activeProfile : activeProfiles) {
                for (PomDependencyMgt dep : activeProfile.getDependencyMgt()) {
                    addTo(mdBuilder, dep, ivySettings);
                }
                for (PomDependencyData dep : activeProfile.getDependencies()) {
                    mdBuilder.addDependency(res, dep);
                }
                for (PomPluginElement plugin : activeProfile.getPlugins()) {
                    mdBuilder.addPlugin(plugin);
                }
            }
            if (parentDescr != null) {
                for (DependencyDescriptor descriptor : parentDescr.getDependencies()) {
                    if (descriptor instanceof PomDependencyDescriptor) {
                        PomDependencyData parentDep = ((PomDependencyDescriptor) descriptor).getPomDependencyData();
                        PomDependencyData dep = domReader.new PomDependencyData(parentDep);
                        mdBuilder.addDependency(res, dep);
                    } else {
                        mdBuilder.addDependency(descriptor);
                    }
                }
            }
            mdBuilder.addMainArtifact(artifactId, domReader.getPackaging());
            addSourcesAndJavadocArtifactsIfPresent(mdBuilder, ivySettings);
        }
    } catch (SAXException e) {
        throw newParserException(e);
    } finally {
        IvyContext.popContext();
    }
    return mdBuilder.getModuleDescriptor();
}
Also used : Configuration(org.apache.ivy.core.module.descriptor.Configuration) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) PomDependencyDescriptor(org.apache.ivy.plugins.parser.m2.PomModuleDescriptorBuilder.PomDependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) ArrayList(java.util.ArrayList) License(org.apache.ivy.core.module.descriptor.License) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) SAXException(org.xml.sax.SAXException) PomDependencyMgtElement(org.apache.ivy.plugins.parser.m2.PomReader.PomDependencyMgtElement) IvyContext(org.apache.ivy.core.IvyContext) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) PomProfileElement(org.apache.ivy.plugins.parser.m2.PomReader.PomProfileElement) CircularDependencyException(org.apache.ivy.plugins.circular.CircularDependencyException) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) IOException(java.io.IOException) PomPluginElement(org.apache.ivy.plugins.parser.m2.PomReader.PomPluginElement) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) PomDependencyDescriptor(org.apache.ivy.plugins.parser.m2.PomModuleDescriptorBuilder.PomDependencyDescriptor) PomDependencyData(org.apache.ivy.plugins.parser.m2.PomReader.PomDependencyData) ParseException(java.text.ParseException) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 IvyContext (org.apache.ivy.core.IvyContext)1 Configuration (org.apache.ivy.core.module.descriptor.Configuration)1 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)1 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)1 License (org.apache.ivy.core.module.descriptor.License)1 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)1 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)1 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)1 CircularDependencyException (org.apache.ivy.plugins.circular.CircularDependencyException)1 PomDependencyDescriptor (org.apache.ivy.plugins.parser.m2.PomModuleDescriptorBuilder.PomDependencyDescriptor)1 PomDependencyData (org.apache.ivy.plugins.parser.m2.PomReader.PomDependencyData)1 PomDependencyMgtElement (org.apache.ivy.plugins.parser.m2.PomReader.PomDependencyMgtElement)1 PomPluginElement (org.apache.ivy.plugins.parser.m2.PomReader.PomPluginElement)1 PomProfileElement (org.apache.ivy.plugins.parser.m2.PomReader.PomProfileElement)1 SAXException (org.xml.sax.SAXException)1