org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter Java Examples

The following examples show how to use org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RepositoryAnalyser.java    From ant-ivy with Apache License 2.0 6 votes vote down vote up
public void analyse(String pattern, DependencyAnalyser depAnalyser) {
    JarModuleFinder finder = new JarModuleFinder(pattern);
    ModuleDescriptor[] mds = depAnalyser.analyze(finder.findJarModules());
    Message.info("found " + mds.length + " modules");
    for (ModuleDescriptor md : mds) {
        File ivyFile = new File(IvyPatternHelper.substitute(
                pattern,
                DefaultArtifact.newIvyArtifact(md.getModuleRevisionId(),
                        md.getPublicationDate())));
        try {
            Message.info("generating " + ivyFile);
            XmlModuleDescriptorWriter.write(md, ivyFile);
        } catch (IOException e) {
            Message.debug(e);
        }
    }
}
 
Example #2
Source File: FixDepsTask.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
@Override
public void doExecute() throws BuildException {
    prepareAndCheck();

    if (dest == null) {
        throw new BuildException("Missing required parameter 'tofile'");
    }
    if (dest.exists() && dest.isDirectory()) {
        throw new BuildException("The destination file '" + dest.getAbsolutePath()
                + "' already exist and is a folder");
    }

    ResolveReport report = getResolvedReport();

    List<ModuleId> midToKeep = new ArrayList<>();
    for (Keep keep : keeps) {
        midToKeep.add(ModuleId.newInstance(keep.org, keep.module));
    }

    ModuleDescriptor md = report.toFixedModuleDescriptor(getSettings(), midToKeep);
    try {
        XmlModuleDescriptorWriter.write(md, dest);
    } catch (IOException e) {
        throw new BuildException("Failed to write into the file " + dest.getAbsolutePath()
                + " (" + e.getMessage() + ")", e);
    }
}
 
Example #3
Source File: OSGiManifestParser.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
public void toIvyFile(InputStream is, Resource res, File destFile, ModuleDescriptor md)
        throws ParseException, IOException {
    try {
        XmlModuleDescriptorWriter.write(md, destFile);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
 
Example #4
Source File: DefaultModuleDescriptor.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
public void toIvyFile(File destFile) throws ParseException, IOException {
    if (parser != null && resource != null) {
        parser.toIvyFile(resource.openStream(), resource, destFile, this);
    } else {
        XmlModuleDescriptorWriter.write(this, destFile);
    }
}
 
Example #5
Source File: PomModuleDescriptorParser.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
public void toIvyFile(InputStream is, Resource res, File destFile, ModuleDescriptor md)
        throws ParseException, IOException {
    try {
        XmlModuleDescriptorWriter.write(md, destFile);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
 
Example #6
Source File: BasicResolver.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
private void cacheModuleDescriptor(ModuleDescriptor systemMd, ModuleRevisionId systemMrid,
        ResolvedResource ivyRef, ResolvedModuleRevision rmr) {
    RepositoryCacheManager cacheManager = getRepositoryCacheManager();

    final ModuleDescriptorParser parser = systemMd.getParser();

    // the metadata artifact which was used to cache the original metadata file
    Artifact requestedMetadataArtifact = (ivyRef == null) ? systemMd.getMetadataArtifact()
            : parser.getMetadataArtifact(
                    ModuleRevisionId.newInstance(systemMrid, systemMd.getRevision()),
            ivyRef.getResource());

    cacheManager.originalToCachedModuleDescriptor(this, ivyRef, requestedMetadataArtifact, rmr,
        new ModuleDescriptorWriter() {
            public void write(ResolvedResource originalMdResource, ModuleDescriptor md,
                    File src, File dest) throws IOException, ParseException {
                if (originalMdResource == null) {
                    // a basic ivy file is written containing default data
                    XmlModuleDescriptorWriter.write(md, dest);
                } else {
                    // copy and update ivy file from source to cache
                    parser.toIvyFile(new FileInputStream(src),
                        originalMdResource.getResource(), dest, md);
                    long repLastModified = originalMdResource.getLastModified();
                    if (repLastModified > 0) {
                        dest.setLastModified(repLastModified);
                    }
                }
            }
        });
}
 
Example #7
Source File: TestHelper.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
/**
 * Fills a repository with a set of module, using empty files for published artifacts.
 *
 * @param resolver
 *            the resolver to use to publish the modules
 * @param mds
 *            the descriptors of the modules to put in the repository
 * @throws IOException
 *             if an IO problem occurs while filling the repository
 */
public static void fillRepository(DependencyResolver resolver, Collection<ModuleDescriptor> mds)
        throws IOException {
    File tmp = File.createTempFile("ivy", "tmp");
    try {
        for (ModuleDescriptor md : mds) {
            boolean overwrite = false;
            resolver.beginPublishTransaction(md.getModuleRevisionId(), overwrite);
            boolean published = false;
            try {
                XmlModuleDescriptorWriter.write(md, tmp);
                resolver.publish(md.getMetadataArtifact(), tmp, overwrite);
                tmp.delete();
                tmp.createNewFile();
                for (Artifact artifact : md.getAllArtifacts()) {
                    resolver.publish(artifact, tmp, overwrite);
                }
                resolver.commitPublishTransaction();
                published = true;
            } finally {
                if (!published) {
                    resolver.abortPublishTransaction();
                }
            }
        }
    } finally {
        tmp.delete();
    }
}
 
Example #8
Source File: TestClassWithDependencyRunner.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
private static List<URL> resolveArtifacts(List<String> dependencies) throws Exception {
    //creates clear ivy settings
    IvySettings ivySettings = new IvySettings();
    //url resolver for configuration of maven repo
    URLResolver resolver = new URLResolver();
    resolver.setM2compatible(true);
    resolver.setName("central");
    //you can specify the url resolution pattern strategy
    resolver.addArtifactPattern(
        "https://repo1.maven.org/maven2/"
            + "[organisation]/[module]/[revision]/[artifact](-[revision]).[ext]");
    //adding maven repo resolver
    ivySettings.addResolver(resolver);
    //set to the default resolver
    ivySettings.setDefaultResolver(resolver.getName());
    //creates an Ivy instance with settings
    Ivy ivy = Ivy.newInstance(ivySettings);

    File ivyfile = File.createTempFile("ivy", ".xml");
    ivyfile.deleteOnExit();

    DefaultModuleDescriptor md =
        DefaultModuleDescriptor.newDefaultInstance(ModuleRevisionId.newInstance("foo", "foo" + "-caller", "working"));
    for (String dependency : dependencies) {
        String[] split = dependency.split(":");

        DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md,
            ModuleRevisionId.newInstance(split[0], split[1], split[2]), false, false, true);
        md.addDependency(dd);
    }

    //creates an ivy configuration file
    XmlModuleDescriptorWriter.write(md, ivyfile);

    String[] confs = new String[]{"default"};
    ResolveOptions resolveOptions = new ResolveOptions().setConfs(confs);

    //init resolve report
    ResolveReport report = ivy.resolve(ivyfile.toURL(), resolveOptions);

    List<URL> resolvedDependencies = new ArrayList<>();
    ArtifactDownloadReport[] allArtifactsReports = report.getAllArtifactsReports();
    for (ArtifactDownloadReport allArtifactsReport : allArtifactsReports) {
        resolvedDependencies.add(allArtifactsReport.getLocalFile().toURI().toURL());
    }
    assertThat(resolvedDependencies).hasSizeGreaterThanOrEqualTo(dependencies.size());
    return resolvedDependencies;
}
 
Example #9
Source File: PublishEngineTest.java    From ant-ivy with Apache License 2.0 4 votes vote down vote up
@Test
public void testAtomicity() throws Exception {
    IvySettings settings = new IvySettings();
    final PublishEngine engine = new PublishEngine(settings, new EventManager());
    final int[] counter = new int[] {0};

    final DefaultModuleDescriptor md = DefaultModuleDescriptor
            .newDefaultInstance(ModuleRevisionId.parse("#A;1.0"));
    final FileSystemResolver resolver = new FileSystemResolver() {
        public void publish(Artifact artifact, File src, boolean overwrite) throws IOException {
            super.publish(artifact, src, overwrite);
            synchronized (PublishEngineTest.this) {
                counter[0]++;
            }
            sleepSilently(50);
            synchronized (PublishEngineTest.this) {
                counter[0]++;
            }
        }
    };
    resolver.setName("test");
    resolver.setSettings(settings);
    String publishRepoDir = new File("build/test/publish/repo").getAbsolutePath();
    resolver.addIvyPattern(publishRepoDir + "/[module]/[revision]/[artifact].[ext]");
    resolver.addArtifactPattern(publishRepoDir + "/[module]/[revision]/[artifact].[ext]");

    FileUtil.copy(new File("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), new File(
            "build/test/publish/module/A.jar"), null);
    XmlModuleDescriptorWriter.write(md, new File("build/test/publish/module/ivy.xml"));

    resolveAndAssertNotFound(settings, resolver, "#A;latest.integration", "before publishing");

    // run publish asynchronously
    new Thread() {
        public void run() {
            try {
                engine.publish(md, Collections.singletonList("build/test/publish/module/[artifact].[ext]"),
                    resolver, new PublishOptions().setSrcIvyPattern("build/test/publish/module/[artifact].[ext]"));
                synchronized (PublishEngineTest.this) {
                    counter[0]++;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }.start();

    while (true) {
        sleepSilently(5);
        synchronized (this) {
            if (counter[0] == 5) {
                break;
            } else if (counter[0] < 4) {
                resolveAndAssertNotFound(settings, resolver, "#A;latest.integration", "after "
                        + (counter[0] / 2) + " artifacts published");
            }
        }
    }
    resolveAndAssertFound(settings, resolver, "#A;1.0");
}
 
Example #10
Source File: TestPerformance.java    From ant-ivy with Apache License 2.0 4 votes vote down vote up
private void generateModules(int nbModules, int minDependencies, int maxDependencies,
        int minVersions, int maxVersions) throws IOException {
    int nb = 0;
    int curDep = 1;
    int varDeps = maxDependencies - minDependencies;
    int varVersions = maxVersions - minVersions;
    Random r = new Random(System.currentTimeMillis());

    while (nb < nbModules) {
        int deps = minDependencies + r.nextInt(varDeps + 1);
        int versions = minVersions + r.nextInt(varVersions + 1);

        int prevCurDep = curDep;
        for (int ver = 0; ver < versions; ver++) {
            DefaultModuleDescriptor md = new DefaultModuleDescriptor(
                    ModuleRevisionId.newInstance("apache", "mod" + nb, "1." + ver),
                    "integration", new Date());

            curDep = prevCurDep;
            for (int i = 0; i < deps && curDep < nbModules; i++) {
                int d;
                if (i % 2 == 1) {
                    d = nb + i;
                    if (d >= prevCurDep) {
                        d = curDep;
                        curDep++;
                    }
                } else {
                    d = curDep;
                    curDep++;
                }
                DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(
                        md,
                        ModuleRevisionId.newInstance("apache", "mod" + d, "latest.integration"),
                        false, false, true);
                dd.addDependencyConfiguration("default", "default");
                md.addDependency(dd);
            }
            XmlModuleDescriptorWriter.write(md, new File("build/test/perf/mod" + nb + "/ivy-1."
                    + ver + ".xml"));
            FileUtil.copy(new File("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"),
                new File("build/test/perf/mod" + nb + "/mod" + nb + "-1." + ver + ".jar"), null);
        }
        nb++;
    }
}