public String exportResources( VariableSpace space, Map<String, ResourceDefinition> definitions,
      ResourceNamingInterface namingInterface, Repository repository, IMetaStore metaStore ) throws KettleException {
    String resourceName = null;
    try {
      // Handle naming for both repository and XML bases resources...
      //
      String baseName;
      String originalPath;
      String fullname;
      String extension = "kjb";
      if ( Utils.isEmpty( getFilename() ) ) {
        // Assume repository...
        //
        originalPath = directory.getPath();
        baseName = getName();
        fullname =
            directory.getPath() + ( directory.getPath().endsWith( RepositoryDirectory.DIRECTORY_SEPARATOR ) ? ""
                : RepositoryDirectory.DIRECTORY_SEPARATOR ) + getName() + "." + extension; //
      } else {
        // Assume file
        //
        FileObject fileObject = KettleVFS.getFileObject( space.environmentSubstitute( getFilename() ), space );
        originalPath = fileObject.getParent().getName().getPath();
        baseName = fileObject.getName().getBaseName();
        fullname = fileObject.getName().getPath();
      }

      resourceName = namingInterface
          .nameResource( baseName, originalPath, extension, ResourceNamingInterface.FileNamingType.JOB );
      ResourceDefinition definition = definitions.get( resourceName );
      if ( definition == null ) {
        // If we do this once, it will be plenty :-)
        //
        JobMeta jobMeta = (JobMeta) this.realClone( false );

        // All objects get re-located to the root folder,
        // but, when exporting, we need to see current directory
        // in order to make 'Internal.Entry.Current.Directory' variable work
        jobMeta.setRepositoryDirectory( directory );

        // Add used resources, modify transMeta accordingly
        // Go through the list of steps, etc.
        // These critters change the steps in the cloned TransMeta
        // At the end we make a new XML version of it in "exported"
        // format...

        // loop over steps, databases will be exported to XML anyway.
        //
        for ( JobEntryCopy jobEntry : jobMeta.jobcopies ) {
          compatibleJobEntryExportResources( jobEntry.getEntry(), jobMeta, definitions, namingInterface, repository );
          jobEntry.getEntry().exportResources( jobMeta, definitions, namingInterface, repository, metaStore );
        }

        // Set a number of parameters for all the data files referenced so far...
        //
        Map<String, String> directoryMap = namingInterface.getDirectoryMap();
        if ( directoryMap != null ) {
          for ( Map.Entry<String, String> entry : directoryMap.entrySet() ) {
            jobMeta.addParameterDefinition( entry.getValue(), entry.getKey(), "Data file path discovered during export" );
          }
        }

        // At the end, add ourselves to the map...
        //
        String jobMetaContent = jobMeta.getXML();

        definition = new ResourceDefinition( resourceName, jobMetaContent );

        // Also remember the original filename (if any), including variables etc.
        //
        if ( Utils.isEmpty( this.getFilename() ) ) { // Repository
          definition.setOrigin( fullname );
        } else {
          definition.setOrigin( this.getFilename() );
        }

        definitions.put( fullname, definition );
      }
    } catch ( FileSystemException e ) {
      throw new KettleException(
          BaseMessages.getString( PKG, "JobMeta.Exception.AnErrorOccuredReadingJob", getFilename() ), e );
    } catch ( KettleFileException e ) {
      throw new KettleException(
          BaseMessages.getString( PKG, "JobMeta.Exception.AnErrorOccuredReadingJob", getFilename() ), e );
    }

    return resourceName;
  }
@Test
  public void shouldUseExistingRepositoryDirectoryWhenExporting() throws KettleException {
    final JobMeta jobMetaSpy = spy( jobMeta );
    JobMeta jobMeta = new JobMeta() {
      @Override
      public Object realClone( boolean doClear ) {
        return jobMetaSpy;
      }
    };
    jobMeta.setRepositoryDirectory( directoryJob );
    jobMeta.setName( JOB_META_NAME );
    jobMeta.exportResources( null, new HashMap<String, ResourceDefinition>( 4 ), mock( ResourceNamingInterface.class ),
        null, null );

    // assert
    verify( jobMetaSpy ).setRepositoryDirectory( directoryJob );
  }