title: ivy publish : file not found in cache
body: <p>I am trying to publish an artifact to Artifactory using Java.</p>
  
  <p>Here is the code.</p>
  
  <pre><code>public static void publishToArtifactoryPro(String groupId,
                                             String artifactId,
                                             String version,
                                             Collection artifacts) throws JarMigrationServiceException{
  
      // create an ivy instance
      IvySettings ivySettings = new IvySettings();
      ivySettings.setDefaultCache(new File("ivy/cache"));
  
      // use the biblio resolver, if you consider resolving 
      // POM declared dependencies
      IBiblioResolver br = new IBiblioResolver();
      br.setM2compatible(true);
      br.setUsepoms(true);
      br.setName("ibiblio");
      br.setRoot(Constants.ARTIFACTORY_PRO_RELEASE_LOCAL);
  
      ivySettings.addResolver(br);
      ivySettings.setDefaultResolver(br.getName());
  
      Ivy ivy = Ivy.newInstance(ivySettings);
  
      //you always need to resolve before you can retrieve
      ResolveOptions ro = new ResolveOptions();
      // this seems to have no impact, if you resolve by module descriptor (in contrast to resolve by ModuleRevisionId)
      ro.setTransitive(true);
      // if set to false, nothing will be downloaded
      ro.setDownload(true);
  
      // 1st create an ivy module (this always(!) has a "default" configuration already)
      DefaultModuleDescriptor md = DefaultModuleDescriptor.newDefaultInstance(
          // give it some related name (so it can be cached)
          ModuleRevisionId.newInstance(
              groupId, 
              artifactId+"-envelope", 
              version
          )
      );
  
      // 2. add dependencies for what we are really looking for
      ModuleRevisionId ri = ModuleRevisionId.newInstance(
          groupId, 
          artifactId,
          version
      );
      // don't go transitive here, if you want the single artifact
      DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, ri, false, false, false);
  
      // map to master to just get the code jar. See generated ivy module xmls from maven repo
      // on how configurations are mapped into ivy. Or check 
      // e.g. http://lightguard-jp.blogspot.de/2009/04/ivy-configurations-when-pulling-from.html
      dd.addDependencyConfiguration("default", "master");
      md.addDependency(dd);
  
      // now resolve
      ResolveReport rr = null;
      try {
          rr = ivy.resolve(md,ro);
          if (rr.hasError()) {
              throw new RuntimeException(rr.getAllProblemMessages().toString());
          }
      } 
      catch (ParseException e) {
          throw new JarMigrationServiceException(e.getMessage(), e);
      } 
      catch (IOException e) {
          throw new JarMigrationServiceException(e.getMessage(), e);
      }
  
      try {
          ivy.publish(
                  ModuleRevisionId.newInstance(groupId, artifactId, version), 
                  artifacts, 
                  "artifactory-publish", 
                  new PublishOptions()
                  // this is from the envelop module
                  .setConfs(new String[]{"default"})
                  );
  
      } 
      catch (IOException e) {
          throw new JarMigrationServiceException(e.getMessage(), e);
      }
  }
  </code></pre>
  
  <p>Here is the error:</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalStateException: ivy file not found in cache for com.mycompany#MAO;1.1.2: please resolve dependencies before publishing (ivy/cache/resolved-com.mycompany-MAO-1.1.2.xml)
  at org.apache.ivy.core.publish.PublishEngine.publish(PublishEngine.java:105)
  at org.apache.ivy.Ivy.publish(Ivy.java:600)
  at com.mycompany.is.dt.Utilities.JarMigrationUtility.publishToArtifactoryPro(JarMigrationUtility.java:96)
  at com.mycompany.is.dt.controllers.JarMigrationService.main(JarMigrationService.java:29)
  </code></pre>
  
  <p>So I am confused because it says <code>ivy file not found in cache</code> but you can see that I am setting the cache here <code>ivySettings.setDefaultCache(new File("ivy/cache"));</code> And when I go to <code>ivy/cache/com/mycompany/MAO</code> I see the jar there... Any ideas?</p>