@Override
  protected List<File> getDependencies() {
    ArrayList<File> dependencies = new ArrayList<File>();

    @SuppressWarnings("unchecked")
    final Iterator<Artifact> it = project.getArtifacts().iterator();
    while (it.hasNext()) {
      final Artifact declared = it.next();
      this.log.debug("Checking artifact " + declared);
      if (this.isJavaArtifact(declared)) {
        if (Artifact.SCOPE_COMPILE.equals(declared.getScope())
            || Artifact.SCOPE_RUNTIME.equals(declared.getScope())
            || Artifact.SCOPE_PROVIDED.equals(declared.getScope())
            || Artifact.SCOPE_SYSTEM.equals(declared.getScope())) {
          this.log.debug("Resolving artifact " + declared);
          if (declared.getFile() != null) {
            dependencies.add(declared.getFile());
          } else {
            this.log.debug("Unable to resolve artifact " + declared);
          }
        } else {
          this.log.debug(
              "Artifact "
                  + declared
                  + " has not scope compile or runtime, but "
                  + declared.getScope());
        }
      } else {
        this.log.debug(
            "Artifact " + declared + " is not a java artifact, type is " + declared.getType());
      }
    }

    return dependencies;
  }
Пример #2
0
  private Artifact XcreateArtifact(
      String groupId,
      String artifactId,
      VersionRange versionRange,
      String type,
      String classifier,
      String scope,
      String inheritedScope,
      boolean optional) {
    String desiredScope = Artifact.SCOPE_RUNTIME;

    if (inheritedScope == null) {
      desiredScope = scope;
    } else if (Artifact.SCOPE_TEST.equals(scope) || Artifact.SCOPE_PROVIDED.equals(scope)) {
      return null;
    } else if (Artifact.SCOPE_COMPILE.equals(scope)
        && Artifact.SCOPE_COMPILE.equals(inheritedScope)) {
      // added to retain compile artifactScope. Remove if you want compile inherited as runtime
      desiredScope = Artifact.SCOPE_COMPILE;
    }

    if (Artifact.SCOPE_TEST.equals(inheritedScope)) {
      desiredScope = Artifact.SCOPE_TEST;
    }

    if (Artifact.SCOPE_PROVIDED.equals(inheritedScope)) {
      desiredScope = Artifact.SCOPE_PROVIDED;
    }

    if (Artifact.SCOPE_SYSTEM.equals(scope)) {
      // system scopes come through unchanged...
      desiredScope = Artifact.SCOPE_SYSTEM;
    }

    ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(type);

    return new DefaultArtifact(
        groupId, artifactId, versionRange, desiredScope, type, classifier, handler, optional);
  }
Пример #3
0
  private Set<Artifact> getNativeDependenciesArtifacts() {
    Set<Artifact> filteredArtifacts = new HashSet<Artifact>();
    @SuppressWarnings("unchecked")
    final Set<Artifact> allArtifacts = project.getDependencyArtifacts();

    for (Artifact artifact : allArtifacts) {
      if ("so".equals(artifact.getType())
          && (Artifact.SCOPE_COMPILE.equals(artifact.getScope())
              || Artifact.SCOPE_RUNTIME.equals(artifact.getScope()))) {
        filteredArtifacts.add(artifact);
      }
    }

    return filteredArtifacts;
  }
  public Set<Artifact> getNativeDependenciesArtifacts(File unpackDirectory, boolean sharedLibraries)
      throws MojoExecutionException {
    final Set<Artifact> filteredArtifacts = new LinkedHashSet<Artifact>();

    // Add all dependent artifacts declared in the pom file
    @SuppressWarnings("unchecked")
    final Set<Artifact> allArtifacts = project.getDependencyArtifacts();

    // Add all attached artifacts as well - this could come from the NDK mojo for example
    boolean result = allArtifacts.addAll(project.getAttachedArtifacts());

    for (Artifact artifact : allArtifacts) {
      // A null value in the scope indicates that the artifact has been attached
      // as part of a previous build step (NDK mojo)
      if (isNativeLibrary(sharedLibraries, artifact.getType()) && artifact.getScope() == null) {
        // Including attached artifact
        log.debug(
            "Including attached artifact: "
                + artifact.getArtifactId()
                + "("
                + artifact.getGroupId()
                + ")");
        filteredArtifacts.add(artifact);
      } else if (isNativeLibrary(sharedLibraries, artifact.getType())
          && (Artifact.SCOPE_COMPILE.equals(artifact.getScope())
              || Artifact.SCOPE_RUNTIME.equals(artifact.getScope()))) {
        filteredArtifacts.add(artifact);
      } else if (APKLIB.equals(artifact.getType())) {
        // Check if the artifact contains a libs folder - if so, include it in the list
        File libsFolder =
            new File(
                AbstractAndroidMojo.getLibraryUnpackDirectory(unpackDirectory, artifact) + "/libs");
        if (libsFolder.exists()) {
          filteredArtifacts.add(artifact);
        }
      }
    }

    Set<Artifact> transientArtifacts =
        processTransientDependencies(project.getDependencies(), sharedLibraries);

    filteredArtifacts.addAll(transientArtifacts);

    return filteredArtifacts;
  }
  protected List<IMavenProjectFacade> getWorkspaceDependencies(
      IProject project, MavenProject mavenProject) {
    Set<IProject> projects = new HashSet<IProject>();
    List<IMavenProjectFacade> dependencies = new ArrayList<IMavenProjectFacade>();
    Set<Artifact> artifacts = mavenProject.getArtifacts();
    for (Artifact artifact : artifacts) {
      IMavenProjectFacade dependency =
          projectManager.getMavenProject(
              artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());

      if ((Artifact.SCOPE_COMPILE.equals(artifact.getScope())
              || Artifact.SCOPE_RUNTIME.equals(
                  artifact.getScope())) // MNGECLIPSE-1578 Runtime dependencies should be deployed
          && dependency != null
          && !dependency.getProject().equals(project)
          && dependency.getFullPath(artifact.getFile()) != null
          && projects.add(dependency.getProject())) {
        dependencies.add(dependency);
      }
    }
    return dependencies;
  }
  public void configureClasspath(
      IProject project,
      MavenProject mavenProject,
      IClasspathDescriptor classpath,
      IProgressMonitor monitor)
      throws CoreException {

    // Improve skinny war support by generating the manifest classpath
    // similar to mvn eclipse:eclipse
    // http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html
    WarPluginConfiguration config = new WarPluginConfiguration(mavenProject, project);
    WarPackagingOptions opts = new WarPackagingOptions(config);

    StringBuilder manifestCp = new StringBuilder();

    /*
     * Need to take care of three separate cases
     *
     * 1. remove any project dependencies (they are represented as J2EE module dependencies)
     * 2. add non-dependency attribute for entries originated by artifacts with
     *    runtime, system, test scopes or optional dependencies (not sure about the last one)
     * 3. make sure all dependency JAR files have unique file names, i.e. artifactId/version collisions
     */

    Set<String> dups = new LinkedHashSet<String>();
    Set<String> names = new HashSet<String>();

    // first pass removes projects, adds non-dependency attribute and collects colliding filenames
    Iterator<IClasspathEntryDescriptor> iter = classpath.getEntryDescriptors().iterator();
    while (iter.hasNext()) {
      IClasspathEntryDescriptor descriptor = iter.next();
      IClasspathEntry entry = descriptor.getClasspathEntry();
      String scope = descriptor.getScope();
      String key =
          ArtifactUtils.versionlessKey(descriptor.getGroupId(), descriptor.getArtifactId());
      Artifact artifact = mavenProject.getArtifactMap().get(key);
      String extension = artifact.getArtifactHandler().getExtension();

      if (IClasspathEntry.CPE_PROJECT == entry.getEntryKind()
          && Artifact.SCOPE_COMPILE.equals(scope)) {
        // get deployed name for project dependencies
        // TODO can this be done somehow more elegantly?
        IProject p =
            (IProject) ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());

        IVirtualComponent component = ComponentCore.createComponent(p);

        boolean usedInEar = opts.isReferenceFromEar(component, extension);
        if (opts.isSkinnyWar() && usedInEar) {
          if (manifestCp.length() > 0) {
            manifestCp.append(" ");
          }
          // MNGECLIPSE-2393 prepend ManifestClasspath prefix
          if (config.getManifestClasspathPrefix() != null
              && !JEEPackaging.isJEEPackaging(artifact.getType())) {
            manifestCp.append(config.getManifestClasspathPrefix());
          }

          manifestCp.append(component.getDeployedName()).append(".").append(extension);
        }

        if (!descriptor.isOptionalDependency() || usedInEar) {
          // remove mandatory project dependency from classpath
          iter.remove();
          continue;
        } // else : optional dependency not used in ear -> need to trick ClasspathAttribute with
          // NONDEPENDENCY_ATTRIBUTE
      }

      if (opts.isSkinnyWar() && opts.isReferenceFromEar(descriptor)) {

        if (manifestCp.length() > 0) {
          manifestCp.append(" ");
        }
        if (config.getManifestClasspathPrefix() != null
            && !JEEPackaging.isJEEPackaging(artifact.getType())) {
          manifestCp.append(config.getManifestClasspathPrefix());
        }
        manifestCp.append(entry.getPath().lastSegment());

        // ear references aren't kept in the Maven Dependencies
        iter.remove();
        continue;
      }

      // add non-dependency attribute
      // Check the scope & set WTP non-dependency as appropriate
      // Optional artifact shouldn't be deployed
      if (Artifact.SCOPE_PROVIDED.equals(scope)
          || Artifact.SCOPE_TEST.equals(scope)
          || Artifact.SCOPE_SYSTEM.equals(scope)
          || descriptor.isOptionalDependency()) {
        descriptor.addClasspathAttribute(NONDEPENDENCY_ATTRIBUTE);
      }

      // collect duplicate file names
      if (!names.add(entry.getPath().lastSegment())) {
        dups.add(entry.getPath().lastSegment());
      }
    }

    String targetDir = mavenProject.getBuild().getDirectory();

    // second pass disambiguates colliding entry file names
    iter = classpath.getEntryDescriptors().iterator();
    while (iter.hasNext()) {
      IClasspathEntryDescriptor descriptor = iter.next();
      IClasspathEntry entry = descriptor.getClasspathEntry();

      if (dups.contains(entry.getPath().lastSegment())) {
        File src = new File(entry.getPath().toOSString());
        String groupId = descriptor.getGroupId();
        File dst = new File(targetDir, groupId + "-" + entry.getPath().lastSegment());
        try {
          if (src.canRead()) {
            if (isDifferent(src, dst)) { // uses lastModified
              FileUtils.copyFile(src, dst);
              dst.setLastModified(src.lastModified());
            }
            descriptor.setClasspathEntry(
                JavaCore.newLibraryEntry(
                    Path.fromOSString(dst.getCanonicalPath()), //
                    entry.getSourceAttachmentPath(), //
                    entry.getSourceAttachmentRootPath(), //
                    entry.getAccessRules(), //
                    entry.getExtraAttributes(), //
                    entry.isExported()));
          }
        } catch (IOException ex) {
          MavenLogger.log("File copy failed", ex);
        }
      }
    }

    if (opts.isSkinnyWar()) {

      // writing the manifest only works when the project has been properly created
      // placing this check on the top of the method broke 2 other tests
      // thats why its placed here now.
      if (ComponentCore.createComponent(project) == null) {
        return;
      }

      // write manifest, using internal API - seems ok for 3.4/3.5, though
      ArchiveManifest mf = J2EEProjectUtilities.readManifest(project);
      if (mf == null) {
        mf = new ArchiveManifestImpl();
      }
      mf.addVersionIfNecessary();
      mf.setClassPath(manifestCp.toString());

      try {
        J2EEProjectUtilities.writeManifest(project, mf);
      } catch (Exception ex) {
        MavenLogger.log("Could not write web module manifest file", ex);
      }
    }
  }
  public void configureClasspath(
      IProject project,
      MavenProject mavenProject,
      IClasspathDescriptor classpath,
      IProgressMonitor monitor)
      throws CoreException {

    // Improve skinny war support by generating the manifest classpath
    // similar to mvn eclipse:eclipse
    // http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html
    WarPluginConfiguration config = new WarPluginConfiguration(mavenProject, project);
    IPackagingConfiguration opts =
        new PackagingConfiguration(config.getPackagingIncludes(), config.getPackagingExcludes());

    /*
     * Need to take care of three separate cases
     *
     * 1. remove any project dependencies (they are represented as J2EE module dependencies)
     * 2. add non-dependency attribute for entries originated by artifacts with
     *    runtime, system, test scopes or optional dependencies (not sure about the last one)
     * 3. make sure all dependency JAR files have unique file names, i.e. artifactId/version collisions
     */

    Set<String> dups = new LinkedHashSet<String>();
    Set<String> names = new HashSet<String>();
    FileNameMapping fileNameMapping = config.getFileNameMapping();
    String targetDir = mavenProject.getBuild().getDirectory();

    // first pass removes projects, adds non-dependency attribute and collects colliding filenames
    Iterator<IClasspathEntryDescriptor> iter = classpath.getEntryDescriptors().iterator();
    while (iter.hasNext()) {
      IClasspathEntryDescriptor descriptor = iter.next();
      IClasspathEntry entry = descriptor.toClasspathEntry();
      String scope = descriptor.getScope();
      Artifact artifact =
          ArtifactHelper.getArtifact(mavenProject.getArtifacts(), descriptor.getArtifactKey());

      ArtifactHelper.fixArtifactHandler(artifact.getArtifactHandler());

      String deployedName = fileNameMapping.mapFileName(artifact);

      boolean isDeployed =
          (Artifact.SCOPE_COMPILE.equals(scope) || Artifact.SCOPE_RUNTIME.equals(scope))
              && !descriptor.isOptionalDependency()
              && opts.isPackaged("WEB-INF/lib/" + deployedName)
              && !isWorkspaceProject(artifact);

      // add non-dependency attribute if this classpathentry is not meant to be deployed
      // or if it's a workspace project (projects already have a reference created in configure())
      if (!isDeployed) {
        descriptor.setClasspathAttribute(
            NONDEPENDENCY_ATTRIBUTE.getName(), NONDEPENDENCY_ATTRIBUTE.getValue());
      }

      // If custom fileName is used, then copy the artifact and rename the artifact under the build
      // dir
      String fileName = entry.getPath().lastSegment();
      if (!deployedName.equals(fileName)) {
        IPath newPath = renameArtifact(targetDir, entry.getPath(), deployedName);
        if (newPath != null) {
          descriptor.setPath(newPath);
        }
      }

      if (!names.add(deployedName)) {
        dups.add(deployedName);
      }
    }

    // second pass disambiguates colliding entry file names
    iter = classpath.getEntryDescriptors().iterator();
    while (iter.hasNext()) {
      IClasspathEntryDescriptor descriptor = iter.next();
      IClasspathEntry entry = descriptor.toClasspathEntry();

      if (dups.contains(entry.getPath().lastSegment())) {
        String newName = descriptor.getGroupId() + "-" + entry.getPath().lastSegment();
        IPath newPath = renameArtifact(targetDir, entry.getPath(), newName);
        if (newPath != null) {
          descriptor.setPath(newPath);
        }
      }
    }
  }
Пример #8
0
  public Set<Artifact> getNativeDependenciesArtifacts(
      AbstractAndroidMojo mojo, File unpackDirectory, boolean sharedLibraries)
      throws MojoExecutionException {
    log.debug(
        "Finding native dependencies. UnpackFolder="
            + unpackDirectory
            + " shared="
            + sharedLibraries);
    final Set<Artifact> filteredArtifacts = new LinkedHashSet<Artifact>();
    final Set<Artifact> allArtifacts = new LinkedHashSet<Artifact>();

    // Add all dependent artifacts declared in the pom file
    // Note: The result of project.getDependencyArtifacts() can be an UnmodifiableSet so we
    //       have created our own above and add to that.
    allArtifacts.addAll(project.getDependencyArtifacts());

    // Add all attached artifacts as well - this could come from the NDK mojo for example
    allArtifacts.addAll(project.getAttachedArtifacts());

    // Add all transitive artifacts as well
    // this allows armeabi classifier -> apklib -> apklib -> apk chaining to only include armeabi in
    // APK
    allArtifacts.addAll(project.getArtifacts());

    for (Artifact artifact : allArtifacts) {
      log.debug("Checking artifact : " + artifact);
      // A null value in the scope indicates that the artifact has been attached
      // as part of a previous build step (NDK mojo)
      if (isNativeLibrary(sharedLibraries, artifact.getType()) && artifact.getScope() == null) {
        // Including attached artifact
        log.debug("Including attached artifact: " + artifact + ". Artifact scope is not set.");
        filteredArtifacts.add(artifact);
      } else {
        if (isNativeLibrary(sharedLibraries, artifact.getType())
            && (Artifact.SCOPE_COMPILE.equals(artifact.getScope())
                || Artifact.SCOPE_RUNTIME.equals(artifact.getScope()))) {
          log.debug(
              "Including attached artifact: "
                  + artifact
                  + ". Artifact scope is Compile or Runtime.");
          filteredArtifacts.add(artifact);
        } else {
          final String type = artifact.getType();

          if (APKLIB.equals(type)) {
            // Check if the artifact contains a libs folder - if so, include it in the list
            File libsFolder = null;
            if (mojo != null) {
              libsFolder = mojo.getUnpackedLibNativesFolder(artifact);
            } else {
              // This is used from NativeHelperTest since we have no AbstractAndroidMojo there
              libsFolder =
                  new File(
                      AbstractAndroidMojo.getLibraryUnpackDirectory(unpackDirectory, artifact),
                      "libs");
            }

            if (!libsFolder.exists()) {
              log.debug("Skipping " + libsFolder.getAbsolutePath() + " for native artifacts");
              continue;
            }

            if (!libsFolder.isDirectory()) {
              continue;
            }

            log.debug("Checking " + libsFolder.getAbsolutePath() + " for native artifacts");
            // make sure we ignore libs folders that only contain JARs
            // The regular expression filters out all file paths ending with '.jar' or '.JAR',
            // so all native libs remain
            if (libsFolder.list(new PatternFilenameFilter("^.*(?<!(?i)\\.jar)$")).length > 0) {
              log.debug("Including attached artifact: " + artifact + ". Artifact is APKLIB.");
              filteredArtifacts.add(artifact);
            }
          } else if (!"jar".equals(type)) {
            log.debug("Not checking " + type + " for native artifacts");
          }
        }
      }
    }

    Set<Artifact> transitiveArtifacts =
        processTransitiveDependencies(project.getDependencies(), sharedLibraries);

    filteredArtifacts.addAll(transitiveArtifacts);

    return filteredArtifacts;
  }
  public Set<Artifact> getNativeDependenciesArtifacts(File unpackDirectory, boolean sharedLibraries)
      throws MojoExecutionException {
    final Set<Artifact> filteredArtifacts = new LinkedHashSet<Artifact>();
    final Set<Artifact> allArtifacts = new LinkedHashSet<Artifact>();

    // Add all dependent artifacts declared in the pom file
    // Note: The result of project.getDependencyArtifacts() can be an UnmodifiableSet so we
    //       have created our own above and add to that.
    allArtifacts.addAll(project.getDependencyArtifacts());

    // Add all attached artifacts as well - this could come from the NDK mojo for example
    allArtifacts.addAll(project.getAttachedArtifacts());

    for (Artifact artifact : allArtifacts) {
      // A null value in the scope indicates that the artifact has been attached
      // as part of a previous build step (NDK mojo)
      if (isNativeLibrary(sharedLibraries, artifact.getType()) && artifact.getScope() == null) {
        // Including attached artifact
        log.debug(
            "Including attached artifact: "
                + artifact.getArtifactId()
                + "("
                + artifact.getGroupId()
                + "). Artifact scope is not set.");
        filteredArtifacts.add(artifact);
      } else {
        if (isNativeLibrary(sharedLibraries, artifact.getType())
            && (Artifact.SCOPE_COMPILE.equals(artifact.getScope())
                || Artifact.SCOPE_RUNTIME.equals(artifact.getScope()))) {
          log.debug(
              "Including attached artifact: "
                  + artifact.getArtifactId()
                  + "("
                  + artifact.getGroupId()
                  + "). Artifact scope is Compile or Runtime.");
          filteredArtifacts.add(artifact);
        } else {
          if (APKLIB.equals(artifact.getType())) {
            // Check if the artifact contains a libs folder - if so, include it in the list
            File libsFolder =
                new File(
                    AbstractAndroidMojo.getLibraryUnpackDirectory(unpackDirectory, artifact),
                    "libs");
            // make sure we ignore libs folders that only contain JARs
            // The regular expression filters out all file paths ending with '.jar' or '.JAR',
            // so all native libs remain
            if (libsFolder.exists()
                && libsFolder.list(new PatternFilenameFilter("^.*(?<!(?i)\\.jar)$")).length > 0) {
              log.debug(
                  "Including attached artifact: "
                      + artifact.getArtifactId()
                      + "("
                      + artifact.getGroupId()
                      + "). Artifact is APKLIB.");
              filteredArtifacts.add(artifact);
            }
          }
        }
      }
    }

    Set<Artifact> transientArtifacts =
        processTransientDependencies(project.getDependencies(), sharedLibraries);

    filteredArtifacts.addAll(transientArtifacts);

    return filteredArtifacts;
  }