/**
   * Returns <code>true</code> if the update should be applied.
   *
   * @param artifact The artifact.
   * @param currentVersion The current version of the artifact.
   * @param updateVersion The proposed new version of the artifact.
   * @return <code>true</code> if the update should be applied.
   * @since 1.0-alpha-1
   */
  protected boolean shouldApplyUpdate(
      Artifact artifact, String currentVersion, ArtifactVersion updateVersion) {
    getLog().debug("Proposal is to update from " + currentVersion + " to " + updateVersion);

    if (updateVersion == null) {
      getLog().warn("Not updating version: could not resolve any versions");
      return false;
    }

    artifact.setVersion(updateVersion.toString());
    try {
      resolver.resolveAlways(artifact, remoteArtifactRepositories, localRepository);
    } catch (ArtifactResolutionException e) {
      getLog().warn("Not updating version: could not resolve " + artifact.toString(), e);
      return false;
    } catch (ArtifactNotFoundException e) {
      getLog().warn("Not updating version: could not find " + artifact.toString(), e);
      return false;
    }

    if (currentVersion.equals(updateVersion.toString())) {
      getLog().info("Current version of " + artifact.toString() + " is the latest.");
      return false;
    }
    return true;
  }
 @Override
 public boolean accept(Artifact artifact) {
   for (Filter filter : filters) {
     if (filter.accept(artifact)) {
       return result(true, artifact.toString());
     }
   }
   return result(false, artifact.toString());
 }
 protected File getFile(String artifactName) {
   // artifactMap is a bit too simplistic
   for (Artifact artifact : project.getArtifacts())
     if (artifact.toString().startsWith(artifactName)) return artifact.getFile();
   throw new IllegalArgumentException(
       "could not find " + artifactName + " in " + project.getArtifacts());
 }
  /**
   * Detect if this artifact is already an osgi bundle. If it is, then we don't need to wrap it. The
   * best way to figure this out is to crack open the JAR and take a look at the manifest.
   *
   * @param artifact
   * @throws Exception
   */
  protected boolean isBundle(Artifact artifact) throws Exception {
    // Resolve the artifact.
    ArtifactResolutionRequest request = new ArtifactResolutionRequest().setArtifact(artifact);
    ArtifactResolutionResult result = repositorySystem.resolve(request);
    // If not found, then assume it's a reactor dependency and therefore should be a bundle.
    if (result.getArtifacts().isEmpty()) {
      getLog()
          .info(
              "Artifact "
                  + artifact.toString()
                  + " not found in local repository, assuming reactor dependency."); //$NON-NLS-1$
      // //$NON-NLS-2$
      return true;
    }
    artifact = result.getArtifacts().iterator().next();
    if (!artifact.getFile().isFile()) {
      throw new Exception(
          "Resolved artifact is not a file: "
              + artifact.getFile().getAbsolutePath()); // $NON-NLS-1$
    }

    // Crack open the dependency JAR, read the manifest, check for osgi attributes.
    JarFile jf = null;
    try {
      jf = new JarFile(artifact.getFile());
      Manifest manifest = jf.getManifest();
      if (manifest == null) {
        getLog()
            .info(
                "Artifact "
                    + artifact.toString()
                    + " missing a manifest!  Assuming not a bundle."); //$NON-NLS-1$ //$NON-NLS-2$
        return false;
      }
      Attributes attributes = manifest.getMainAttributes();
      if (attributes != null) {
        String value = attributes.getValue("Bundle-SymbolicName"); // $NON-NLS-1$
        if (value != null && value.trim().length() > 0) {
          return true;
        }
      }
    } finally {
      jf.close();
    }

    return false;
  }
Exemplo n.º 5
0
 /**
  * Find unused artifacts.
  *
  * @param env Environment
  * @return Collection of unused artifacts
  */
 private static Collection<String> unused(final MavenEnvironment env) {
   final ProjectDependencyAnalysis analysis = DependenciesValidator.analyze(env);
   final Collection<String> unused = new LinkedList<String>();
   for (final Object obj : analysis.getUnusedDeclaredArtifacts()) {
     final Artifact artifact = (Artifact) obj;
     if (!artifact.getScope().equals(Artifact.SCOPE_COMPILE)) {
       continue;
     }
     unused.add(artifact.toString());
   }
   return unused;
 }
  Set<Artifact> getResolvedArtifactsFromUnresolvedDependencies(
      List<Dependency> unresolvedDependencies, boolean resolveTransitively)
      throws MojoExecutionException {
    final Set<Artifact> resolvedArtifacts = new HashSet<Artifact>();
    //    Artifact mojoArtifact = this.artifactFactory.createBuildArtifact(project.getGroupId(),
    // project.getArtifactId(), project.getVersion(), "pom");

    /*
     * Resolve each artifact.  This will get all transitive artifacts AND eliminate conflicts.
     */
    final Set<Artifact> unresolvedArtifacts;

    //    for (Dependency d : unresolvedDependencies)
    //      System.out.println("dependency: " + d.toString());

    try {
      unresolvedArtifacts =
          MavenMetadataSource.createArtifacts(
              this.artifactFactory, unresolvedDependencies, null, null, null);
      //      for (Artifact artifact : unresolvedArtifacts) {
      //        System.out.println("unresolved " + artifact.toString());
      //      }

      if (resolveTransitively) {
        ArtifactResolutionResult artifacts =
            artifactResolver.resolveTransitively(
                unresolvedArtifacts,
                project.getArtifact(),
                remoteRepositories,
                localRepository,
                artifactMetadataSource);
        resolvedArtifacts.addAll(artifacts.getArtifacts());
      } else {
        // resolve each artifact individually
        for (Artifact artifact : unresolvedArtifacts) {
          artifactResolver.resolve(artifact, remoteRepositories, localRepository);

          resolvedArtifacts.add(artifact);
        }
      }

    } catch (Exception e) {
      throw new MojoExecutionException("Unable to complete configuring the build settings", e);
    }

    for (Artifact artifact : resolvedArtifacts) {
      System.out.println("matched " + artifact.toString());
    }

    return resolvedArtifacts;
  }
Exemplo n.º 7
0
  /**
   * Allow the script to work with every JAR dependency of both the project and plugin, including
   * optional and provided dependencies. Runtime classpath elements are loaded first, so that legacy
   * behavior is not modified. Additional elements are added first in the order of project
   * artifacts, then in the order of plugin artifacts.
   */
  protected List getProjectClasspathElements() throws DependencyResolutionRequiredException {
    Set results = new LinkedHashSet();

    for (Iterator i = project.getRuntimeClasspathElements().iterator(); i.hasNext(); ) {
      String cpe = (String) i.next();
      try {
        results.add(new File(cpe).getCanonicalPath());
      } catch (IOException e) {
        throw new RuntimeException("Classpath element not found: " + cpe, e);
      }
    }

    for (Iterator i = project.getArtifacts().iterator(); i.hasNext(); ) {
      Artifact artifact = (Artifact) i.next();
      if (artifact.getType().equals("jar") && artifact.getClassifier() == null) {
        try {
          results.add(artifact.getFile().getCanonicalPath());
        } catch (IOException e) {
          throw new RuntimeException("Maven artifact file not found: " + artifact.toString(), e);
        }
      }
    }

    for (Iterator i = pluginArtifacts.iterator(); i.hasNext(); ) {
      Artifact artifact = (Artifact) i.next();
      if (artifact.getType().equals("jar") && artifact.getClassifier() == null) {
        try {
          results.add(artifact.getFile().getCanonicalPath());
        } catch (IOException e) {
          throw new RuntimeException(
              "Maven plugin-artifact file not found: " + artifact.toString(), e);
        }
      }
    }
    return new ArrayList(results);
  }
Exemplo n.º 8
0
  private Set<File> getSurefirePlugins(String testFramework) throws MojoExecutionException {
    Set<File> result = new LinkedHashSet<File>();

    String fragment;
    if (TestFramework.TEST_JUNIT.equals(testFramework)) {
      fragment = "org.sonatype.tycho.surefire.junit";
    } else if (TestFramework.TEST_JUNIT4.equals(testFramework)) {
      fragment = "org.sonatype.tycho.surefire.junit4";
    } else {
      throw new IllegalArgumentException("Unsupported test framework " + testFramework);
    }

    for (Artifact artifact : pluginArtifacts) {
      if ("org.sonatype.tycho".equals(artifact.getGroupId())) {
        if ("org.sonatype.tycho.surefire.osgibooter".equals(artifact.getArtifactId())
            || fragment.equals(artifact.getArtifactId())) {
          result.add(artifact.getFile());
        }
      }
    }

    if (result.size() != 2) {
      StringBuilder sb =
          new StringBuilder(
              "Unable to locate org.sonatype.tycho:org.sonatype.tycho.surefire.osgibooter and/or its fragments\n");
      sb.append("Test framework: " + testFramework);
      sb.append("All plugin artifacts: ");
      for (Artifact artifact : pluginArtifacts) {
        sb.append("\n\t").append(artifact.toString());
      }
      sb.append("\nMatched OSGi test booter artifacts: ");
      for (File file : result) {
        sb.append("\n\t").append(file.getAbsolutePath());
      }

      throw new MojoExecutionException(sb.toString());
    }

    return result;
  }