/** put the dependencies that we are interested in using into the runtime object */
  private void prepareRuntimeDependencies(Runtime runtime, MavenProject project) {
    List artifactList = project.getRuntimeArtifacts();
    List dependencies = runtime.getJar().getDependencies();

    for (Iterator i = artifactList.iterator(); i.hasNext(); ) {
      Artifact artifact = (Artifact) i.next();

      String dependency =
          artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion();

      // let preexisting dependencies take precedence
      if (!dependencies.contains(dependency)) {
        runtime.getJar().addDependency(dependency);
      }
    }
  }
  public void build(Runtime runtime, MavenProject project, File runtimeFile, File outputDirectory)
      throws MavenRuntimeBuilderException {
    try {
      // prepare output directories
      initializeOutputDirectories(runtime, outputDirectory);

      // add needed dependencies to the runtime object
      prepareRuntimeDependencies(runtime, project);

      prepareRuntimeClasses();

      prepareRuntimeExecutionDependencies(project);

      // we need all the compiled classes into a jar of their own
      JarMaker projectJar =
          new JarMaker(
              runtimeLibraryDirectory,
              project.getArtifactId() + "-" + project.getVersion() + "-runtime.jar");

      projectJar.addDirectory(
          "**/**",
          "**/package.html,**/.svn/**",
          "",
          new File(project.getBuild().getDirectory() + File.separator + "classes"));
      projectJar.create();

      // make a dependency reference for this newly created jar
      runtime
          .getJar()
          .addDependency(
              project.getGroupId()
                  + ":"
                  + project.getArtifactId()
                  + ":"
                  + project.getVersion()
                  + "-runtime");

      // place the finalized runtime descriptor
      RuntimeXpp3Writer runtimeWriter = new RuntimeXpp3Writer();

      FileWriter fw = new FileWriter(new File(runtimeMetaDirectory, "jar.runtime"));
      runtimeWriter.write(fw, runtime);
      fw.close();

      // finally generate the end game jar
      JarMaker runtimeJar =
          new JarMaker(
              outputDirectory,
              project.getArtifactId() + "-runtime-" + project.getVersion() + ".jar");
      runtimeJar.addDirectory("**/**", "**/package.html,**/.svn/**", "", runtimeOutputDirectory);
      runtimeJar.addManifestEntry(
          "Main-Class", "org.codehaus.mojo.runtime.execute.RuntimeExecutor");
      runtimeJar.create();
    } catch (IOException ex) {
      throw new MavenRuntimeBuilderException("Exception while building the runtime", ex);
    }
  }
  private void initializeOutputDirectories(Runtime runtime, File outputDirectory)
      throws MavenRuntimeBuilderException {
    runtimeOutputDirectory = new File(outputDirectory, runtime.getJar().getId());
    runtimeLibraryDirectory = new File(runtimeOutputDirectory, "lib");
    runtimeExecutableDirectory = new File(runtimeOutputDirectory, "bin");
    runtimeMetaDirectory = new File(runtimeOutputDirectory, "META-INF/runtimes");

    mkdir(runtimeOutputDirectory);
    mkdir(runtimeLibraryDirectory);
    mkdir(runtimeExecutableDirectory);
    mkdir(runtimeMetaDirectory);
  }