@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());
  }
  private RepositorySystemSession newSession(RepositorySystem system) {
    DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();

    LocalRepository localRepo = new LocalRepository(localRepoPath);
    session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));

    return session;
  }
 @Test
 public void testFailingEventsMetadataExistsAsDir() {
   String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
   assertTrue(
       "failed to setup test: could not create " + path,
       new File(session.getLocalRepository().getBasedir(), path).mkdirs());
   checkFailedEvents("target exists as dir", metadata);
 }
  @Test(expected = InstallationException.class)
  public void testMetadataDestinationEqualsSource() throws Exception {
    String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
    File file = new File(session.getLocalRepository().getBasedir(), path);
    metadata = metadata.setFile(file);
    TestFileUtils.writeString(file, "test");

    request.addMetadata(metadata);
    installer.install(session, request);
  }
  @Test(expected = InstallationException.class)
  public void testMetadataExistsAsDir() throws InstallationException {
    String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
    assertTrue(
        "failed to setup test: could not create " + path,
        new File(session.getLocalRepository().getBasedir(), path).mkdirs());

    request.addMetadata(metadata);
    installer.install(session, request);
  }
  @Test(expected = InstallationException.class)
  public void testArtifactExistsAsDir() throws InstallationException {
    String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
    File file = new File(session.getLocalRepository().getBasedir(), path);
    assertFalse(file.getAbsolutePath() + " is a file, not directory", file.isFile());
    assertFalse(file.getAbsolutePath() + " already exists", file.exists());
    assertTrue(
        "failed to setup test: could not create " + file.getAbsolutePath(),
        file.mkdirs() || file.isDirectory());

    request.addArtifact(artifact);
    installer.install(session, request);
  }
  /**
   * Creates a new Maven-like repository system session by initializing the session with values
   * typical for Maven-based resolution. In more detail, this method configures settings relevant
   * for the processing of dependency graphs, most other settings remain at their generic default
   * value. Use the various setters to further configure the session with authentication, mirror,
   * proxy and other information required for your environment.
   *
   * @return The new repository system session, never {@code null}.
   */
  public static DefaultRepositorySystemSession newSession() {
    DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();

    DependencyTraverser depTraverser = new FatArtifactTraverser();
    session.setDependencyTraverser(depTraverser);

    DependencyManager depManager = new ClassicDependencyManager();
    session.setDependencyManager(depManager);

    DependencySelector depFilter =
        new AndDependencySelector(
            new ScopeDependencySelector("test", "provided"),
            new OptionalDependencySelector(),
            new ExclusionDependencySelector());
    session.setDependencySelector(depFilter);

    DependencyGraphTransformer transformer =
        new ConflictResolver(
            new NearestVersionSelector(), new JavaScopeSelector(),
            new SimpleOptionalitySelector(), new JavaScopeDeriver());

    session.setDependencyGraphTransformer(
        new ChainedDependencyGraphTransformer(transformer, new JavaDependencyContextRefiner()));

    DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
    stereotypes.add(new DefaultArtifactType("pom"));
    stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java"));
    stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java"));
    stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java"));
    stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java"));
    stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java"));
    stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java"));
    stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false));
    stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true));
    stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true));
    stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true));
    stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true));
    session.setArtifactTypeRegistry(stereotypes);

    session.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, true));

    final Properties systemProperties = new Properties();

    // MNG-5670 guard against ConcurrentModificationException
    // MNG-6053 guard against key without value
    Properties sysProp = System.getProperties();
    synchronized (sysProp) {
      systemProperties.putAll(sysProp);
    }

    session.setSystemProperties(systemProperties);
    session.setConfigProperties(systemProperties);

    return session;
  }
 /**
  * Attempts to resolve an {@link Artifact} to a {@link File}.
  *
  * @param artifact to resolve
  * @return a {@link File} to the resolved artifact, never <code>null</code>.
  * @throws MojoExecutionException if the artifact could not be resolved.
  */
 protected File resolveArtifactToFile(Artifact artifact) throws MojoExecutionException {
   Artifact resolvedArtifact = null;
   if (container.hasComponent("org.sonatype.aether.RepositorySystem")) {
     org.sonatype.aether.RepositorySystem system;
     try {
       system = container.lookup(org.sonatype.aether.RepositorySystem.class);
     } catch (ComponentLookupException e) {
       throw new MojoExecutionException(e.getMessage());
     }
     org.sonatype.aether.RepositorySystemSession repositorySession =
         session.getRepositorySession();
     List<org.sonatype.aether.repository.RemoteRepository> remoteProjectRepositories =
         project.getRemoteProjectRepositories();
     resolvedArtifact =
         AetherHelper.resolveArtifact(
             artifact, system, repositorySession, remoteProjectRepositories);
   } else if (container.hasComponent("org.eclipse.aether.RepositorySystem")) {
     org.eclipse.aether.RepositorySystem system;
     try {
       system = container.lookup(org.eclipse.aether.RepositorySystem.class);
     } catch (ComponentLookupException e) {
       throw new MojoExecutionException(e.getMessage());
     }
     org.eclipse.aether.DefaultRepositorySystemSession session =
         MavenRepositorySystemUtils.newSession();
     org.eclipse.aether.repository.LocalRepository localRepo =
         new org.eclipse.aether.repository.LocalRepository(Utility.getLocalRepoPath());
     session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
     List<?> repositories = project.getRemoteProjectRepositories();
     resolvedArtifact =
         EclipseAetherHelper.resolveArtifact(artifact, system, session, repositories);
   }
   final File jar = resolvedArtifact.getFile();
   if (jar == null) {
     throw new MojoExecutionException(
         "Could not resolve artifact "
             + artifact.getId()
             + ". Please install it with \"mvn install:install-file ...\" or deploy it to a repository "
             + "with \"mvn deploy:deploy-file ...\"");
   }
   return jar;
 }
示例#9
0
  private RepositorySystemSession newRepositorySystemSession() {
    if (this.session != null) {
      return this.session;
    }
    DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();

    LocalRepository localRepo = new LocalRepository(mavenSettings.getLocalRepository().toString());
    session.setLocalRepositoryManager(
        REPOSITORY_SYSTEM.newLocalRepositoryManager(session, localRepo));

    // Copy these from the aether demo if they are nice to have
    session.setTransferListener(new ConsoleTransferListener());
    session.setRepositoryListener(new ConsoleRepositoryListener());
    session.setTransferListener(
        new AbstractTransferListener() {
          @Override
          public void transferFailed(TransferEvent event) {
            super.transferFailed(event);
          }
        });
    this.session = session;
    return session;
  }
  @Test
  public void testSuccessfulInstall()
      throws InstallationException, UnsupportedEncodingException, IOException {
    File artifactFile =
        new File(
            session.getLocalRepositoryManager().getRepository().getBasedir(), localArtifactPath);
    File metadataFile =
        new File(
            session.getLocalRepositoryManager().getRepository().getBasedir(), localMetadataPath);

    artifactFile.delete();
    metadataFile.delete();

    request.addArtifact(artifact);
    request.addMetadata(metadata);

    InstallResult result = installer.install(session, request);

    assertTrue(artifactFile.exists());
    assertEquals("artifact", TestFileUtils.readString(artifactFile));

    assertTrue(metadataFile.exists());
    assertEquals("metadata", TestFileUtils.readString(metadataFile));

    assertEquals(result.getRequest(), request);

    assertEquals(result.getArtifacts().size(), 1);
    assertTrue(result.getArtifacts().contains(artifact));

    assertEquals(result.getMetadata().size(), 1);
    assertTrue(result.getMetadata().contains(metadata));

    assertEquals(1, lrm.getMetadataRegistration().size());
    assertTrue(lrm.getMetadataRegistration().contains(metadata));
    assertEquals(1, lrm.getArtifactRegistration().size());
    assertTrue(lrm.getArtifactRegistration().contains(artifact));
  }
示例#11
0
 /**
  * Creates a new repository session whose local repository manager is initialized with an instance
  * of {@link TestLocalRepositoryManager}.
  */
 public static DefaultRepositorySystemSession newSession() {
   DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
   session.setLocalRepositoryManager(new TestLocalRepositoryManager());
   return session;
 }
 public SessionData setData(SessionData data) {
   DefaultRepositorySystemSession session = getSession();
   SessionData origSessionData = session.getData();
   session.setData(data);
   return origSessionData;
 }
 public TransferListener setTransferListener(TransferListener transferListener) {
   DefaultRepositorySystemSession session = getSession();
   TransferListener origTransferListener = session.getTransferListener();
   session.setTransferListener(transferListener);
   return origTransferListener;
 }
 private void resetListeners() {
   final DefaultRepositorySystemSession session = session();
   session.setRepositoryListener(this.repositoryListener);
   session.setTransferListener(this.transferListener);
 }
 @After
 public void teardown() throws Exception {
   TestFileUtils.deleteFile(session.getLocalRepository().getBasedir());
 }