Search in sources :

Example 36 with Artifact

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

the class PublishEngine method publish.

/**
 * Publishes a module to the repository. The publish can update the ivy file to publish if
 * update is set to true. In this case it will use the given pubrevision, pubdate and status. If
 * pubdate is null it will default to the current date. If status is null it will default to the
 * current ivy file status (which itself defaults to integration if none is found). If update is
 * false, then if the revision is not the same in the ivy file than the one expected (given as
 * parameter), this method will fail with an IllegalArgumentException. pubdate and status are
 * not used if update is false. extra artifacts can be used to publish more artifacts than
 * actually declared in the ivy file. This can be useful to publish additional metadata or
 * reports. The extra artifacts array can be null (= no extra artifacts), and if non null only
 * the name, type, ext url and extra attributes of the artifacts are really used. Other methods
 * can return null safely.
 *
 * @param mrid ModuleRevisionId
 * @param srcArtifactPattern a Collection of String
 * @param resolverName String
 * @param options PublishOptions
 * @return Collection<Artifact>
 * @throws IOException if something goes wrong
 */
public Collection<Artifact> publish(ModuleRevisionId mrid, Collection<String> srcArtifactPattern, String resolverName, PublishOptions options) throws IOException {
    Message.info(":: publishing :: " + mrid.getModuleId());
    Message.verbose("\tvalidate = " + options.isValidate());
    long start = System.currentTimeMillis();
    options.setSrcIvyPattern(settings.substitute(options.getSrcIvyPattern()));
    if (options.getPubBranch() == null) {
        options.setPubbranch(mrid.getBranch());
    }
    if (options.getPubrevision() == null) {
        options.setPubrevision(mrid.getRevision());
    }
    ModuleRevisionId pubmrid = ModuleRevisionId.newInstance(mrid, options.getPubBranch(), options.getPubrevision());
    // let's find the resolved module descriptor
    ModuleDescriptor md = null;
    if (options.getSrcIvyPattern() != null) {
        File ivyFile = settings.resolveFile(IvyPatternHelper.substitute(options.getSrcIvyPattern(), DefaultArtifact.newIvyArtifact(pubmrid, new Date())));
        if (!ivyFile.exists()) {
            throw new IllegalArgumentException("ivy file to publish not found for " + mrid + ": call deliver before (" + ivyFile + ")");
        }
        URL ivyFileURL = ivyFile.toURI().toURL();
        try {
            md = XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, ivyFileURL, false);
            if (options.isUpdate()) {
                File tmp = File.createTempFile("ivy", ".xml");
                tmp.deleteOnExit();
                String[] confs = replaceWildcards(options.getConfs(), md);
                Set<String> confsToRemove = new HashSet<>(Arrays.asList(md.getConfigurationsNames()));
                confsToRemove.removeAll(Arrays.asList(confs));
                try {
                    XmlModuleDescriptorUpdater.update(ivyFileURL, tmp, new UpdateOptions().setSettings(settings).setStatus(options.getStatus() == null ? md.getStatus() : options.getStatus()).setRevision(options.getPubrevision()).setBranch(options.getPubBranch()).setPubdate(options.getPubdate() == null ? new Date() : options.getPubdate()).setMerge(options.isMerge()).setMergedDescriptor(md).setConfsToExclude(confsToRemove.toArray(new String[confsToRemove.size()])));
                    ivyFile = tmp;
                    // we parse the new file to get updated module descriptor
                    md = XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, ivyFile.toURI().toURL(), false);
                    options.setSrcIvyPattern(ivyFile.getAbsolutePath());
                } catch (SAXException e) {
                    throw new IllegalStateException("bad ivy file for " + mrid + ": " + ivyFile + ": " + e);
                }
            } else if (!options.getPubrevision().equals(md.getModuleRevisionId().getRevision())) {
                throw new IllegalArgumentException("cannot publish " + ivyFile + " as " + options.getPubrevision() + ": bad revision found in ivy file (Revision: " + md.getModuleRevisionId().getRevision() + "). Use forcedeliver or update.");
            }
        } catch (ParseException e) {
            throw new IllegalStateException("bad ivy file for " + mrid + ": " + ivyFile + ": " + e);
        }
    } else {
        ResolutionCacheManager cacheManager = settings.getResolutionCacheManager();
        try {
            md = cacheManager.getResolvedModuleDescriptor(mrid);
        } catch (ParseException e) {
            throw new IllegalStateException("bad ivy file in cache for " + mrid + ": " + e);
        }
        md.setResolvedModuleRevisionId(pubmrid);
    }
    DependencyResolver resolver = settings.getResolver(resolverName);
    if (resolver == null) {
        throw new IllegalArgumentException("unknown resolver " + resolverName);
    }
    // collect all declared artifacts of this module
    Collection<Artifact> missing = publish(md, srcArtifactPattern, resolver, options);
    Message.verbose("\tpublish done (" + (System.currentTimeMillis() - start) + "ms)");
    return missing;
}
Also used : ResolutionCacheManager(org.apache.ivy.core.cache.ResolutionCacheManager) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Date(java.util.Date) URL(java.net.URL) UpdateOptions(org.apache.ivy.plugins.parser.xml.UpdateOptions) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) MDArtifact(org.apache.ivy.core.module.descriptor.MDArtifact) SAXException(org.xml.sax.SAXException) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ParseException(java.text.ParseException) File(java.io.File) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 37 with Artifact

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

the class ResolveTest method assertContainsArtifact.

// //////////////////////////////////////////////////////////
// helper methods to ease the tests
// //////////////////////////////////////////////////////////
private void assertContainsArtifact(String org, String module, String rev, String artName, String type, String ext, ConfigurationResolveReport conf) {
    Artifact art = getArtifact(org, module, rev, artName, type, ext);
    assertContainsArtifact(conf, art);
}
Also used : Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact)

Example 38 with Artifact

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

the class ResolveTest method testArtifactOrigin.

@Test
public void testArtifactOrigin() throws Exception {
    ResolveReport report = ivy.resolve(new File("test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml"), getResolveOptions(new String[] { "default" }));
    assertNotNull(report);
    ArtifactDownloadReport[] dReports = report.getConfigurationReport("default").getDownloadReports(ModuleRevisionId.newInstance("org1", "mod1.2", "2.0"));
    assertNotNull(dReports);
    assertEquals("number of downloaded artifacts not correct", 1, dReports.length);
    Artifact artifact = dReports[0].getArtifact();
    assertNotNull(artifact);
    String expectedLocation = new File("test/repositories/1/org1/mod1.2/jars/mod1.2-2.0.jar").getAbsolutePath();
    // verify the origin in the report
    ArtifactOrigin reportOrigin = dReports[0].getArtifactOrigin();
    assertNotNull(reportOrigin);
    assertTrue("isLocal for artifact not correct", reportOrigin.isLocal());
    assertEquals("location for artifact not correct", expectedLocation, reportOrigin.getLocation());
    // verify the saved origin on disk
    ArtifactOrigin ivyOrigin = getSavedArtifactOrigin(artifact);
    assertNotNull(ivyOrigin);
    assertTrue("isLocal for artifact not correct", ivyOrigin.isLocal());
    assertEquals("location for artifact not correct", expectedLocation, ivyOrigin.getLocation());
    // now resolve the same artifact again and verify the origin of the (not-downloaded)
    // artifact
    report = ivy.resolve(new File("test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml"), getResolveOptions(new String[] { "default" }));
    assertNotNull(report);
    dReports = report.getConfigurationReport("default").getDownloadReports(ModuleRevisionId.newInstance("org1", "mod1.2", "2.0"));
    assertNotNull(dReports);
    assertEquals("number of downloaded artifacts not correct", 1, dReports.length);
    assertEquals("download status not correct", DownloadStatus.NO, dReports[0].getDownloadStatus());
    reportOrigin = dReports[0].getArtifactOrigin();
    assertNotNull(reportOrigin);
    assertTrue("isLocal for artifact not correct", reportOrigin.isLocal());
    assertEquals("location for artifact not correct", expectedLocation, reportOrigin.getLocation());
}
Also used : ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) ArtifactOrigin(org.apache.ivy.core.cache.ArtifactOrigin) JarFile(java.util.jar.JarFile) File(java.io.File) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Test(org.junit.Test)

Example 39 with Artifact

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

the class ResolveTest method testIVY1366.

/**
 * Test case for IVY-1366.
 *
 * @throws Exception if something goes wrong
 * @see <a href="https://issues.apache.org/jira/browse/IVY-1366">IVY-1366</a>
 */
@Test
public void testIVY1366() throws Exception {
    Ivy ivy = new Ivy();
    ivy.configure(new File("test/repositories/IVY-1366/ivysettings.xml"));
    ResolveReport report = ivy.resolve(new File("test/repositories/IVY-1366/ivy.xml"), new ResolveOptions().setConfs(new String[] { "runtime" }));
    assertFalse(report.hasError());
    List<Artifact> artifacts = report.getArtifacts();
    assertEquals(3, artifacts.size());
    assertEquals("test#a;1!a.jar", artifacts.get(0).toString());
    assertEquals("test#c;1!c.jar", artifacts.get(1).toString());
    assertEquals("test#b;1!b.jar", artifacts.get(2).toString());
}
Also used : ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) Ivy(org.apache.ivy.Ivy) JarFile(java.util.jar.JarFile) File(java.io.File) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Test(org.junit.Test)

Example 40 with Artifact

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

the class PomModuleDescriptorParserTest method testParent.

@Test
public void testParent() throws Exception {
    ModuleDescriptor md = PomModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("test-parent.pom"), false);
    assertNotNull(md);
    ModuleRevisionId mrid = ModuleRevisionId.newInstance("org.apache", "test", "1.0");
    assertEquals(mrid, md.getModuleRevisionId());
    Artifact[] artifact = md.getArtifacts("master");
    assertEquals(1, artifact.length);
    assertEquals(mrid, artifact[0].getModuleRevisionId());
    assertEquals("test", artifact[0].getName());
}
Also used : DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Artifact(org.apache.ivy.core.module.descriptor.Artifact) XmlModuleDescriptorParserTest(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest) Test(org.junit.Test)

Aggregations

Artifact (org.apache.ivy.core.module.descriptor.Artifact)106 Test (org.junit.Test)68 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)59 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)55 File (java.io.File)53 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)31 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)30 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)28 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)25 Date (java.util.Date)22 DownloadReport (org.apache.ivy.core.report.DownloadReport)20 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)15 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)13 Configuration (org.apache.ivy.core.module.descriptor.Configuration)10 IOException (java.io.IOException)9 PublishArtifact (org.gradle.api.artifacts.PublishArtifact)9 ParseException (java.text.ParseException)8 XmlModuleDescriptorParserTest (org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest)8 ResolveReport (org.apache.ivy.core.report.ResolveReport)7 DownloadOptions (org.apache.ivy.core.resolve.DownloadOptions)7