Ejemplo n.º 1
0
  /**
   * copy from MavenUtil but here we have to ignore localRepo path and setting as thoses paths comes
   * from the remote node and can not exist in master see
   * http://issues.jenkins-ci.org/browse/JENKINS-8711
   */
  private MavenEmbedder createEmbedder(TaskListener listener, AbstractBuild<?, ?> build)
      throws MavenEmbedderException, IOException, InterruptedException {
    MavenInstallation m = null;
    File settingsLoc = null, remoteGlobalSettingsFromConfig = null;
    String profiles = null;
    Properties systemProperties = null;
    String privateRepository = null;
    FilePath remoteSettingsFromConfig = null;

    File tmpSettings = File.createTempFile("jenkins", "temp-settings.xml");
    try {
      AbstractProject project = build.getProject();

      if (project instanceof MavenModuleSet) {
        MavenModuleSet mavenModuleSet = ((MavenModuleSet) project);
        profiles = mavenModuleSet.getProfiles();
        systemProperties = mavenModuleSet.getMavenProperties();

        // olamy see
        // we have to take about the settings use for the project
        // order tru configuration
        // TODO maybe in goals with -s,--settings last wins but not done in during pom parsing
        // or -Dmaven.repo.local
        // if not we must get ~/.m2/settings.xml then $M2_HOME/conf/settings.xml

        // TODO check if the remoteSettings has a localRepository configured and disabled it

        String settingsConfigId = mavenModuleSet.getSettingConfigId();
        String altSettingsPath = null;

        if (!StringUtils.isBlank(settingsConfigId)) {
          Config config =
              SettingsProviderUtils.findConfig(settingsConfigId, MavenSettingsProvider.class);
          if (config == null) {
            listener
                .getLogger()
                .println(
                    " your Apache Maven build is setup to use a config with id "
                        + settingsConfigId
                        + " but cannot find the config");
          } else {
            listener
                .getLogger()
                .println("redeploy publisher using settings config with name " + config.name);
            String settingsContent = config.content;
            if (config.content != null) {
              remoteSettingsFromConfig =
                  SettingsProviderUtils.copyConfigContentToFilePath(config, build.getWorkspace());
              altSettingsPath = remoteSettingsFromConfig.getRemote();
            }
          }
        }

        if (mavenModuleSet.getAlternateSettings() != null) {
          altSettingsPath = mavenModuleSet.getAlternateSettings();
        }

        String globalSettingsConfigId = mavenModuleSet.getGlobalSettingConfigId();
        if (!StringUtils.isBlank(globalSettingsConfigId)) {
          Config config =
              SettingsProviderUtils.findConfig(
                  globalSettingsConfigId, GlobalMavenSettingsProvider.class);
          if (config == null) {
            listener
                .getLogger()
                .println(
                    " your Apache Maven build is setup to use a global settings config with id "
                        + globalSettingsConfigId
                        + " but cannot find the config");
          } else {
            listener
                .getLogger()
                .println(
                    "redeploy publisher using global settings config with name " + config.name);
            if (config.content != null) {
              remoteGlobalSettingsFromConfig =
                  SettingsProviderUtils.copyConfigContentToFile(config);
            }
          }
        }
        Node buildNode = build.getBuiltOn();

        if (buildNode == null) {
          // assume that build was made on master
          buildNode = Jenkins.getInstance();
        }

        if (StringUtils.isBlank(altSettingsPath)) {
          // get userHome from the node where job has been executed
          String remoteUserHome = build.getWorkspace().act(new GetUserHome());
          altSettingsPath = remoteUserHome + "/.m2/settings.xml";
        }

        // we copy this file in the master in a  temporary file
        FilePath filePath = new FilePath(tmpSettings);
        FilePath remoteSettings = build.getWorkspace().child(altSettingsPath);
        if (!remoteSettings.exists()) {
          // JENKINS-9084 we finally use $M2_HOME/conf/settings.xml as maven do

          String mavenHome =
              ((MavenModuleSet) project).getMaven().forNode(buildNode, listener).getHome();
          String settingsPath = mavenHome + "/conf/settings.xml";
          remoteSettings = build.getWorkspace().child(settingsPath);
        }
        listener
            .getLogger()
            .println(
                "Maven RedeployPublished use remote "
                    + (buildNode != null ? buildNode.getNodeName() : "local")
                    + " maven settings from : "
                    + remoteSettings.getRemote());
        remoteSettings.copyTo(filePath);
        settingsLoc = tmpSettings;
      }

      MavenEmbedderRequest mavenEmbedderRequest =
          new MavenEmbedderRequest(
              listener,
              m != null ? m.getHomeDir() : null,
              profiles,
              systemProperties,
              privateRepository,
              settingsLoc);

      if (remoteGlobalSettingsFromConfig != null) {
        mavenEmbedderRequest.setGlobalSettings(remoteGlobalSettingsFromConfig);
      }

      mavenEmbedderRequest.setTransferListener(
          new ConsoleMavenTransferListener(listener.getLogger()));

      return MavenUtil.createEmbedder(mavenEmbedderRequest);
    } finally {
      if (tmpSettings != null) {
        tmpSettings.delete();
      }
      if (remoteSettingsFromConfig != null) {
        remoteSettingsFromConfig.delete();
      }
      FileUtils.deleteQuietly(remoteGlobalSettingsFromConfig);
    }
  }