Пример #1
0
  public List<ArtifactResult> resolveArtifacts(
      RepositorySystemSession session, Collection<? extends ArtifactRequest> requests)
      throws ArtifactResolutionException {
    List<ArtifactResult> results = new ArrayList<ArtifactResult>();

    for (ArtifactRequest request : requests) {
      ArtifactResult result = new ArtifactResult(request);
      results.add(result);

      Artifact artifact = request.getArtifact();
      if ("maven-test".equals(artifact.getGroupId())) {
        String scope = artifact.getArtifactId().substring("scope-".length());

        try {
          artifact =
              artifact.setFile(
                  ProjectClasspathTest.getFileForClasspathResource(
                      ProjectClasspathTest.dir + "transitive-" + scope + "-dep.xml"));
          result.setArtifact(artifact);
        } catch (FileNotFoundException e) {
          throw new IllegalStateException("Missing test POM for " + artifact);
        }
      } else {
        result.addException(new ArtifactNotFoundException(artifact, null));
        throw new ArtifactResolutionException(results);
      }
    }

    return results;
  }
Пример #2
0
  @Override
  protected void merge(Metadata recessive) {
    Snapshot snapshot;
    String lastUpdated;

    if (metadata.getVersioning() == null) {
      DateFormat utcDateFormatter = new SimpleDateFormat("yyyyMMdd.HHmmss");
      utcDateFormatter.setCalendar(new GregorianCalendar());
      utcDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

      snapshot = new Snapshot();
      snapshot.setBuildNumber(getBuildNumber(recessive) + 1);
      snapshot.setTimestamp(utcDateFormatter.format(new Date()));

      Versioning versioning = new Versioning();
      versioning.setSnapshot(snapshot);
      versioning.setLastUpdated(snapshot.getTimestamp().replace(".", ""));
      lastUpdated = versioning.getLastUpdated();

      metadata.setVersioning(versioning);
    } else {
      snapshot = metadata.getVersioning().getSnapshot();
      lastUpdated = metadata.getVersioning().getLastUpdated();
    }

    for (Artifact artifact : artifacts) {
      String version = artifact.getVersion();

      if (version.endsWith(SNAPSHOT)) {
        String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
        version = version.substring(0, version.length() - SNAPSHOT.length()) + qualifier;
      }

      SnapshotVersion sv = new SnapshotVersion();
      sv.setClassifier(artifact.getClassifier());
      sv.setExtension(artifact.getExtension());
      sv.setVersion(version);
      sv.setUpdated(lastUpdated);

      versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
    }

    artifacts.clear();

    Versioning versioning = recessive.getVersioning();
    if (versioning != null) {
      for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
        String key = getKey(sv.getClassifier(), sv.getExtension());
        if (!versions.containsKey(key)) {
          versions.put(key, sv);
        }
      }
    }

    if (!legacyFormat) {
      metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
    }
  }
 private void assertExpectedArtifacts(
     String[] expectedAIDs, List<Dependency> managedDependencies) {
   HashSet<String> expected = new HashSet<String>(Arrays.asList(expectedAIDs));
   for (Dependency d : managedDependencies) {
     Artifact foundArtifact = d.getArtifact();
     if (foundArtifact != null) {
       expected.remove(foundArtifact.getArtifactId());
     }
   }
 }
  private Artifact unify(Artifact dependent, Artifact dependency) {
    for (Entry<String, String> unification : unified.entrySet()) {
      String groupFilter = unification.getKey();
      String version = unification.getValue();
      String artifactId = dependency.toString();

      if ("".equals(version)) {
        version = dependent.getVersion();
      }
      if (artifactId.startsWith(groupFilter) && (!"".equals(version))) {
        Artifact retVal =
            new DefaultArtifact(
                dependency.getGroupId(),
                dependency.getArtifactId(),
                dependency.getClassifier(),
                dependency.getExtension(),
                // The important one
                version);
        retVal.setProperties(dependency.getProperties());
        return retVal;
      }
    }

    return dependency;
  }
Пример #5
0
  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }

    if (obj instanceof DelegatingArtifact) {
      return delegate.equals(((DelegatingArtifact) obj).delegate);
    }

    return delegate.equals(obj);
  }
Пример #6
0
  private static boolean equals(
      org.eclipse.aether.artifact.Artifact a1, org.eclipse.aether.artifact.Artifact a2) {
    if (a1 == a2) {
      return true;
    }

    return a1.getArtifactId().equals(a2.getArtifactId())
        && a1.getGroupId().equals(a2.getGroupId())
        && a1.getVersion().equals(a2.getVersion())
        && a1.getExtension().equals(a2.getExtension())
        && a1.getClassifier().equals(a2.getClassifier());
  }
 @Override
 public void artifactDownloaded(RepositoryEvent event) {
   super.artifactDownloaded(event);
   Artifact artifact = event.getArtifact();
   String key = artifactAsString(artifact);
   long downloadTimeNanos = System.nanoTime() - startTimes.remove(key);
   double downloadTimeMs = TimeUnit.NANOSECONDS.toMillis(downloadTimeNanos);
   double downloadTimeSec = TimeUnit.NANOSECONDS.toSeconds(downloadTimeNanos);
   long size = artifact.getFile().length();
   double sizeK = (1 / 1024D) * size;
   double downloadRateKBytesPerSecond = sizeK / downloadTimeSec;
   info(
       "Downloaded %s (%d bytes) in %gms (%g kbytes/sec).",
       key, size, downloadTimeMs, downloadRateKBytesPerSecond);
 }
  @Override
  public ModelSource resolveModel(String groupId, String artifactId, String version)
      throws UnresolvableModelException {
    Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "", "pom", version);

    try {
      ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, null);
      pomArtifact = system.resolveArtifact(session, request).getArtifact();
    } catch (ArtifactResolutionException ex) {
      throw new UnresolvableModelException(ex.getMessage(), groupId, artifactId, version, ex);
    }

    File pomFile = pomArtifact.getFile();

    return new FileModelSource(pomFile);
  }
  @Before
  public void setup() throws IOException {
    artifact = new DefaultArtifact("gid", "aid", "jar", "ver");
    artifact = artifact.setFile(TestFileUtils.createTempFile("artifact".getBytes(), 1));
    metadata =
        new DefaultMetadata(
            "gid",
            "aid",
            "ver",
            "type",
            Nature.RELEASE_OR_SNAPSHOT,
            TestFileUtils.createTempFile("metadata".getBytes(), 1));

    session = TestUtils.newSession();
    localArtifactPath = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
    localMetadataPath = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);

    localArtifactFile = new File(session.getLocalRepository().getBasedir(), localArtifactPath);

    installer = new DefaultInstaller();
    installer.setFileProcessor(new TestFileProcessor());
    installer.setRepositoryEventDispatcher(new StubRepositoryEventDispatcher());
    installer.setSyncContextFactory(new StubSyncContextFactory());
    request = new InstallRequest();
    listener = new RecordingRepositoryListener();
    session.setRepositoryListener(listener);

    lrm = (TestLocalRepositoryManager) session.getLocalRepositoryManager();

    TestFileUtils.deleteFile(session.getLocalRepository().getBasedir());
  }
Пример #10
0
  @Test(expected = InstallationException.class)
  public void testNonExistentArtifactFile() throws InstallationException {
    InstallRequest request = new InstallRequest();
    request.addArtifact(artifact.setFile(new File("missing.txt")));

    installer.install(session, request);
  }
Пример #11
0
  @Test(expected = InstallationException.class)
  public void testNullArtifactFile() throws InstallationException {
    InstallRequest request = new InstallRequest();
    request.addArtifact(artifact.setFile(null));

    installer.install(session, request);
  }
Пример #12
0
 public Artifact setProperties(Map<String, String> properties) {
   Artifact artifact = delegate.setProperties(properties);
   if (artifact != delegate) {
     return newInstance(artifact);
   }
   return this;
 }
Пример #13
0
 public Artifact setVersion(String version) {
   Artifact artifact = delegate.setVersion(version);
   if (artifact != delegate) {
     return newInstance(artifact);
   }
   return this;
 }
Пример #14
0
 public Artifact setFile(File file) {
   Artifact artifact = delegate.setFile(file);
   if (artifact != delegate) {
     return newInstance(artifact);
   }
   return this;
 }
  public DependentLoaderImplementation findOverride(Artifact artifactId) {
    if (overridingLoaders.size() == 0) return null;
    String asString = artifactId.toString();
    for (Wildcards wild : overridingLoaders) {
      if (asString.startsWith(wild.what)) return wild.loader;
    }

    return null;
  }
Пример #16
0
 @Override
 public void packageResolveFailed(DependencyResolutionException exception) {
   clearProgressBar();
   Artifact artifact = exception.getResult().getRoot().getArtifact();
   try {
     reader.println(
         String.format(
             "Could not resolve dependencies for %s.%s version %s",
             artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()));
     reader.println("The following artifacts could not be located:");
     for (ArtifactResult result : exception.getResult().getArtifactResults()) {
       if (result.isMissing()) {
         reader.println("* " + result.getRequest().getArtifact());
       }
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Пример #17
0
  @Test(expected = InstallationException.class)
  public void testArtifactDestinationEqualsSource() throws Exception {
    String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
    File file = new File(session.getLocalRepository().getBasedir(), path);
    artifact = artifact.setFile(file);
    TestFileUtils.writeString(file, "test");

    request.addArtifact(artifact);
    installer.install(session, request);
  }
  private File findOverrideFile(Artifact artifactId) {
    if (overridingLoaders.size() == 0) return null;
    String asString = artifactId.toString();
    for (Wildcards wild : overridingLoaders) {

      if (wild.where.length > 0 && asString.startsWith(wild.what)) {
        return wild.where[0];
      }
    }

    return null;
  }
Пример #19
0
  public static Optional<File> resolveArtifact(
      String groupId,
      String artifactId,
      String version,
      Optional<String> type,
      Optional<String> classifier,
      Optional<List<Repository>> customRepositories) {
    Optional<File> result;

    LocalRepository localRepository =
        MavenSettingsUtil.getLocalRepository(MavenSettingsUtil.loadSettings());
    RepositorySystem system = AetherBootstrap.newRepositorySystem();
    RepositorySystemSession session =
        AetherBootstrap.newRepositorySystemSession(
            system, localRepository.getBasedir().getAbsolutePath());

    Artifact artifact =
        new DefaultArtifact(groupId, artifactId, classifier.orNull(), type.orNull(), version);
    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact(artifact);
    artifactRequest.setRepositories(
        MavenSettingsUtil.getRemoteRepositories(
            MavenSettingsUtil.loadSettings(), customRepositories.orNull()));

    try {
      ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);
      artifact = artifactResult.getArtifact();
      if (artifact != null) {
        result = Optional.fromNullable(artifact.getFile());
      } else {
        result = Optional.absent();
      }
    } catch (ArtifactResolutionException e) {
      // TODO add error handling -> maybe throw an exception that indicates the error or return an
      // Optional
      result = Optional.absent();
    }

    return result;
  }
  public void testResolveArtifacts() throws Exception {
    String[] artifactsCoords = {
      "org.springframework.boot:spring-boot:jar:0.5.0.BUILD-SNAPSHOT", // spring snapshot artifact
      "org.springframework:spring-jdbc:jar:4.0.0.RELEASE", // spring release artifact
      "commons-logging:commons-logging:jar:1.1.3" // general purpose (from maven central)
    };
    List<Artifact> artifacts = new ArrayList<Artifact>();
    for (String c : artifactsCoords) {
      artifacts.add(new DefaultArtifact(c));
    }
    for (Artifact artifact : artifacts) {
      assertNull(artifact.getFile());
    }

    boolean ignoreUnresolved = false;
    artifacts = helper.resolve(artifacts, ignoreUnresolved);

    assertEquals(artifactsCoords.length, artifacts.size());

    for (Artifact artifact : artifacts) {
      assertNotNull("Artifact not resolved: '" + artifact + "'", artifact.getFile());
      assertTrue("Artifact not a file: '" + artifact + "'", artifact.getFile().isFile());
      assertTrue(
          "Artifact not a jar file: '" + artifact + "'",
          artifact.getFile().toString().endsWith(".jar"));
    }
  }
Пример #21
0
  @Test
  public void testSetArtifactTimestamps() throws InstallationException {
    artifact.getFile().setLastModified(artifact.getFile().lastModified() - 60000);

    request.addArtifact(artifact);

    installer.install(session, request);

    assertEquals(
        "artifact timestamp was not set to src file",
        artifact.getFile().lastModified(),
        localArtifactFile.lastModified());

    request = new InstallRequest();

    request.addArtifact(artifact);

    artifact.getFile().setLastModified(artifact.getFile().lastModified() - 60000);

    installer.install(session, request);

    assertEquals(
        "artifact timestamp was not set to src file",
        artifact.getFile().lastModified(),
        localArtifactFile.lastModified());
  }
Пример #22
0
  @Override
  public ClassLoader getClassLoader(KieModule kmodule) {
    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    if (parent == null) {
      parent = ClassLoader.getSystemClassLoader();
    }
    if (parent == null) {
      parent = MavenClassLoaderResolver.class.getClassLoader();
    }

    InternalKieModule internalKModule = (InternalKieModule) kmodule;
    Collection<ReleaseId> jarDependencies = internalKModule.getJarDependencies();

    if (jarDependencies.isEmpty()) {
      return parent;
    }

    ArtifactResolver resolver = ArtifactResolver.getResolverFor(internalKModule.getPomModel());
    List<URL> urls = new ArrayList<URL>();
    List<ReleaseId> unresolvedDeps = new ArrayList<ReleaseId>();

    for (ReleaseId rid : jarDependencies) {
      try {
        Artifact artifact = resolver.resolveArtifact(rid);
        if (artifact != null) {
          File jar = artifact.getFile();
          urls.add(jar.toURI().toURL());
        } else {
          logger.error("Dependency artifact not found for: " + rid);
          unresolvedDeps.add(rid);
        }
      } catch (MalformedURLException e) {
        throw new RuntimeException(e);
      }
    }

    internalKModule.setUnresolvedDependencies(unresolvedDeps);
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
  }
Пример #23
0
  URL createMavenGavURL(String artifactGav) throws MalformedURLException {
    Artifact artifact = new DefaultArtifact(artifactGav);
    if (artifact.getVersion() == null) {
      throw new IllegalArgumentException("Null version");
    }

    VersionScheme versionScheme = new GenericVersionScheme();
    try {
      versionScheme.parseVersion(artifact.getVersion());
    } catch (InvalidVersionSpecificationException e) {
      throw new IllegalArgumentException(e);
    }

    try {
      versionScheme.parseVersionRange(artifact.getVersion());
      throw new IllegalArgumentException(
          artifact.getVersion() + " is a version range. A specific version is needed");
    } catch (InvalidVersionSpecificationException expected) {

    }

    RepositorySystemSession session = newRepositorySystemSession();

    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact(artifact);
    for (RemoteRepository remoteRepo : remoteRepositories) {
      artifactRequest.addRepository(remoteRepo);
    }

    ArtifactResult artifactResult;
    try {
      artifactResult = REPOSITORY_SYSTEM.resolveArtifact(session, artifactRequest);
    } catch (ArtifactResolutionException e) {
      throw new RuntimeException(e);
    }

    File file = artifactResult.getArtifact().getFile().getAbsoluteFile();
    return file.toURI().toURL();
  }
Пример #24
0
 public File getFile() {
   return delegate.getFile();
 }
 private String artifactAsString(Artifact artifact) {
   return Settings.GAV_JOINER.join(
       artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
 }
Пример #26
0
 public Map<String, String> getProperties() {
   return delegate.getProperties();
 }
Пример #27
0
 public String getProperty(String key, String defaultValue) {
   return delegate.getProperty(key, defaultValue);
 }
Пример #28
0
 public boolean isSnapshot() {
   return delegate.isSnapshot();
 }
Пример #29
0
 public String getExtension() {
   return delegate.getExtension();
 }
Пример #30
0
 public String getClassifier() {
   return delegate.getClassifier();
 }