public InputStream getInputStream() throws CoreException {
    if (filePath != null) {
      File file = new File(filePath);
      if (file.isFile()) {
        IMaven maven = MavenPlugin.getDefault().getMaven();
        Settings settings = maven.buildSettings(null, filePath);

        List<Server> servers = settings.getServers();
        if (servers != null) {
          for (Server server : servers) {
            server.setUsername(obfuscate(server.getUsername()));
            server.setPassword(obfuscate(server.getPassword()));
            server.setPassphrase(obfuscate(server.getPassphrase()));
          }
        }

        List<Proxy> proxies = settings.getProxies();
        if (proxies != null) {
          for (Proxy proxy : proxies) {
            proxy.setUsername(obfuscate(proxy.getUsername()));
            proxy.setPassword(obfuscate(proxy.getPassword()));
          }
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 4);
        maven.writeSettings(settings, baos);
        return new ByteArrayInputStream(baos.toByteArray());
      }
    }
    return null;
  }
 public ClassRealm getPluginClassRealm(MavenSession session, MojoExecution mojoExecution)
     throws CoreException {
   IMaven mvn = MavenPlugin.getMaven();
   // call for side effect of ensuring that the realm is set in the
   // descriptor.
   mvn.getConfiguredMojo(session, mojoExecution, Mojo.class);
   MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
   return mojoDescriptor.getPluginDescriptor().getClassRealm();
 }
 public static String[] getModifiedFiles(
     MavenSession mavenSession,
     MojoExecution mojoExecution,
     IMaven maven,
     BuildContext buildContext,
     String sourceParam,
     String includesParam,
     String excludesParam)
     throws Exception {
   return getModifiedFiles(
       buildContext,
       maven.getMojoParameterValue(mavenSession, mojoExecution, sourceParam, File.class),
       maven.getMojoParameterValue(mavenSession, mojoExecution, includesParam, String[].class),
       maven.getMojoParameterValue(mavenSession, mojoExecution, excludesParam, String[].class));
 }
Example #4
0
 /**
  * converts an InputLocation to a file path on the local disk, null if not available. still the
  * input source's model value can be used further..
  *
  * @param location
  * @return
  */
 public static File fileForInputLocation(InputLocation location, MavenProject origin) {
   InputSource source = location.getSource();
   if (source != null) {
     // MNGECLIPSE-2539 apparently if maven can't resolve the model from local storage,
     // the location will be empty. not only applicable to local repo models but
     // apparently also to models in workspace not reachable by relativePath
     String loc = source.getLocation();
     File file = null;
     if (loc != null) {
       file = new File(loc);
     } else {
       // try to find pom by coordinates..
       String modelId = source.getModelId();
       if (origin.getModel().getId().equals(modelId) && origin.getFile() != null) {
         return origin.getFile();
       }
       String[] splitStrings = modelId.split(":");
       assert splitStrings.length == 3;
       IMavenProjectFacade facade =
           MavenPlugin.getMavenProjectRegistry()
               .getMavenProject(splitStrings[0], splitStrings[1], splitStrings[2]);
       if (facade != null) {
         file = facade.getPomFile();
       } else {
         // if not in the workspace, try looking into the local repository.
         IMaven maven = MavenPlugin.getMaven();
         try {
           String path =
               maven.getArtifactPath(
                   maven.getLocalRepository(),
                   splitStrings[0],
                   splitStrings[1],
                   splitStrings[2],
                   "pom",
                   null);
           if (path != null) {
             file = new File(maven.getLocalRepositoryPath(), path);
           }
         } catch (CoreException e) {
           log.error("Failed to calculate local repository path of artifact", e);
         }
       }
     }
     return file;
   }
   return null;
 }
  /**
   * {@inheritDoc}
   *
   * @see org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant#build(int,
   *     org.eclipse.core.runtime.IProgressMonitor)
   */
  @Override
  public Set<IProject> build(int kind, IProgressMonitor monitor) throws Exception {
    final IMaven maven = MavenPlugin.getMaven();
    final BuildContext buildContext = getBuildContext();
    final MavenProject project = getMavenProjectFacade().getMavenProject();
    final MojoExecution mojoExecution = getMojoExecution();

    // execute mojo
    final Set<IProject> result = super.build(kind, monitor);

    // tell m2e builder to refresh generated files
    File generated =
        maven.getMojoParameterValue(project, mojoExecution, "outputDirectory", File.class, monitor);
    if (generated != null) {
      buildContext.refresh(generated);
    }

    return result;
  }
  public IStatus validateProjectLocation(String projectName, IPath path) {
    IStatus retval = Status.OK_STATUS;
    // if the path is a folder and it has a pom.xml that is a package type of 'pom' then this is a
    // valid location

    final File dir = path.toFile();

    if (dir.exists()) {
      final File pomFile = path.append(IMavenConstants.POM_FILE_NAME).toFile();

      if (pomFile.exists()) {
        final IMaven maven = MavenPlugin.getMaven();

        try {
          final Model result = maven.readModel(pomFile);

          if (!"pom".equals(result.getPackaging())) {
            retval =
                LiferayMavenCore.createErrorStatus(
                    "\"" + pomFile.getParent() + "\" contains a non-parent maven project.");
          } else {
            final IPath newProjectPath = path.append(projectName);

            retval = validateProjectLocation(projectName, newProjectPath);
          }
        } catch (CoreException e) {
          retval = LiferayMavenCore.createErrorStatus("Invalid project location.", e);
          LiferayMavenCore.log(retval);
        }
      } else {
        final File[] files = dir.listFiles();

        if (files.length > 0) {
          retval =
              LiferayMavenCore.createErrorStatus("Project location is not empty or a parent pom.");
        }
      }
    }

    return retval;
  }
Example #7
0
  /**
   * @param goals
   * @param serverProperties
   * @param module
   * @param monitor
   * @return
   * @throws CoreException
   */
  public static boolean runBuild(
      List<String> goals, Properties serverProperties, IModule module, IProgressMonitor monitor)
      throws CoreException {
    IMaven maven = MavenPlugin.getMaven();
    IMavenExecutionContext executionContext = maven.createExecutionContext();
    MavenExecutionRequest executionRequest = executionContext.getExecutionRequest();
    executionRequest.setPom(getModelFile(module));
    if (serverProperties != null && serverProperties.isEmpty() == false) {
      Server fabric8Server = new Server();
      fabric8Server.setId(serverProperties.getProperty(SERVER_ID));
      fabric8Server.setUsername(serverProperties.getProperty(SERVER_USER));
      fabric8Server.setPassword(serverProperties.getProperty(SERVER_PASSWORD));
      executionRequest.addServer(fabric8Server);
    }
    executionRequest.setGoals(goals);

    MavenExecutionResult result = maven.execute(executionRequest, monitor);
    for (Throwable t : result.getExceptions()) {
      Activator.getLogger().error(t);
    }
    return !result.hasExceptions();
  }
  @Override
  public <T> List<T> getData(String key, Class<T> type, Object... params) {
    List<T> retval = null;

    if ("profileIds".equals(key)) {
      final List<T> profileIds = new ArrayList<T>();

      try {
        final List<Profile> profiles = MavenPlugin.getMaven().getSettings().getProfiles();

        for (final Profile profile : profiles) {
          if (profile.getActivation() != null) {
            if (profile.getActivation().isActiveByDefault()) {
              continue;
            }
          }

          profileIds.add(type.cast(profile.getId()));
        }

        if (params[0] != null && params[0] instanceof File) {
          final File locationDir = (File) params[0];

          File pomFile = new File(locationDir, IMavenConstants.POM_FILE_NAME);

          if (!pomFile.exists() && locationDir.getParentFile().exists()) {
            // try one level up for when user is adding new module
            pomFile = new File(locationDir.getParentFile(), IMavenConstants.POM_FILE_NAME);
          }

          if (pomFile.exists()) {
            final IMaven maven = MavenPlugin.getMaven();

            Model model = maven.readModel(pomFile);

            File parentDir = pomFile.getParentFile();

            while (model != null) {
              for (org.apache.maven.model.Profile p : model.getProfiles()) {
                profileIds.add(type.cast(p.getId()));
              }

              parentDir = parentDir.getParentFile();

              if (parentDir != null && parentDir.exists()) {
                try {
                  model = maven.readModel(new File(parentDir, IMavenConstants.POM_FILE_NAME));
                } catch (Exception e) {
                  model = null;
                }
              }
            }
          }
        }
      } catch (CoreException e) {
        LiferayMavenCore.logError(e);
      }

      retval = profileIds;
    } else if ("liferayVersions".equals(key)) {
      final List<T> possibleVersions = new ArrayList<T>();

      final RepositorySystem system = AetherUtil.newRepositorySystem();

      final RepositorySystemSession session = AetherUtil.newRepositorySystemSession(system);

      final String groupId = params[0].toString();
      final String artifactId = params[1].toString();

      final String coords = groupId + ":" + artifactId + ":[6,)";

      final Artifact artifact = new DefaultArtifact(coords);

      final VersionRangeRequest rangeRequest = new VersionRangeRequest();
      rangeRequest.setArtifact(artifact);
      rangeRequest.addRepository(AetherUtil.newCentralRepository());
      rangeRequest.addRepository(AetherUtil.newLiferayRepository());

      try {
        final VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest);

        final List<Version> versions = rangeResult.getVersions();

        for (Version version : versions) {
          final String val = version.toString();

          if (!"6.2.0".equals(val)) {
            possibleVersions.add(type.cast(val));
          }
        }

        retval = possibleVersions;
      } catch (VersionRangeResolutionException e) {
      }
    } else if ("parentVersion".equals(key)) {
      final List<T> version = new ArrayList<T>();

      final File locationDir = (File) params[0];

      final File parentPom = new File(locationDir, IMavenConstants.POM_FILE_NAME);

      if (parentPom.exists()) {
        try {
          final IMaven maven = MavenPlugin.getMaven();

          final Model model = maven.readModel(parentPom);

          version.add(type.cast(model.getVersion()));

          retval = version;
        } catch (CoreException e) {
          LiferayMavenCore.logError("unable to get parent version", e);
        }
      }
    } else if ("parentGroupId".equals(key)) {
      final List<T> groupId = new ArrayList<T>();

      final File locationDir = (File) params[0];

      final File parentPom = new File(locationDir, IMavenConstants.POM_FILE_NAME);

      if (parentPom.exists()) {
        try {
          final IMaven maven = MavenPlugin.getMaven();

          final Model model = maven.readModel(parentPom);

          groupId.add(type.cast(model.getGroupId()));

          retval = groupId;
        } catch (CoreException e) {
          LiferayMavenCore.logError("unable to get parent groupId", e);
        }
      }
    }

    return retval;
  }