MavenProject readProject(File pomFile) throws IOException {
   MavenXpp3Reader mavenReader = new MavenXpp3Reader();
   FileReader fileReader = null;
   try {
     fileReader = new FileReader(pomFile);
     Model model = mavenReader.read(fileReader);
     model.setPomFile(pomFile);
     MavenProject project = new MavenProject(model);
     project.setFile(pomFile);
     project.setArtifact(
         createArtifact(
             pomFile,
             model.getGroupId(),
             model.getArtifactId(),
             model.getVersion(),
             "compile",
             model.getPackaging(),
             ""));
     return project;
   } catch (Exception e) {
     throw new RuntimeException(e);
   } finally {
     if (fileReader != null) {
       fileReader.close();
     }
   }
 }
Beispiel #2
0
  public void testIsImpalaModule() throws Exception {
    assertEquals("war", model.getPackaging());

    mojo.setProject(project);
    assertTrue(mojo.isImpalaHost());

    properties.setProperty("impala.host", "false");
    assertFalse(mojo.isImpalaHost());

    properties.setProperty("impala.host", "true");
    assertTrue(mojo.isImpalaHost());

    model.setPackaging("jar");
    assertTrue(mojo.isImpalaHost());
  }
  /** For each project in the current build set, reset the version if using project.version */
  @Override
  public Set<Project> applyChanges(final List<Project> projects, final ManipulationSession session)
      throws ManipulationException {
    final ProjectVersionEnforcingState state = session.getState(ProjectVersionEnforcingState.class);
    if (!session.isEnabled()
        || !session.anyStateEnabled(State.activeByDefault)
        || state == null
        || !state.isEnabled()) {
      logger.debug("Project version enforcement is disabled.");
      return Collections.emptySet();
    }

    final Set<Project> changed = new HashSet<Project>();

    for (final Project project : projects) {
      final Model model = project.getModel();

      if (model.getPackaging().equals("pom")) {
        enforceProjectVersion(project, model.getDependencies(), changed);

        if (model.getDependencyManagement() != null) {
          enforceProjectVersion(
              project, model.getDependencyManagement().getDependencies(), changed);
        }

        final List<Profile> profiles = model.getProfiles();
        if (profiles != null) {
          for (final Profile profile : model.getProfiles()) {
            enforceProjectVersion(project, profile.getDependencies(), changed);
            if (profile.getDependencyManagement() != null) {
              enforceProjectVersion(
                  project, profile.getDependencyManagement().getDependencies(), changed);
            }
          }
        }
      }
    }
    if (changed.size() > 0) {
      logger.warn(
          "Using ${project.version} in pom files may lead to unexpected errors with inheritance.");
    }
    return changed;
  }
  @Override
  public Model getLifecycleModel(final Model model) {
    if (model == null) {
      throw new NullPointerException("model");
    }

    final Model lifecycleModel = new Model();
    lifecycleModel.setBuild(new Build());
    lifecycleModel
        .getBuild()
        .setPluginManagement(
            model.getBuild() != null ? model.getBuild().getPluginManagement() : null);

    lifecycleModel
        .getBuild()
        .getPlugins()
        .addAll(this.getPluginsBoundByDefaultToAllLifecycles(model.getPackaging()));

    return lifecycleModel;
  }
  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;
  }
 /**
  * Create the descriptor and set base information.
  *
  * @param model The model.
  * @param scanner The scanner.
  * @return The descriptor.
  */
 protected MavenPomDescriptor createMavenPomDescriptor(Model model, Scanner scanner) {
   ScannerContext context = scanner.getContext();
   MavenPomDescriptor pomDescriptor = context.peek(MavenPomDescriptor.class);
   pomDescriptor.setName(model.getName());
   pomDescriptor.setGroupId(model.getGroupId());
   pomDescriptor.setArtifactId(model.getArtifactId());
   pomDescriptor.setPackaging(model.getPackaging());
   pomDescriptor.setVersion(model.getVersion());
   String pomFqn = getFullyQualifiedName(model);
   pomDescriptor.setFullQualifiedName(pomFqn);
   Coordinates artifactCoordinates = new ModelCoordinates(model);
   MavenArtifactDescriptor artifact =
       getArtifactResolver(context).resolve(artifactCoordinates, context);
   // if the pom describes itself as artifact then the returned artifact
   // descriptor must be used as pom descriptor (the old instance is
   // invalidated due to adding labels)
   if (MavenPomDescriptor.class.isAssignableFrom(artifact.getClass())) {
     pomDescriptor = MavenPomDescriptor.class.cast(artifact);
   }
   pomDescriptor.getDescribes().add(artifact);
   return pomDescriptor;
 }
  private DefaultCoreExtension parseMavenPom(
      URL descriptorUrl, DefaultCoreExtensionRepository repository)
      throws IOException, XmlPullParserException {
    DefaultCoreExtension coreExtension = null;

    InputStream descriptorStream = descriptorUrl.openStream();
    try {
      MavenXpp3Reader reader = new MavenXpp3Reader();
      Model mavenModel = reader.read(descriptorStream);

      String version = resolveVersion(mavenModel.getVersion(), mavenModel, false);
      String groupId = resolveGroupId(mavenModel.getGroupId(), mavenModel, false);

      URL extensionURL = getExtensionURL(descriptorUrl);

      coreExtension =
          new MavenCoreExtension(
              repository,
              extensionURL,
              new ExtensionId(groupId + ':' + mavenModel.getArtifactId(), version),
              packagingToType(mavenModel.getPackaging()),
              mavenModel);

      coreExtension.setName(mavenModel.getName());
      coreExtension.setSummary(mavenModel.getDescription());
      for (Developer developer : mavenModel.getDevelopers()) {
        URL authorURL = null;
        if (developer.getUrl() != null) {
          try {
            authorURL = new URL(developer.getUrl());
          } catch (MalformedURLException e) {
            // TODO: log ?
          }
        }

        coreExtension.addAuthor(new DefaultExtensionAuthor(developer.getId(), authorURL));
      }
      coreExtension.setWebsite(mavenModel.getUrl());

      // licenses
      for (License license : mavenModel.getLicenses()) {
        coreExtension.addLicense(getExtensionLicense(license));
      }

      // features
      String featuresString = mavenModel.getProperties().getProperty("xwiki.extension.features");
      if (StringUtils.isNotBlank(featuresString)) {
        coreExtension.setFeatures(
            this.converter.<Collection<String>>convert(List.class, featuresString));
      }

      // custom properties
      coreExtension.putProperty("maven.groupId", groupId);
      coreExtension.putProperty("maven.artifactId", mavenModel.getArtifactId());

      // dependencies
      for (Dependency mavenDependency : mavenModel.getDependencies()) {
        if (!mavenDependency.isOptional()
            && (mavenDependency.getScope() == null
                || mavenDependency.getScope().equals("compile")
                || mavenDependency.getScope().equals("runtime"))) {

          String dependencyGroupId = resolveGroupId(mavenDependency.getGroupId(), mavenModel, true);
          String dependencyArtifactId = mavenDependency.getArtifactId();
          String dependencyClassifier = mavenDependency.getClassifier();
          String dependencyVersion = resolveVersion(mavenDependency.getVersion(), mavenModel, true);

          DefaultExtensionDependency extensionDependency =
              new MavenCoreExtensionDependency(
                  toExtensionId(dependencyGroupId, dependencyArtifactId, dependencyClassifier),
                  new DefaultVersionConstraint(dependencyVersion),
                  mavenDependency);

          coreExtension.addDependency(extensionDependency);
        }
      }
    } finally {
      IOUtils.closeQuietly(descriptorStream);
    }

    return coreExtension;
  }
  public MojoProjectStub(File projectDir) {
    this.basedir = projectDir;
    props.setProperty("basedir", this.basedir.getAbsolutePath());

    File pom = new File(getBasedir(), "plugin-config.xml");
    MavenXpp3Reader pomReader = new MavenXpp3Reader();
    Model model = null;
    FileReader fileReader = null;

    try {
      fileReader = new FileReader(pom);
      model = pomReader.read(fileReader);
      setModel(model);
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      IOUtils.closeQuietly(fileReader);
    }

    setGroupId(model.getGroupId());
    setArtifactId(model.getArtifactId());
    setVersion(model.getVersion());
    setName(model.getName());
    setUrl(model.getUrl());
    setPackaging(model.getPackaging());
    setFile(pom);

    build = model.getBuild();
    if (build == null) {
      build = new Build();
    }

    File srcDir = getStandardDir(getBuild().getSourceDirectory(), "src/main/java");
    getBuild().setSourceDirectory(srcDir.getAbsolutePath());

    File targetDir = getStandardDir(getBuild().getDirectory(), "target");
    getBuild().setDirectory(targetDir.getAbsolutePath());

    File outputDir = getStandardDir(getBuild().getOutputDirectory(), "target/classes");
    getBuild().setOutputDirectory(outputDir.getAbsolutePath());

    List<Resource> resources = new ArrayList<Resource>();
    resources.addAll(getBuild().getResources());

    if (resources.isEmpty()) {
      resources = new ArrayList<Resource>();
      Resource resource = new Resource();
      File resourceDir = normalize("src/main/resources");
      resource.setDirectory(resourceDir.getAbsolutePath());
      makeDirs(resourceDir);
      resources.add(resource);
    } else {
      // Make this project work on windows ;-)
      for (Resource resource : resources) {
        File dir = normalize(resource.getDirectory());
        resource.setDirectory(dir.getAbsolutePath());
      }
    }

    getBuild().setResources(resources);
  }
  /**
   * Deploys with given Wagon (hint is provided), to deployUrl. It is caller matter to adjust those
   * two (ie. deployUrl with file: protocol to be deployed with file wagon would be error). Model is
   * supplied since it is read before.
   *
   * @param wagonHint
   * @param deployUrl
   * @param model
   * @throws Exception
   */
  protected void deployArtifacts(File project, String wagonHint, String deployUrl, Model model)
      throws Exception {
    log.info(
        "Deploying project \""
            + project.getAbsolutePath()
            + "\" using Wagon:"
            + wagonHint
            + " to URL=\""
            + deployUrl
            + "\".");

    // we already check if the pom.xml was in here.
    File pom = new File(project, "pom.xml");

    // FIXME, this needs to be fluffed up a little, should add the classifier, etc.
    String artifactFileName = model.getArtifactId() + "." + model.getPackaging();
    File artifactFile = new File(project, artifactFileName);

    log.debug("wow, this is working: " + artifactFile.getName());

    Gav gav =
        new Gav(
            model.getGroupId(),
            model.getArtifactId(),
            model.getVersion(),
            null,
            model.getPackaging(),
            0,
            new Date().getTime(),
            model.getName(),
            false,
            false,
            null,
            false,
            null);

    // the Restlet Client does not support multipart forms:
    // http://restlet.tigris.org/issues/show_bug.cgi?id=71

    // int status = DeployUtils.deployUsingPomWithRest( deployUrl, repositoryId, gav, artifactFile,
    // pom );

    if (!"pom".equals(model.getPackaging()) && !artifactFile.isFile()) {
      throw new FileNotFoundException(
          "File " + artifactFile.getAbsolutePath() + " doesn't exists!");
    }

    File artifactSha1 = new File(artifactFile.getAbsolutePath() + ".sha1");
    File artifactMd5 = new File(artifactFile.getAbsolutePath() + ".md5");
    File artifactAsc = new File(artifactFile.getAbsolutePath() + ".asc");

    File pomSha1 = new File(pom.getAbsolutePath() + ".sha1");
    File pomMd5 = new File(pom.getAbsolutePath() + ".md5");
    File pomAsc = new File(pom.getAbsolutePath() + ".asc");

    try {
      if (artifactSha1.exists()) {
        getDeployUtils()
            .deployWithWagon(
                wagonHint, deployUrl, artifactSha1, this.getRelitiveArtifactPath(gav) + ".sha1");
      }
      if (artifactMd5.exists()) {
        getDeployUtils()
            .deployWithWagon(
                wagonHint, deployUrl, artifactMd5, this.getRelitiveArtifactPath(gav) + ".md5");
      }
      if (artifactAsc.exists()) {
        getDeployUtils()
            .deployWithWagon(
                wagonHint, deployUrl, artifactAsc, this.getRelitiveArtifactPath(gav) + ".asc");
      }

      if (artifactFile.exists()) {
        getDeployUtils()
            .deployWithWagon(wagonHint, deployUrl, artifactFile, this.getRelitiveArtifactPath(gav));
      }

      if (pomSha1.exists()) {
        getDeployUtils()
            .deployWithWagon(wagonHint, deployUrl, pomSha1, this.getRelitivePomPath(gav) + ".sha1");
      }
      if (pomMd5.exists()) {
        getDeployUtils()
            .deployWithWagon(wagonHint, deployUrl, pomMd5, this.getRelitivePomPath(gav) + ".md5");
      }
      if (pomAsc.exists()) {
        getDeployUtils()
            .deployWithWagon(wagonHint, deployUrl, pomAsc, this.getRelitivePomPath(gav) + ".asc");
      }

      getDeployUtils().deployWithWagon(wagonHint, deployUrl, pom, this.getRelitivePomPath(gav));
    } catch (Exception e) {
      log.error(getTestId() + " Unable to deploy " + artifactFileName, e);
      throw e;
    }
  }
Beispiel #10
0
 /**
  * returns the packaging of the maven project
  *
  * @param module
  * @return
  * @throws CoreException
  */
 private static String getPackaging(IModule module) throws CoreException {
   Model model = MavenPlugin.getMavenModelManager().readMavenModel(getModelFile(module));
   return model.getPackaging();
 }