public Dependency getDependency(ArtifactDependency artifactDependency) {
    Dependency dependency = new Dependency();
    dependency.setGroupId(artifactDependency.getOrg());
    dependency.setArtifactId(artifactDependency.getName());

    final String latestVersion =
        resolverService.getLatestVersion(artifactDependency.getOrg(), artifactDependency.getName());
    if (latestVersion == null) {
      // Case of third party or not tracked;
      dependency.setVersion(artifactDependency.getVersion());
    } else {
      dependency.setVersion(latestVersion);
    }

    dependency.setScope(artifactDependency.getScope());
    List<ArtifactDependencyExclusion> artifactDependencyExclusions =
        artifactDependency.getExclusions();
    List<Exclusion> exclusions = new ArrayList<Exclusion>();
    for (ArtifactDependencyExclusion artifactDependencyExclusion : artifactDependencyExclusions) {
      Exclusion ex = new Exclusion();
      ex.setGroupId(artifactDependencyExclusion.getGroupId());
      ex.setArtifactId(artifactDependencyExclusion.getArtifactId());
      exclusions.add(ex);
    }
    dependency.setExclusions(exclusions);

    return dependency;
  }
Пример #2
0
  @Test
  public void testJacocoCommands() throws Exception {

    Project project = initializeJavaProject();
    Dependency junitDep = new Dependency();
    junitDep.setArtifactId("junit");
    junitDep.setGroupId("junit");
    junitDep.setVersion("4.10");
    junitDep.setScope("test");
    MavenCoreFacet facet = project.getFacet(MavenCoreFacet.class);
    Model pom = facet.getPOM();
    pom.addDependency(junitDep);
    facet.setPOM(pom);

    queueInputLines("");
    // create some simple classes
    getShell().execute("java new-class --package com.test \"public class A {}\"");
    getShell().execute("java new-field \"private int numberA=1;\"");
    getShell().execute("java new-method  \"public int getNumberA(){return numberA;}\"");

    getShell().execute("java new-class --package com.test \"public class B {}\"");
    getShell().execute("java new-field \"private int numberB=2;\"");
    getShell().execute("java new-method  \"public int getNumberB(){return numberB;}\"");

    // create and copy test class
    getShell()
        .execute(
            "java new-class --package com.test \"package com.test; import org.junit.Test;import static org.junit.Assert.*; public class ABTest {}\"");
    getShell()
        .execute(
            "java new-method  \"@Test public void testClasses(){A a = new A();B b = new B();assertEquals(3,a.getNumberA()+b.getNumberB());}\"");
    getShell().execute("cd " + project.getProjectRoot().getFullyQualifiedName());
    getShell().execute("cp src/main/java/com/test/ABTest.java src/test/java/");
    getShell().execute("rm -rf src/main/java/com/test/ABTest.java");

    // run jacoco plugin commands
    getShell().execute("jacoco setup");
    getShell().execute("jacoco run-tests --package com.test");
    queueInputLines("N");
    getShell().execute("jacoco create-report");

    assertTrue(
        "Missing jacoco.exec file!",
        project.getProjectRoot().getChildDirectory("target").getChild("jacoco.exec").exists());
    assertTrue(
        "Missing index.html report file!",
        project
            .getProjectRoot()
            .getChildDirectory("target")
            .getChildDirectory("site")
            .getChildDirectory("jacoco")
            .getChild("index.html")
            .exists());
  }
 private Dependency createSystemScopeDependency(Artifact artifact, File location, String suffix) {
   String artifactId = artifact.getArtifactId();
   if (suffix != null) {
     artifactId = "_" + suffix;
   }
   final Dependency dependency = new Dependency();
   dependency.setGroupId(artifact.getGroupId());
   dependency.setArtifactId(artifactId);
   dependency.setVersion(artifact.getVersion());
   dependency.setScope(Artifact.SCOPE_SYSTEM);
   dependency.setSystemPath(location.getAbsolutePath());
   return dependency;
 }
Пример #4
0
  private void createFullSourcePom(List<Dependency> newDependencies) throws MojoFailureException {
    // Change spring-test and jmock dependencies to use <optional>true</option> instead of
    // <scope>test</scope>.
    // This is necessary because Base*TestCase classes are in src/main/java. If we move these
    // classes to their
    // own test module, this will no longer be necessary. For the first version of this mojo, it
    // seems easier
    // to follow the convention used in AppFuse rather than creating a test module and changing all
    // modules to
    // depend on it.

    // create properties based on dependencies while we're at it
    Set<String> projectProperties = new TreeSet<String>();

    for (Dependency dep : newDependencies) {
      if (dep.getArtifactId().equals("spring-test")
          || dep.getArtifactId().contains("jmock")
          || dep.getArtifactId().equals("junit")
          || dep.getArtifactId().equals("shale-test")) {
        dep.setOptional(true);
        dep.setScope(null);
      }
      String version = dep.getVersion();
      // trim off ${}
      if (version.startsWith("${")) {
        version = version.substring(2);
      }

      if (version.endsWith("}")) {
        version = version.substring(0, version.length() - 1);
      }
      projectProperties.add(version);
    }

    // add core as a dependency for modular wars
    if (project.getPackaging().equals("war") && project.hasParent()) {
      Dependency core = new Dependency();
      core.setGroupId("${project.parent.groupId}");
      core.setArtifactId("core");
      core.setVersion("${project.parent.version}");
      newDependencies.add(core);

      // workaround for JSF requiring JSP 2.1 - this is a true hack
      if (project.getProperties().getProperty("web.framework").equals("jsf")) {
        Dependency jsp21 = new Dependency();
        jsp21.setGroupId("javax.servlet.jsp");
        jsp21.setArtifactId("jsp-api");
        jsp21.setVersion("${jsp.version}");
        jsp21.setScope("provided");
        newDependencies.add(jsp21);

        // replace jsp.version property as well
        project.getOriginalModel().getProperties().setProperty("jsp.version", "2.1");
      }
    }

    Collections.sort(newDependencies, new BeanComparator("groupId"));

    project.getOriginalModel().setDependencies(newDependencies);

    Properties currentProperties = project.getOriginalModel().getProperties();

    Set<String> currentKeys = new LinkedHashSet<String>();
    for (Object key : currentProperties.keySet()) {
      currentKeys.add((String) key);
    }

    StringBuffer sortedProperties = new StringBuffer();

    Properties appfuseProperties = getAppFuseProperties();

    // holder for properties - stored in ThreadLocale
    Map<String, String> propertiesForPom = new LinkedHashMap<String, String>();

    for (String key : projectProperties) {
      // don't add property if it already exists in project
      if (!currentKeys.contains(key)) {
        String value = appfuseProperties.getProperty(key);

        // this happens when the version number is hard-coded
        if (value == null) {
          continue;
        }

        // hack for Tapestry depending on commons-pool (a.k.a. commons-dbcp 1.2.2)
        if ("tapestry".equals(project.getProperties().getProperty("web.framework"))
            && key.equals("commons.dbcp.version")) {
          value = "1.2.2";
        }

        if (value.contains("&amp;")) {
          value = "<![CDATA[" + value + "]]>";
        }

        sortedProperties
            .append("        <")
            .append(key)
            .append(">")
            .append(value)
            .append("</")
            .append(key)
            .append(">" + "\n");
        propertiesForPom.put(key, value);
      }
    }

    if (project.getPackaging().equals("pom") || project.hasParent()) {
      // store sorted properties in a thread local for later retrieval
      Map<String, String> properties = new LinkedHashMap<String, String>();
      if (propertiesContextHolder.get() != null) {
        properties = (LinkedHashMap) propertiesContextHolder.get();
      }

      for (String key : propertiesForPom.keySet()) {
        if (!properties.containsKey(key)) {
          properties.put(key, propertiesForPom.get(key));
        }
      }

      propertiesContextHolder.set(properties);
    }

    StringWriter writer = new StringWriter();

    try {
      project.writeOriginalModel(writer);

      File pom = new File("pom-fullsource.xml");

      if (pom.exists()) {
        pom.delete();
      }

      FileWriter fw = new FileWriter(pom);
      fw.write(writer.toString());
      fw.flush();
      fw.close();
    } catch (IOException ex) {
      getLog().error("Unable to create pom-fullsource.xml: " + ex.getMessage(), ex);
      throw new MojoFailureException(ex.getMessage());
    }

    log("Updated dependencies in pom.xml...");

    // I tried to use regex here, but couldn't get it to work - going with the old fashioned way
    // instead
    String pomXml = writer.toString();
    int startTag = pomXml.indexOf("\n  <dependencies>");

    String dependencyXml = pomXml.substring(startTag, pomXml.indexOf("</dependencies>", startTag));
    // change 2 spaces to 4
    dependencyXml = dependencyXml.replaceAll("  ", "    ");
    dependencyXml =
        "\n    <!-- Dependencies calculated by AppFuse when running full-source plugin -->"
            + dependencyXml;

    try {
      String packaging = project.getPackaging();
      String pathToPom = "pom.xml";
      if (project.hasParent()) {
        if (packaging.equals("jar")) {
          pathToPom = "core/" + pathToPom;
        } else if (packaging.equals("war")) {
          pathToPom = "web/" + pathToPom;
        }
      }

      String originalPom = FileUtils.readFileToString(new File(pathToPom), "UTF-8");
      // replace tabs with spaces (in case user has changed their pom.xml
      originalPom = originalPom.replace("\t", "    ");
      startTag = originalPom.indexOf("\n    <dependencies>");

      StringBuffer sb = new StringBuffer();
      sb.append(originalPom.substring(0, startTag));
      sb.append(dependencyXml);
      sb.append(originalPom.substring(originalPom.indexOf("</dependencies>", startTag)));

      String adjustedPom = sb.toString();

      // Calculate properties and add them to pom if not a modular project - otherwise properties
      // are added
      // near the end of this method from a threadlocal
      if (!project.getPackaging().equals("pom") && !project.hasParent()) {
        adjustedPom = addPropertiesToPom(adjustedPom, sortedProperties);
      }

      adjustedPom = adjustLineEndingsForOS(adjustedPom);

      FileUtils.writeStringToFile(
          new File(pathToPom), adjustedPom, "UTF-8"); // was pomWithProperties
    } catch (IOException ex) {
      getLog().error("Unable to write to pom.xml: " + ex.getMessage(), ex);
      throw new MojoFailureException(ex.getMessage());
    }

    boolean renamePackages = true;
    if (System.getProperty("renamePackages") != null) {
      renamePackages = Boolean.valueOf(System.getProperty("renamePackages"));
    }

    if (renamePackages && !project.getPackaging().equals("pom")) {
      log("Renaming packages to '" + project.getGroupId() + "'...");
      RenamePackages renamePackagesTool = new RenamePackages(project.getGroupId());
      if (project.hasParent()) {
        if (project.getPackaging().equals("jar")) {
          renamePackagesTool.setBaseDir("core/src");
        } else {
          renamePackagesTool.setBaseDir("web/src");
        }
      }

      renamePackagesTool.execute();
    }

    // when performing full-source on a modular project, add the properties to the root pom.xml at
    // the end
    if (project.getPackaging().equals("war") && project.hasParent()) {
      // store sorted properties in a thread local for later retrieval
      Map properties = propertiesContextHolder.get();
      // alphabetize the properties by key
      Set<String> propertiesToAdd = new TreeSet<String>(properties.keySet());

      StringBuffer calculatedProperties = new StringBuffer();

      for (String key : propertiesToAdd) {
        // don't add property if it already exists in project
        Set<Object> keysInProject = project.getParent().getOriginalModel().getProperties().keySet();
        if (!keysInProject.contains(key)) {
          String value = getAppFuseProperties().getProperty(key);

          if (value.contains("&amp;")) {
            value = "<![CDATA[" + value + "]]>";
          }

          calculatedProperties.append("        <");
          calculatedProperties.append(key);
          calculatedProperties.append(">");
          calculatedProperties.append(value);
          calculatedProperties.append("</");
          calculatedProperties.append(key);
          calculatedProperties.append(">");
          calculatedProperties.append("\n");
        }
      }

      try {
        String originalPom = FileUtils.readFileToString(new File("pom.xml"), "UTF-8");

        // Move modules to build section.
        originalPom = originalPom.replaceAll("  <modules>", "");
        // Because I hate f*****g regex.
        originalPom = originalPom.replaceAll("    <module>.*?</module>", "");
        originalPom = originalPom.replaceAll("  </modules>", "");

        originalPom =
            originalPom.replace(
                "<repositories>",
                "<modules>\n"
                    + "        <module>core</module>\n"
                    + "        <module>web</module>\n"
                    + "    </modules>\n\n    <repositories>");

        String pomWithProperties = addPropertiesToPom(originalPom, calculatedProperties);

        FileUtils.writeStringToFile(new File("pom.xml"), pomWithProperties, "UTF-8");
      } catch (IOException ex) {
        getLog().error("Unable to read root pom.xml: " + ex.getMessage(), ex);
        throw new MojoFailureException(ex.getMessage());
      }
    }

    // cleanup so user isn't aware that files were created
    File pom = new File("pom-fullsource.xml");

    if (pom.exists()) {
      pom.delete();
    }
  }
    /**
     * Build FO documents from DocBook XML sources, including fonts.
     *
     * @param baseConfiguration Common configuration for all executions
     * @param format Specific output format (pdf, rtf)
     * @throws MojoExecutionException Failed to build the output.
     */
    void buildFO(final ArrayList<MojoExecutor.Element> baseConfiguration, final String format)
        throws MojoExecutionException {
      if (!(format.equalsIgnoreCase("pdf") || format.equalsIgnoreCase("rtf"))) {
        throw new MojoExecutionException(
            "Output format " + format + " is not supported." + " Use either pdf or rtf.");
      }

      ArrayList<MojoExecutor.Element> cfg = new ArrayList<MojoExecutor.Element>();
      cfg.addAll(baseConfiguration);
      cfg.add(
          element(
              name("foCustomization"), FilenameUtils.separatorsToUnix(foCustomization.getPath())));

      // If you update this list, also see copyFonts().
      String fontDir = FilenameUtils.separatorsToUnix(fontsDirectory.getPath());
      cfg.add(
          element(
              name("fonts"),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSans"),
                  element(name("style"), "normal"),
                  element(name("weight"), "normal"),
                  element(name("embedFile"), fontDir + "/DejaVuSans.ttf"),
                  element(name("metricsFile"), fontDir + "/DejaVuSans-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSans"),
                  element(name("style"), "normal"),
                  element(name("weight"), "bold"),
                  element(name("embedFile"), fontDir + "/DejaVuSansCondensed-Bold.ttf"),
                  element(name("metricsFile"), fontDir + "/DejaVuSansCondensed-Bold-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSans"),
                  element(name("style"), "italic"),
                  element(name("weight"), "normal"),
                  element(name("embedFile"), fontDir + "/DejaVuSans-Oblique.ttf"),
                  element(name("metricsFile"), fontDir + "/DejaVuSans-Oblique-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSans"),
                  element(name("style"), "italic"),
                  element(name("weight"), "bold"),
                  element(name("embedFile"), fontDir + "/DejaVuSansCondensed-BoldOblique.ttf"),
                  element(
                      name("metricsFile"),
                      fontDir + "/DejaVuSansCondensed-BoldOblique-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSansMono"),
                  element(name("style"), "normal"),
                  element(name("weight"), "normal"),
                  element(name("embedFile"), fontDir + "/DejaVuSansMono.ttf"),
                  element(name("metricsFile"), fontDir + "/DejaVuSansMono-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSansMono"),
                  element(name("style"), "normal"),
                  element(name("weight"), "bold"),
                  element(name("embedFile"), fontDir + "/DejaVuSansMono-Bold.ttf"),
                  element(name("metricsFile"), fontDir + "/DejaVuSansMono-Bold-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSansMono"),
                  element(name("style"), "italic"),
                  element(name("weight"), "normal"),
                  element(name("embedFile"), fontDir + "/DejaVuSansMono-Oblique.ttf"),
                  element(name("metricsFile"), fontDir + "/DejaVuSansMono-Oblique-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSansMono"),
                  element(name("style"), "italic"),
                  element(name("weight"), "bold"),
                  element(name("embedFile"), fontDir + "/DejaVuSansMono-BoldOblique.ttf"),
                  element(
                      name("metricsFile"), fontDir + "/DejaVuSansMono-BoldOblique-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSerif"),
                  element(name("style"), "normal"),
                  element(name("weight"), "normal"),
                  element(name("embedFile"), fontDir + "/DejaVuSerif.ttf"),
                  element(name("metricsFile"), fontDir + "/DejaVuSerif-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSerif"),
                  element(name("style"), "normal"),
                  element(name("weight"), "bold"),
                  element(name("embedFile"), fontDir + "/DejaVuSerifCondensed-Bold.ttf"),
                  element(name("metricsFile"), fontDir + "/DejaVuSerifCondensed-Bold-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSerif"),
                  element(name("style"), "italic"),
                  element(name("weight"), "normal"),
                  element(name("embedFile"), fontDir + "/DejaVuSerif-Italic.ttf"),
                  element(name("metricsFile"), fontDir + "/DejaVuSerif-Italic-metrics.xml")),
              element(
                  name("font"),
                  element(name("name"), "DejaVuSerif"),
                  element(name("style"), "italic"),
                  element(name("weight"), "bold"),
                  element(name("embedFile"), fontDir + "/DejaVuSerifCondensed-BoldItalic.ttf"),
                  element(
                      name("metricsFile"),
                      fontDir + "/DejaVuSerifCondensed-BoldItalic-metrics.xml"))));

      Set<String> docNames = DocUtils.getDocumentNames(xmlSourceDirectory, getDocumentSrcName());
      if (docNames.isEmpty()) {
        throw new MojoExecutionException("No document names found.");
      }

      // When using generated sources, copy the images manually.
      if (xmlSourceDirectory != getDocbkxSourceDirectory()) {
        for (String docName : docNames) {
          File srcDir = new File(imageSourceDirectory, docName + File.separator + "images");
          File destDir = new File(xmlSourceDirectory, docName + File.separator + "images");
          try {
            if (srcDir.exists()) {
              FileUtils.copyDirectory(srcDir, destDir);
            }
          } catch (IOException e) {
            throw new MojoExecutionException(
                "Failed to copy images from " + srcDir + " to " + destDir);
          }
        }
      }

      for (String docName : docNames) {
        cfg.add(element(name("includes"), docName + "/" + getDocumentSrcName()));

        // Permit hyphenation.
        Dependency offo = new Dependency();
        offo.setGroupId("net.sf.offo");
        offo.setArtifactId("fop-hyph");
        offo.setVersion("1.2");
        offo.setScope("runtime");
        Plugin plugin =
            plugin(
                groupId("com.agilejava.docbkx"),
                artifactId("docbkx-maven-plugin"),
                version(getDocbkxVersion()));
        plugin.addDependency(offo);

        executeMojo(
            plugin,
            goal("generate-" + format),
            configuration(cfg.toArray(new Element[0])),
            executionEnvironment(getProject(), getSession(), getPluginManager()));

        // Avoid each new document overwriting the last.
        File file =
            new File(
                getDocbkxOutputDirectory(),
                format
                    + File.separator
                    + FilenameUtils.getBaseName(getDocumentSrcName())
                    + "."
                    + format);
        renameDocument(file, docName);
      }
    }