Search in sources :

Example 1 with DefaultDependencyDescriptor

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

the class JarJarDependencyAnalyser method analyze.

public ModuleDescriptor[] analyze(JarModule[] modules) {
    StringBuilder jarjarCmd = new StringBuilder("java -jar \"").append(jarjarjarLocation.getAbsolutePath()).append("\" --find --level=jar ");
    Map<String, JarModule> jarModulesMap = new HashMap<>();
    Map<ModuleRevisionId, DefaultModuleDescriptor> mds = new HashMap<>();
    for (JarModule jarModule : modules) {
        jarModulesMap.put(jarModule.getJar().getAbsolutePath(), jarModule);
        DefaultModuleDescriptor md = DefaultModuleDescriptor.newBasicInstance(jarModule.getMrid(), new Date(jarModule.getJar().lastModified()));
        mds.put(jarModule.getMrid(), md);
        jarjarCmd.append("\"").append(jarModule.getJar().getAbsolutePath()).append("\"");
        jarjarCmd.append(File.pathSeparator);
    }
    if (modules.length > 0) {
        jarjarCmd.setLength(jarjarCmd.length() - 1);
    }
    Message.verbose("jarjar command: " + jarjarCmd);
    try {
        Process p = Runtime.getRuntime().exec(jarjarCmd.toString());
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = r.readLine()) != null) {
            String[] deps = line.split(" -> ");
            JarModule module = jarModulesMap.get(deps[0]);
            JarModule dependency = jarModulesMap.get(deps[1]);
            if (module.getMrid().getModuleId().equals(dependency.getMrid().getModuleId())) {
                continue;
            }
            Message.verbose(module.getMrid() + " depends on " + dependency.getMrid());
            DefaultModuleDescriptor md = mds.get(module.getMrid());
            DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, dependency.getMrid(), false, false, true);
            dd.addDependencyConfiguration(ModuleDescriptor.DEFAULT_CONFIGURATION, ModuleDescriptor.DEFAULT_CONFIGURATION);
            md.addDependency(dd);
        }
    } catch (IOException e) {
        Message.debug(e);
    }
    return mds.values().toArray(new ModuleDescriptor[mds.values().size()]);
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) IOException(java.io.IOException) Date(java.util.Date) BufferedReader(java.io.BufferedReader) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)

Example 2 with DefaultDependencyDescriptor

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

the class InstallEngine method install.

public ResolveReport install(ModuleRevisionId mrid, String from, String to, InstallOptions options) throws IOException {
    DependencyResolver fromResolver = settings.getResolver(from);
    DependencyResolver toResolver = settings.getResolver(to);
    if (fromResolver == null) {
        throw new IllegalArgumentException("unknown resolver " + from + ". Available resolvers are: " + settings.getResolverNames());
    }
    if (toResolver == null) {
        throw new IllegalArgumentException("unknown resolver " + to + ". Available resolvers are: " + settings.getResolverNames());
    }
    PatternMatcher matcher = settings.getMatcher(options.getMatcherName());
    if (matcher == null) {
        throw new IllegalArgumentException("unknown matcher " + options.getMatcherName() + ". Available matchers are: " + settings.getMatcherNames());
    }
    // build module file declaring the dependency
    Message.info(":: installing " + mrid + " ::");
    DependencyResolver oldDictator = resolveEngine.getDictatorResolver();
    boolean log = settings.logNotConvertedExclusionRule();
    try {
        settings.setLogNotConvertedExclusionRule(true);
        resolveEngine.setDictatorResolver(fromResolver);
        DefaultModuleDescriptor md = new DefaultModuleDescriptor(ModuleRevisionId.newInstance("apache", "ivy-install", "1.0"), settings.getStatusManager().getDefaultStatus(), new Date());
        String resolveId = ResolveOptions.getDefaultResolveId(md);
        md.addConfiguration(new Configuration("default"));
        md.addConflictManager(new ModuleId(ExactPatternMatcher.ANY_EXPRESSION, ExactPatternMatcher.ANY_EXPRESSION), ExactPatternMatcher.INSTANCE, new NoConflictManager());
        for (String dc : options.getConfs()) {
            final String depConf = dc.trim();
            if (MatcherHelper.isExact(matcher, mrid)) {
                DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, mrid, false, false, options.isTransitive());
                dd.addDependencyConfiguration("default", depConf);
                md.addDependency(dd);
            } else {
                for (ModuleRevisionId imrid : searchEngine.listModules(fromResolver, mrid, matcher)) {
                    Message.info("\tfound " + imrid + " to install: adding to the list");
                    DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, imrid, false, false, options.isTransitive());
                    dd.addDependencyConfiguration("default", depConf);
                    md.addDependency(dd);
                }
            }
        }
        // resolve using appropriate resolver
        ResolveReport report = new ResolveReport(md, resolveId);
        Message.info(":: resolving dependencies ::");
        ResolveOptions resolveOptions = new ResolveOptions().setResolveId(resolveId).setConfs(new String[] { "default" }).setValidate(options.isValidate());
        IvyNode[] dependencies = resolveEngine.getDependencies(md, resolveOptions, report);
        report.setDependencies(Arrays.asList(dependencies), options.getArtifactFilter());
        Message.info(":: downloading artifacts to cache ::");
        resolveEngine.downloadArtifacts(report, options.getArtifactFilter(), new DownloadOptions());
        // now that everything is in cache, we can publish all these modules
        Message.info(":: installing in " + to + " ::");
        for (IvyNode dependency : dependencies) {
            ModuleDescriptor depmd = dependency.getDescriptor();
            if (depmd != null) {
                ModuleRevisionId depMrid = depmd.getModuleRevisionId();
                Message.verbose("installing " + depMrid);
                boolean successfullyPublished = false;
                try {
                    toResolver.beginPublishTransaction(depMrid, options.isOverwrite());
                    // publish artifacts
                    for (ArtifactDownloadReport artifact : report.getArtifactsReports(depMrid)) {
                        if (artifact.getLocalFile() != null) {
                            toResolver.publish(artifact.getArtifact(), artifact.getLocalFile(), options.isOverwrite());
                        }
                    }
                    // publish metadata
                    MetadataArtifactDownloadReport artifactDownloadReport = dependency.getModuleRevision().getReport();
                    File localIvyFile = artifactDownloadReport.getLocalFile();
                    toResolver.publish(depmd.getMetadataArtifact(), localIvyFile, options.isOverwrite());
                    if (options.isInstallOriginalMetadata()) {
                        if (artifactDownloadReport.getArtifactOrigin() != null && artifactDownloadReport.getArtifactOrigin().isExists() && !ArtifactOrigin.isUnknown(artifactDownloadReport.getArtifactOrigin()) && artifactDownloadReport.getArtifactOrigin().getArtifact() != null && artifactDownloadReport.getArtifactOrigin().getArtifact().getType().endsWith(".original") && !artifactDownloadReport.getArtifactOrigin().getArtifact().getType().equals(depmd.getMetadataArtifact().getType() + ".original")) {
                            // publish original metadata artifact, too, as it has a different
                            // type
                            toResolver.publish(artifactDownloadReport.getArtifactOrigin().getArtifact(), artifactDownloadReport.getOriginalLocalFile(), options.isOverwrite());
                        }
                    }
                    // end module publish
                    toResolver.commitPublishTransaction();
                    successfullyPublished = true;
                } finally {
                    if (!successfullyPublished) {
                        toResolver.abortPublishTransaction();
                    }
                }
            }
        }
        Message.info(":: install resolution report ::");
        // output report
        resolveEngine.outputReport(report, settings.getResolutionCacheManager(), resolveOptions);
        return report;
    } finally {
        // IVY-834: log the problems if there were any...
        Message.sumupProblems();
        resolveEngine.setDictatorResolver(oldDictator);
        settings.setLogNotConvertedExclusionRule(log);
    }
}
Also used : MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) Configuration(org.apache.ivy.core.module.descriptor.Configuration) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) Date(java.util.Date) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver) ModuleId(org.apache.ivy.core.module.id.ModuleId) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) NoConflictManager(org.apache.ivy.plugins.conflict.NoConflictManager) ResolveReport(org.apache.ivy.core.report.ResolveReport) DownloadOptions(org.apache.ivy.core.resolve.DownloadOptions) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ExactPatternMatcher(org.apache.ivy.plugins.matcher.ExactPatternMatcher) PatternMatcher(org.apache.ivy.plugins.matcher.PatternMatcher) ResolveOptions(org.apache.ivy.core.resolve.ResolveOptions) File(java.io.File) IvyNode(org.apache.ivy.core.resolve.IvyNode)

Example 3 with DefaultDependencyDescriptor

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

the class Main method run.

@SuppressWarnings("deprecation")
private static ResolveReport run(CommandLine line, boolean isCli) throws Exception {
    if (line.hasOption("version")) {
        System.out.println("Apache Ivy " + Ivy.getIvyVersion() + " - " + Ivy.getIvyDate() + " :: " + Ivy.getIvyHomeURL());
        return null;
    }
    boolean validate = !line.hasOption("novalidate");
    Ivy ivy = Ivy.newInstance();
    initMessage(line, ivy);
    IvySettings settings = initSettings(line, ivy);
    ivy.pushContext();
    File cache = new File(settings.substitute(line.getOptionValue("cache", settings.getDefaultCache().getAbsolutePath())));
    if (line.hasOption("cache")) {
        // override default cache path with user supplied cache path
        settings.setDefaultCache(cache);
    }
    if (!cache.exists()) {
        cache.mkdirs();
    } else if (!cache.isDirectory()) {
        error(cache + " is not a directory");
    }
    String[] confs;
    if (line.hasOption("confs")) {
        confs = line.getOptionValues("confs");
    } else {
        confs = new String[] { "*" };
    }
    File ivyfile;
    if (line.hasOption("dependency")) {
        String[] dep = line.getOptionValues("dependency");
        ivyfile = File.createTempFile("ivy", ".xml");
        ivyfile.deleteOnExit();
        DefaultModuleDescriptor md = DefaultModuleDescriptor.newDefaultInstance(ModuleRevisionId.newInstance(dep[0], dep[1] + "-caller", "working"));
        DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, ModuleRevisionId.newInstance(dep[0], dep[1], dep[2]), false, false, true);
        for (String conf : confs) {
            dd.addDependencyConfiguration("default", conf);
        }
        md.addDependency(dd);
        XmlModuleDescriptorWriter.write(md, ivyfile);
        confs = new String[] { "default" };
    } else {
        ivyfile = new File(settings.substitute(line.getOptionValue("ivy", "ivy.xml")));
        if (!ivyfile.exists()) {
            error("ivy file not found: " + ivyfile);
        } else if (ivyfile.isDirectory()) {
            error("ivy file is not a file: " + ivyfile);
        }
    }
    if (line.hasOption("useOrigin")) {
        ivy.getSettings().useDeprecatedUseOrigin();
    }
    ResolveOptions resolveOptions = new ResolveOptions().setConfs(confs).setValidate(validate).setResolveMode(line.getOptionValue("mode")).setArtifactFilter(FilterHelper.getArtifactTypeFilter(line.getOptionValues("types")));
    if (line.hasOption("notransitive")) {
        resolveOptions.setTransitive(false);
    }
    if (line.hasOption("refresh")) {
        resolveOptions.setRefresh(true);
    }
    ResolveReport report = ivy.resolve(ivyfile.toURI().toURL(), resolveOptions);
    if (report.hasError()) {
        if (isCli) {
            System.exit(1);
        }
        StringBuilder sb = new StringBuilder();
        for (String problem : report.getAllProblemMessages()) {
            if (sb.length() > 0) {
                sb.append("\n");
            }
            sb.append(problem);
        }
        throw new ResolveProcessException(sb.toString());
    }
    ModuleDescriptor md = report.getModuleDescriptor();
    if (confs.length == 1 && "*".equals(confs[0])) {
        confs = md.getConfigurationsNames();
    }
    if (line.hasOption("retrieve")) {
        String retrievePattern = settings.substitute(line.getOptionValue("retrieve"));
        if (!retrievePattern.contains("[")) {
            retrievePattern += "/lib/[conf]/[artifact].[ext]";
        }
        String ivyPattern = settings.substitute(line.getOptionValue("ivypattern"));
        ivy.retrieve(md.getModuleRevisionId(), new RetrieveOptions().setConfs(confs).setSync(line.hasOption("sync")).setUseOrigin(line.hasOption("useOrigin")).setDestArtifactPattern(retrievePattern).setDestIvyPattern(ivyPattern).setOverwriteMode(line.getOptionValue("overwriteMode")).setArtifactFilter(FilterHelper.getArtifactTypeFilter(line.getOptionValues("types"))).setMakeSymlinks(line.hasOption("symlink")).setMakeSymlinksInMass(line.hasOption("symlinkmass")));
    }
    if (line.hasOption("cachepath")) {
        outputCachePath(ivy, cache, md, confs, line.getOptionValue("cachepath", "ivycachepath.txt"));
    }
    if (line.hasOption("revision")) {
        ivy.deliver(md.getResolvedModuleRevisionId(), settings.substitute(line.getOptionValue("revision")), settings.substitute(line.getOptionValue("deliverto", "ivy-[revision].xml")), DeliverOptions.newInstance(settings).setStatus(settings.substitute(line.getOptionValue("status", "release"))).setValidate(validate));
        if (line.hasOption("publish")) {
            ivy.publish(md.getResolvedModuleRevisionId(), Collections.singleton(settings.substitute(line.getOptionValue("publishpattern", "distrib/[type]s/[artifact]-[revision].[ext]"))), line.getOptionValue("publish"), new PublishOptions().setPubrevision(settings.substitute(line.getOptionValue("revision"))).setValidate(validate).setSrcIvyPattern(settings.substitute(line.getOptionValue("deliverto", "ivy-[revision].xml"))).setOverwrite(line.hasOption("overwrite")));
        }
    }
    if (line.hasOption("makepom")) {
        final String pomFilePath = line.getOptionValue("makepom", "pom.xml");
        final File pomFile = new File(pomFilePath);
        PomModuleDescriptorWriter.write(md, pomFile, new PomWriterOptions());
        Message.debug("Generated a pom file for module at " + pomFile);
    }
    if (line.hasOption("main")) {
        // check if the option cp has been set
        List<File> fileList = getExtraClasspathFileList(line);
        // merge -args and left over args
        String[] fargs = line.getOptionValues("args");
        if (fargs == null) {
            fargs = new String[0];
        }
        String[] extra = line.getLeftOverArgs();
        if (extra == null) {
            extra = new String[0];
        }
        String[] params = new String[fargs.length + extra.length];
        System.arraycopy(fargs, 0, params, 0, fargs.length);
        System.arraycopy(extra, 0, params, fargs.length, extra.length);
        // invoke with given main class and merged params
        invoke(ivy, cache, md, confs, fileList, line.getOptionValue("main"), params);
    }
    ivy.getLoggerEngine().popLogger();
    ivy.popContext();
    return report;
}
Also used : IvySettings(org.apache.ivy.core.settings.IvySettings) RetrieveOptions(org.apache.ivy.core.retrieve.RetrieveOptions) PublishOptions(org.apache.ivy.core.publish.PublishOptions) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ResolveReport(org.apache.ivy.core.report.ResolveReport) ResolveProcessException(org.apache.ivy.core.resolve.ResolveProcessException) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) ResolveOptions(org.apache.ivy.core.resolve.ResolveOptions) PomWriterOptions(org.apache.ivy.plugins.parser.m2.PomWriterOptions)

Example 4 with DefaultDependencyDescriptor

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

the class PomModuleDescriptorBuilder method addDependency.

public void addDependency(Resource res, PomDependencyData dep) {
    String scope = dep.getScope();
    if (!isNullOrEmpty(scope) && !MAVEN2_CONF_MAPPING.containsKey(scope)) {
        // unknown scope, defaulting to 'compile'
        scope = "compile";
    }
    String version = dep.getVersion();
    if (isNullOrEmpty(version)) {
        version = getDefaultVersion(dep);
    }
    ModuleRevisionId moduleRevId = ModuleRevisionId.newInstance(dep.getGroupId(), dep.getArtifactId(), version);
    // Some POMs depend on themselves; Ivy doesn't allow this. Don't add this dependency!
    // Example: https://repo1.maven.org/maven2/net/jini/jsk-platform/2.1/jsk-platform-2.1.pom
    ModuleRevisionId mRevId = ivyModuleDescriptor.getModuleRevisionId();
    if (mRevId != null && mRevId.getModuleId().equals(moduleRevId.getModuleId())) {
        return;
    }
    // experimentation shows the following, excluded modules are
    // inherited from parent POMs if either of the following is true:
    // the <exclusions> element is missing or the <exclusions> element
    // is present, but empty.
    List<ModuleId> excluded = dep.getExcludedModules();
    if (excluded.isEmpty()) {
        excluded = getDependencyMgtExclusions(ivyModuleDescriptor, dep.getGroupId(), dep.getArtifactId());
    }
    final boolean excludeAllTransitiveDeps = shouldExcludeAllTransitiveDeps(excluded);
    // the same dependency mrid could appear twice in the module descriptor,
    // so we check if we already have created a dependency descriptor for the dependency mrid
    final DependencyDescriptor existing = this.ivyModuleDescriptor.depDescriptors.get(moduleRevId);
    final DefaultDependencyDescriptor dd = (existing != null && existing instanceof DefaultDependencyDescriptor) ? (DefaultDependencyDescriptor) existing : new PomDependencyDescriptor(dep, ivyModuleDescriptor, moduleRevId, !excludeAllTransitiveDeps);
    if (isNullOrEmpty(scope)) {
        scope = getDefaultScope(dep);
    }
    ConfMapper mapping = MAVEN2_CONF_MAPPING.get(scope);
    mapping.addMappingConfs(dd, dep.isOptional());
    Map<String, String> extraAtt = new HashMap<>();
    if (dep.getClassifier() != null || dep.getType() != null && !"jar".equals(dep.getType())) {
        String type = "jar";
        if (dep.getType() != null) {
            type = dep.getType();
        }
        String ext = type;
        // Cfr. http://maven.apache.org/guides/mini/guide-attached-tests.html
        if ("test-jar".equals(type)) {
            ext = "jar";
            extraAtt.put("m:classifier", "tests");
        } else if (JAR_PACKAGINGS.contains(type)) {
            ext = "jar";
        }
        // dependency to assume such an artifact is published
        if (dep.getClassifier() != null) {
            extraAtt.put("m:classifier", dep.getClassifier());
        }
        DefaultDependencyArtifactDescriptor depArtifact = new DefaultDependencyArtifactDescriptor(dd, dd.getDependencyId().getName(), type, ext, null, extraAtt);
        // here we have to assume a type and ext for the artifact, so this is a limitation
        // compared to how m2 behave with classifiers
        String optionalizedScope = dep.isOptional() ? "optional" : scope;
        dd.addDependencyArtifact(optionalizedScope, depArtifact);
    }
    for (ModuleId excludedModule : excluded) {
        // in account while defining the DefaultDependencyDescriptor itself
        if ("*".equals(excludedModule.getOrganisation()) && "*".equals(excludedModule.getName())) {
            continue;
        }
        for (String conf : dd.getModuleConfigurations()) {
            dd.addExcludeRule(conf, new DefaultExcludeRule(new ArtifactId(excludedModule, PatternMatcher.ANY_EXPRESSION, PatternMatcher.ANY_EXPRESSION, PatternMatcher.ANY_EXPRESSION), ExactPatternMatcher.INSTANCE, null));
        }
    }
    // intentional identity check to make sure we don't re-add the same dependency
    if (existing != dd) {
        ivyModuleDescriptor.addDependency(dd);
    }
}
Also used : ArtifactId(org.apache.ivy.core.module.id.ArtifactId) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) DefaultDependencyArtifactDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyArtifactDescriptor) ModuleId(org.apache.ivy.core.module.id.ModuleId) DefaultExcludeRule(org.apache.ivy.core.module.descriptor.DefaultExcludeRule) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)

Example 5 with DefaultDependencyDescriptor

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

DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)80 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)61 Test (org.junit.Test)60 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)47 GregorianCalendar (java.util.GregorianCalendar)25 Date (java.util.Date)23 Artifact (org.apache.ivy.core.module.descriptor.Artifact)23 File (java.io.File)22 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)19 DownloadReport (org.apache.ivy.core.report.DownloadReport)18 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)16 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)12 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)9 DownloadOptions (org.apache.ivy.core.resolve.DownloadOptions)9 LatestRevisionStrategy (org.apache.ivy.plugins.latest.LatestRevisionStrategy)7 Configuration (org.apache.ivy.core.module.descriptor.Configuration)6 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)5 ModuleId (org.apache.ivy.core.module.id.ModuleId)5 BufferedReader (java.io.BufferedReader)3 Locale (java.util.Locale)3