Search in sources :

Example 1 with License

use of org.apache.ivy.core.module.descriptor.License 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)

Example 2 with License

use of org.apache.ivy.core.module.descriptor.License in project ant-ivy by apache.

the class XmlModuleDescriptorWriter method printInfoTag.

private static void printInfoTag(ModuleDescriptor md, PrintWriter out) {
    out.println("\t<info organisation=\"" + XMLHelper.escape(md.getModuleRevisionId().getOrganisation()) + "\"");
    out.println("\t\tmodule=\"" + XMLHelper.escape(md.getModuleRevisionId().getName()) + "\"");
    String branch = md.getResolvedModuleRevisionId().getBranch();
    if (branch != null) {
        out.println("\t\tbranch=\"" + XMLHelper.escape(branch) + "\"");
    }
    String revision = md.getResolvedModuleRevisionId().getRevision();
    if (revision != null) {
        out.println("\t\trevision=\"" + XMLHelper.escape(revision) + "\"");
    }
    out.println("\t\tstatus=\"" + XMLHelper.escape(md.getStatus()) + "\"");
    out.println("\t\tpublication=\"" + DateUtil.format(md.getResolvedPublicationDate()) + "\"");
    if (md.isDefault()) {
        out.println("\t\tdefault=\"true\"");
    }
    if (md instanceof DefaultModuleDescriptor) {
        DefaultModuleDescriptor dmd = (DefaultModuleDescriptor) md;
        if (dmd.getNamespace() != null && !dmd.getNamespace().getName().equals("system")) {
            out.println("\t\tnamespace=\"" + XMLHelper.escape(dmd.getNamespace().getName()) + "\"");
        }
    }
    if (!md.getExtraAttributes().isEmpty()) {
        printExtraAttributes(md, out, "\t\t");
        out.println();
    }
    if (requireInnerInfoElement(md)) {
        out.println("\t>");
        for (ExtendsDescriptor parent : md.getInheritedDescriptors()) {
            ModuleRevisionId mrid = parent.getParentRevisionId();
            out.print(String.format("\t\t<extends organisation=\"%s\" module=\"%s\" revision=\"%s\"", XMLHelper.escape(mrid.getOrganisation()), XMLHelper.escape(mrid.getName()), XMLHelper.escape(mrid.getRevision())));
            String location = parent.getLocation();
            if (location != null) {
                out.print(" location=\"" + XMLHelper.escape(location) + "\"");
            }
            out.print(" extendType=\"" + joinArray(parent.getExtendsTypes(), ",") + "\"");
            out.println("/>");
        }
        License[] licenses = md.getLicenses();
        for (License license : licenses) {
            out.print("\t\t<license ");
            if (license.getName() != null) {
                out.print("name=\"" + XMLHelper.escape(license.getName()) + "\" ");
            }
            if (license.getUrl() != null) {
                out.print("url=\"" + XMLHelper.escape(license.getUrl()) + "\" ");
            }
            out.println("/>");
        }
        if (md.getHomePage() != null || md.getDescription() != null) {
            out.print("\t\t<description");
            if (md.getHomePage() != null) {
                out.print(" homepage=\"" + XMLHelper.escape(md.getHomePage()) + "\"");
            }
            if (isNullOrEmpty(md.getDescription())) {
                out.println(" />");
            } else {
                out.println(">");
                out.println("\t\t" + XMLHelper.escape(md.getDescription()));
                out.println("\t\t</description>");
            }
        }
        for (ExtraInfoHolder extraInfo : md.getExtraInfos()) {
            printExtraInfoElement(out, extraInfo, 2);
        }
        out.println("\t</info>");
    } else {
        out.println("\t/>");
    }
}
Also used : ExtendsDescriptor(org.apache.ivy.core.module.descriptor.ExtendsDescriptor) ExtraInfoHolder(org.apache.ivy.core.module.descriptor.ExtraInfoHolder) License(org.apache.ivy.core.module.descriptor.License) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)

Example 3 with License

use of org.apache.ivy.core.module.descriptor.License in project ant-ivy by apache.

the class PomModuleDescriptorParserTest method testOverriddenLicense.

/**
 * Tests that if a project explicitly specifies the licenses, then the licenses (if any) from
 * its parent pom aren't applied to the child project
 *
 * @throws Exception if something goes wrong
 */
@Test
public void testOverriddenLicense() throws Exception {
    final IvySettings customIvySettings = createIvySettingsForParentLicenseTesting("test-parent-with-licenses.pom", "org.apache", "test-ivy-license-parent");
    final String pomFile = "test-project-with-overridden-licenses.pom";
    final ModuleDescriptor childModule = PomModuleDescriptorParser.getInstance().parseDescriptor(customIvySettings, this.getClass().getResource(pomFile), false);
    assertNotNull("Could not find " + pomFile, pomFile);
    final License[] licenses = childModule.getLicenses();
    assertNotNull("No licenses found in the module " + childModule, licenses);
    assertEquals("Unexpected number of licenses found in the module " + childModule, 1, licenses.length);
    assertEquals("Unexpected license name", "The Apache Software License, Version 2.0", licenses[0].getName());
    assertEquals("Unexpected license URL", "http://www.apache.org/licenses/LICENSE-2.0.txt", licenses[0].getUrl());
}
Also used : DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) License(org.apache.ivy.core.module.descriptor.License) IvySettings(org.apache.ivy.core.settings.IvySettings) XmlModuleDescriptorParserTest(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest) Test(org.junit.Test)

Example 4 with License

use of org.apache.ivy.core.module.descriptor.License in project ant-ivy by apache.

the class PomModuleDescriptorParserTest method createIvySettingsForParentLicenseTesting.

private IvySettings createIvySettingsForParentLicenseTesting(final String parentPomFileName, final String parentOrgName, final String parentModuleName) throws Exception {
    final URL parentPomURL = this.getClass().getResource(parentPomFileName);
    assertNotNull("Could not find " + parentPomFileName, parentPomURL);
    final PomReader parentPomReader = new PomReader(parentPomURL, new URLResource(parentPomURL));
    final License[] parentLicenses = parentPomReader.getLicenses();
    assertNotNull("Missing licenses in parent pom " + parentPomFileName, parentLicenses);
    assertEquals("Unexpected number of licenses in parent pom " + parentPomFileName, 1, parentLicenses.length);
    final DependencyResolver dependencyResolver = new MockedDependencyResolver() {

        @Override
        protected ModuleDescriptor getModuleDescriptor(DependencyDescriptor dependencyDescriptor) {
            final String depOrg = dependencyDescriptor.getDependencyId().getOrganisation();
            final String depModuleName = dependencyDescriptor.getDependencyId().getName();
            if (depOrg.equals(parentOrgName) && depModuleName.equals(parentModuleName)) {
                final DefaultModuleDescriptor moduleDescriptor = DefaultModuleDescriptor.newDefaultInstance(dependencyDescriptor.getDependencyRevisionId());
                for (final License license : parentLicenses) {
                    moduleDescriptor.addLicense(license);
                }
                return moduleDescriptor;
            } else {
                return super.getModuleDescriptor(dependencyDescriptor);
            }
        }
    };
    final IvySettings ivySettings = new IvySettings();
    ivySettings.setDictatorResolver(dependencyResolver);
    return ivySettings;
}
Also used : URLResource(org.apache.ivy.plugins.repository.url.URLResource) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) License(org.apache.ivy.core.module.descriptor.License) IvySettings(org.apache.ivy.core.settings.IvySettings) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) URL(java.net.URL) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver)

Example 5 with License

use of org.apache.ivy.core.module.descriptor.License in project ant-ivy by apache.

the class PomModuleDescriptorParserTest method testLicense.

@Test
public void testLicense() throws Exception {
    ModuleDescriptor md = PomModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("spring-hibernate3-2.0.2.pom"), false);
    License[] licenses = md.getLicenses();
    assertNotNull(licenses);
    assertEquals(1, licenses.length);
    assertEquals("The Apache Software License, Version 2.0", licenses[0].getName());
    assertEquals("http://www.apache.org/licenses/LICENSE-2.0.txt", licenses[0].getUrl());
}
Also used : DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) License(org.apache.ivy.core.module.descriptor.License) XmlModuleDescriptorParserTest(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest) Test(org.junit.Test)

Aggregations

License (org.apache.ivy.core.module.descriptor.License)10 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)6 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)5 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)4 Test (org.junit.Test)4 Configuration (org.apache.ivy.core.module.descriptor.Configuration)3 IvySettings (org.apache.ivy.core.settings.IvySettings)3 XmlModuleDescriptorParserTest (org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)2 ExcludeRule (org.apache.ivy.core.module.descriptor.ExcludeRule)2 ExtraInfoHolder (org.apache.ivy.core.module.descriptor.ExtraInfoHolder)2 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)2 IOException (java.io.IOException)1 URL (java.net.URL)1 ParseException (java.text.ParseException)1 GregorianCalendar (java.util.GregorianCalendar)1 Map (java.util.Map)1 IvyContext (org.apache.ivy.core.IvyContext)1