/**
   * If the given jarResource is configured with a main class, the underlying artifact is checked to
   * see if it actually contains the specified class.
   *
   * @param jarResource
   * @throws IllegalStateException if the jarResource's underlying artifact has not yet been
   *     resolved.
   * @throws MojoExecutionException
   */
  private void checkForMainClass(JarResource jarResource) throws MojoExecutionException {

    String mainClass = jarResource.getMainClass();

    if (mainClass == null) {
      return;
    }

    Artifact artifact = jarResource.getArtifact();

    if (artifact == null) {
      throw new IllegalStateException(
          "Implementation Error: The given jarResource cannot be checked for "
              + "a main class until the underlying artifact has been resolved: ["
              + jarResource
              + "]");
    }

    try {
      if (!artifactContainsClass(artifact, mainClass)) {
        throw new MojoExecutionException(
            "The jar specified by the following jarResource does not contain the declared main class:"
                + jarResource);
      }
    } catch (MalformedURLException e) {
      throw new MojoExecutionException(
          "Attempting to find main class [" + mainClass + "] in [" + artifact + "]", e);
    }
  }
  /**
   * Resolve the artifacts represented by the given collection of JarResources and copy them to the
   * working directory if a newer copy of the file doesn't already exist there. Transitive
   * dependencies will also be retrieved.
   *
   * <p>Transitive dependencies are added to the list specified as parameter. TODO fix that.
   *
   * @throws MojoExecutionException
   */
  private void retrieveJarResources(List jarResources) throws MojoExecutionException {

    Set jarResourceArtifacts = new HashSet();

    try {
      // for each configured JarResource, create and resolve the corresponding artifact and
      // check it for the mainClass if specified
      for (Iterator itr = jarResources.iterator(); itr.hasNext(); ) {
        JarResource jarResource = (JarResource) itr.next();
        Artifact artifact = createArtifact(jarResource);
        getArtifactResolver().resolve(artifact, getRemoteRepositories(), getLocalRepository());
        jarResource.setArtifact(artifact);
        checkForMainClass(jarResource);
        jarResourceArtifacts.add(artifact);
      }

      if (!isExcludeTransitive()) {

        retrieveTransitiveDependencies(jarResourceArtifacts, jarResources);
      }

      // for each JarResource, copy its artifact to the lib directory if necessary
      for (Iterator itr = jarResources.iterator(); itr.hasNext(); ) {
        JarResource jarResource = (JarResource) itr.next();
        Artifact artifact = jarResource.getArtifact();
        boolean copied =
            copyJarAsUnprocessedToDirectoryIfNecessary(artifact.getFile(), getLibDirectory());

        if (copied) {
          String name = artifact.getFile().getName();
          if (getLog().isDebugEnabled()) {
            getLog().debug("Adding " + name + " to modifiedJnlpArtifacts list.");
          }
          getModifiedJnlpArtifacts().add(name.substring(0, name.lastIndexOf('.')));
        }

        if (jarResource.isOutputJarVersion()) {
          // Create and set a version-less href for this jarResource
          String hrefValue = buildHrefValue(artifact);
          jarResource.setHrefValue(hrefValue);
        }
      }

    } catch (ArtifactResolutionException e) {
      throw new MojoExecutionException("Unable to resolve an artifact", e);
    } catch (ArtifactNotFoundException e) {
      throw new MojoExecutionException("Unable to find an artifact", e);
    } catch (IOException e) {
      throw new MojoExecutionException("Unable to copy an artifact to the working directory", e);
    }
  }