@Test
  public void autoDiscoverWithOneCompleteLocationFromMirrorWithEncryptedPassword()
      throws NexusDiscoveryException {
    Settings settings = new Settings();

    String url = "http://nexus.somewhere.com/";

    testClientManager.testUrl = url;
    testClientManager.testUser = "******";
    testClientManager.testPassword = "******";

    Mirror mirror = new Mirror();
    mirror.setId("some-mirror");
    mirror.setName("A Mirror");
    mirror.setUrl(url);

    settings.addMirror(mirror);

    Server server = new Server();
    server.setId("some-mirror");
    server.setUsername("user");
    server.setPassword(encryptedPassword);

    settings.addServer(server);

    Model model = new Model();
    model.setModelVersion("4.0.0");
    model.setGroupId("group.id");
    model.setArtifactId("artifact-id");
    model.setVersion("1");

    MavenProject project = new MavenProject(model);

    discovery.discover(settings, project, "blah", true);
  }
  public void update(Settings settings, File settingsFile) throws ManipulationException {
    try {
      Settings defaultSettings = new Settings();

      if (settingsFile.exists()) {
        DefaultSettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
        settingsRequest.setGlobalSettingsFile(settingsFile);
        defaultSettings = settingsBuilder.build(settingsRequest).getEffectiveSettings();
      }

      for (Profile profile : settings.getProfiles()) {
        Iterator<Profile> i = defaultSettings.getProfiles().iterator();
        while (i.hasNext()) {
          if (i.next().getId().equals(profile.getId())) {
            i.remove();
          }
        }
        defaultSettings.addProfile(profile);
      }
      for (String activeProfile : settings.getActiveProfiles()) {
        Iterator<String> i = defaultSettings.getActiveProfiles().iterator();
        while (i.hasNext()) {
          if (i.next().equals(activeProfile)) {
            i.remove();
          }
        }
        defaultSettings.addActiveProfile(activeProfile);
      }
      for (Mirror mirror : settings.getMirrors()) {
        defaultSettings.addMirror(mirror);
      }
      for (Proxy proxy : settings.getProxies()) {
        defaultSettings.addProxy(proxy);
      }
      for (Server server : settings.getServers()) {
        defaultSettings.addServer(server);
      }
      for (String pluginGroup : settings.getPluginGroups()) {
        defaultSettings.addPluginGroup(pluginGroup);
      }
      if (settings.getLocalRepository() != null) {
        defaultSettings.setLocalRepository(settings.getLocalRepository());
      }

      write(defaultSettings, settingsFile);
    } catch (SettingsBuildingException e) {
      throw new ManipulationException(
          "Failed to build existing settings.xml for repo removal backup.",
          e,
          settingsFile,
          e.getMessage());
    }
  }
  @Test
  public void autoDiscoverWithOneCompleteLocationFromSnapshotPOMDistMgmt()
      throws NexusDiscoveryException {
    Settings settings = new Settings();

    String url = "http://nexus.somewhere.com/";
    String id = "some-mirror";
    String user = "******";
    String password = "******";

    testClientManager.testUrl = url;
    testClientManager.testUser = user;
    testClientManager.testPassword = password;

    Server server = new Server();
    server.setId(id);
    server.setUsername(user);
    server.setPassword(password);

    settings.addServer(server);

    Model model = new Model();
    model.setModelVersion("4.0.0");
    model.setGroupId("group.id");
    model.setArtifactId("artifact-id");
    model.setVersion("1-SNAPSHOT");

    DistributionManagement dm = new DistributionManagement();

    DeploymentRepository repo = new DeploymentRepository();
    repo.setId(id);
    repo.setUrl(url);

    dm.setSnapshotRepository(repo);

    model.setDistributionManagement(dm);

    MavenProject project = new MavenProject(model);

    project.setArtifact(factory.create(project));

    discovery.discover(settings, project, "blah", true);
  }
  @Test
  public void autoDiscoverWithOneCompleteLocationFromSettingsProfileRepoWithConfirmation()
      throws NexusDiscoveryException {
    Settings settings = new Settings();

    String url = "http://nexus.somewhere.com/";
    String id = "some-mirror";
    String user = "******";
    String password = "******";

    testClientManager.testUrl = url;
    testClientManager.testUser = user;
    testClientManager.testPassword = password;

    Server server = new Server();
    server.setId(id);
    server.setUsername(user);
    server.setPassword(password);

    settings.addServer(server);

    org.apache.maven.settings.Repository repo = new org.apache.maven.settings.Repository();
    repo.setId(id);
    repo.setUrl(url);
    repo.setName("Profile Repository");

    org.apache.maven.settings.Profile profile = new org.apache.maven.settings.Profile();
    profile.addRepository(repo);

    settings.addProfile(profile);

    Model model = new Model();
    model.setModelVersion("4.0.0");
    model.setGroupId("group.id");
    model.setArtifactId("artifact-id");
    model.setVersion("1");

    MavenProject project = new MavenProject(model);

    prompter.addExpectation("Use this connection?", "y");
    discovery.discover(settings, project, "blah", false);
  }
  @Test
  public void autoDiscoverWithOneCompleteLocationFromPOMRepo() throws NexusDiscoveryException {
    Settings settings = new Settings();

    String url = "http://nexus.somewhere.com/";
    String id = "some-mirror";
    String user = "******";
    String password = "******";

    testClientManager.testUrl = url;
    testClientManager.testUser = user;
    testClientManager.testPassword = password;

    Server server = new Server();
    server.setId(id);
    server.setUsername(user);
    server.setPassword(password);

    settings.addServer(server);

    Model model = new Model();
    model.setModelVersion("4.0.0");
    model.setGroupId("group.id");
    model.setArtifactId("artifact-id");
    model.setVersion("1");

    Repository repo = new Repository();
    repo.setId(id);
    repo.setUrl(url);

    model.addRepository(repo);

    MavenProject project = new MavenProject(model);

    discovery.discover(settings, project, "blah", true);
  }
Esempio n. 6
0
  @Override
  public void execute() throws MojoExecutionException, MojoFailureException {
    if (isIgnoreProject()) return;

    try {
      boolean newUserAdded = false;

      fabricServer = mavenSettings.getServer(serverId);

      if (Strings.isNullOrBlank(consoleUrl)) {
        consoleUrl = DEFAULT_CONSOLE_URL;
      }

      // we may have username and password from consoleUrl
      String jolokiaUsername = null;
      String jolokiaPassword = null;
      try {
        URL url = new URL(consoleUrl);
        String s = url.getUserInfo();
        if (Strings.isNotBlank(s) && s.indexOf(':') > 0) {
          int idx = s.indexOf(':');
          jolokiaUsername = s.substring(0, idx);
          jolokiaPassword = s.substring(idx + 1);
        }
      } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Option consoleUrl is invalid due " + e.getMessage());
      }

      // jolokia url overrides username/password configured in maven settings
      if (jolokiaUsername != null) {
        if (fabricServer == null) {
          fabricServer = new Server();
        }
        getLog()
            .info(
                "Using username: "******" and password from provided consoleUrl option");
        fabricServer.setUsername(jolokiaUsername);
        fabricServer.setPassword(jolokiaPassword);
      }

      if (fabricServer == null) {
        boolean create = false;
        if (mavenSettings.isInteractiveMode() && mavenSettingsWriter != null) {
          System.out.println("Maven settings file: " + mavenSettingsFile.getAbsolutePath());
          System.out.println();
          System.out.println();
          System.out.println(
              "There is no <server> section in your ~/.m2/settings.xml file for the server id: "
                  + serverId);
          System.out.println();
          System.out.println(
              "You can enter the username/password now and have the settings.xml updated or you can do this by hand if you prefer.");
          System.out.println();
          while (true) {
            String value =
                readInput("Would you like to update the settings.xml file now? (y/n): ")
                    .toLowerCase();
            if (value.startsWith("n")) {
              System.out.println();
              System.out.println();
              break;
            } else if (value.startsWith("y")) {
              create = true;
              break;
            }
          }
          if (create) {
            System.out.println("Please let us know the login details for this server: " + serverId);
            System.out.println();
            String userName = readInput("Username: "******"Password: "******"Repeat Password: "******"Passwords do not match, please try again.");
              password = readPassword("Password: "******"Repeat Password: "******".backup-" + counter++ + ".xml");
                if (!backupFile.exists()) {
                  System.out.println(
                      "Copied original: "
                          + mavenSettingsFile.getAbsolutePath()
                          + " to: "
                          + backupFile.getAbsolutePath());
                  Files.copy(mavenSettingsFile, backupFile);
                  break;
                }
              }
            }
            Map<String, Object> config = new HashMap<String, Object>();
            mavenSettingsWriter.write(mavenSettingsFile, config, mavenSettings);
            System.out.println("Updated settings file: " + mavenSettingsFile.getAbsolutePath());
            System.out.println();

            newUserAdded = true;
          }
        }
      }
      if (fabricServer == null) {
        String message =
            "No <server> element can be found in ~/.m2/settings.xml for the server <id>"
                + serverId
                + "</id> so we cannot connect to fabric8!\n\n"
                + "Please add the following to your ~/.m2/settings.xml file (using the correct user/password values):\n\n"
                + "<servers>\n"
                + "  <server>\n"
                + "    <id>"
                + serverId
                + "</id>\n"
                + "    <username>admin</username>\n"
                + "    <password>admin</password>\n"
                + "  </server>\n"
                + "</servers>\n";
        getLog().error(message);
        throw new MojoExecutionException(message);
      }

      if (!isIgnoreProject()) {
        uploadAppZip(newUserAdded);
      } else {
        getLog().info("Ignoring this project so not uploading the App Zip");
      }
    } catch (MojoExecutionException e) {
      throw e;
    } catch (Exception e) {
      throw new MojoExecutionException("Error executing", e);
    }
  }