@Override
  public boolean applyChange(MavenProject project, Element root, String eol)
      throws ProjectRewriteException {
    boolean modified = false;

    if (project.hasParent()) {
      Namespace ns = getNamespaceOrNull(root);
      Element parentVersionElement = root.getChild("parent", ns).getChild("version", ns);
      MavenProject parent = project.getParent();
      String parentId = ArtifactUtils.versionlessKey(parent.getGroupId(), parent.getArtifactId());

      String parentVersion = releaseVersions.get(parentId);
      if (null == parentVersion && consistentProjectVersions && releaseVersions.size() > 0) {
        // Use any release version, as the project's versions are consistent/global
        parentVersion = releaseVersions.values().iterator().next();
      }

      if (null == parentVersion) {
        if (parent.getVersion().equals(originalVersions.get(parentId))) {
          throw new ProjectRewriteException(
              "Release version for parent " + parent.getName() + " was not found");
        }
      } else {
        workLog.add("setting parent version to '" + parentVersion + "'");
        parentVersionElement.setText(parentVersion);
        modified = true;
      }
    }

    return modified;
  }
예제 #2
0
  private List<Dependency> addWebDependencies(
      String appfuseVersion, List<Dependency> newDependencies, String webFramework) {
    // Add dependencies from appfuse-common-web
    newDependencies = addModuleDependencies(newDependencies, "web", "web");

    Double appfuseVersionAsDouble =
        new Double(appfuseVersion.substring(0, appfuseVersion.lastIndexOf(".")));

    getLog().debug("Detected AppFuse version: " + appfuseVersionAsDouble);

    if (isAppFuse() && appfuseVersionAsDouble < 2.1) {

      // Add dependencies from appfuse-common-web
      newDependencies = addModuleDependencies(newDependencies, "web-common", "web/common");

      // newDependencies = addModuleDependencies(newDependencies, webFramework, "web/" +
      // webFramework);
    }

    // modular archetypes still seem to need these - todo: figure out why
    if (isAppFuse() && project.getPackaging().equals("war") && project.hasParent()) {
      newDependencies = addModuleDependencies(newDependencies, "web-common", "web/common");

      newDependencies = addModuleDependencies(newDependencies, webFramework, "web/" + webFramework);
    }
    return newDependencies;
  }
 protected void tryAggregateUpper(MavenProject prj) throws Exception {
   if (prj != null && prj.hasParent() && canAggregate()) {
     MavenProject parent = prj.getParent();
     List<MavenProject> modules = parent.getCollectedProjects();
     if ((modules.size() > 1) && prj.equals(modules.get(modules.size() - 1))) {
       aggregate(parent);
     }
   }
 }
예제 #4
0
  public void execute() throws MojoExecutionException, MojoFailureException {
    // http://issues.appfuse.org/browse/APF-1025
    System.setProperty("file.encoding", "UTF-8");

    // If appfuse.version is specified as a property, and not a SNAPSHOT, use it for the tag
    String appfuseVersion = project.getProperties().getProperty("appfuse.version");

    if ((appfuseVersion != null) && !appfuseVersion.endsWith("SNAPSHOT") && tag.equals("trunk/")) {
      tag = "tags/APPFUSE_" + appfuseVersion.toUpperCase().replaceAll("-", "_") + "/";
      // APF-1168: Allow milestone and release candidates
      if (tag.contains("_M")) {
        tag = tag.replace("_M", "-M");
      } else if (tag.contains("_R")) {
        tag = tag.replace("_R", "-R");
      }
    }

    String daoFramework = project.getProperties().getProperty("dao.framework");

    if (daoFramework == null) {
      log("No dao.framework property specified, defaulting to 'hibernate'");
    }

    String webFramework = project.getProperties().getProperty("web.framework");

    boolean modular = (project.getPackaging().equals("pom") && !project.hasParent());
    if (project.getPackaging().equals("jar")
        || (project.getPackaging().equals("war") && !project.hasParent())) {
      // export data-common
      log("Installing source from data-common module...");
      export("data/common/src", (modular) ? "core/src" : destinationDirectory);

      // export persistence framework
      log("Installing source from " + daoFramework + " module...");
      export("data/" + daoFramework + "/src", (modular) ? "core/src" : destinationDirectory);

      // export service module
      log("Installing source from service module...");
      export("service/src", (modular) ? "core/src" : destinationDirectory);

      // move Base*TestCase to test directory
      moveFiles(
          (modular) ? "core/src/main" : destinationDirectory + "/main",
          (modular) ? "core/src/test" : destinationDirectory + "/test",
          "**/Base*TestCase.java");

      // delete dao.framework related files from test directory
      deleteFile("test/resources/hibernate.cfg.xml");
      deleteFile("test/resources/META-INF");
      deleteFile("test/resources/sql-map-config.xml");

      // If JPA, delete hibernate.cfg.xml b/c it will cause issues when
      // using jpaconfiguration with the hibernate3-maven-plugin
      if ("jpa".equalsIgnoreCase(daoFramework)) {
        deleteFile("main/resources/hibernate.cfg.xml");
      }
    }

    // it's OK if a project created with appfuse-ws doesn't have a web framework defined
    // currently, a project with appfuse-ws can be identified by enunciate
    boolean isWebServicesProject = false;
    for (Object pluginArtifact : project.getPluginArtifacts()) {
      if (((Artifact) pluginArtifact).getArtifactId().contains("enunciate")) {
        isWebServicesProject = true;
        break;
      }
    }

    if (project.getPackaging().equalsIgnoreCase("war")) {

      if (webFramework == null && !isWebServicesProject) {
        getLog()
            .error(
                "The web.framework property is not specified - please modify your pom.xml to add "
                    + " this property. For example: <web.framework>struts</web.framework>.");
        throw new MojoExecutionException(
            "No web.framework property specified, please modify pom.xml to add it.");
      }

      if (project.hasParent()) {
        // copy jdbc.properties to core/src/test/resources
        // FileUtils.copyFileToDirectory(new File("src/main/resources/jdbc.properties"), new
        // File("../core/src/test/resources"));

        // delete hibernate, ibatis and jpa files from web project
        deleteFile("main/resources/hibernate.cfg.xml");
        deleteFile("main/resources/META-INF");
        deleteFile("main/resources/sql-map-config.xml");

        // there's a jdbc.properties in test/resources that shouldn't be there
        deleteFile("test/resources/jdbc.properties");
      } else if (!isAppFuse()) {
        // there's a jdbc.properties in test/resources that shouldn't be there
        deleteFile("test/resources/jdbc.properties");
      }
    }

    log("Source successfully exported, modifying pom.xml...");

    List dependencies = project.getOriginalModel().getDependencies();
    List<Dependency> newDependencies = new ArrayList<Dependency>();

    // remove all appfuse dependencies
    for (Object dependency : dependencies) {
      Dependency dep = (Dependency) dependency;

      if (!dep.getGroupId().equals(APPFUSE_GROUP_ID)) {
        newDependencies.add(dep);
      }
    }

    if (!project.getPackaging().equals("pom") && !project.hasParent()) {

      // add dependencies from root appfuse pom
      newDependencies = addModuleDependencies(newDependencies, "root", "");

      // Add dependencies from appfuse-data
      newDependencies = addModuleDependencies(newDependencies, "data", "data");

      // Add dependencies from appfuse-data-common
      newDependencies = addModuleDependencies(newDependencies, "data-common", "data/common");

      // Add dependencies from appfuse-${dao.framework}
      newDependencies =
          addModuleDependencies(newDependencies, daoFramework, "data/" + daoFramework);

      // Add dependencies from appfuse-service
      newDependencies = addModuleDependencies(newDependencies, "service", "service");

      if (!isWebServicesProject && project.getPackaging().equals("war")) {
        newDependencies = addWebDependencies(appfuseVersion, newDependencies, webFramework);
      }

      createFullSourcePom(newDependencies);
    } else {
      if (project.getPackaging().equals("pom")) {
        // add dependencies from root appfuse pom
        newDependencies = addModuleDependencies(newDependencies, "root", "");

        createFullSourcePom(newDependencies);
      }

      if (project.getPackaging().equals("jar")) {
        newDependencies.clear();

        // Add dependencies from appfuse-data
        newDependencies = addModuleDependencies(newDependencies, "data", "data");

        // Add dependencies from appfuse-data-common
        newDependencies = addModuleDependencies(newDependencies, "data-common", "data/common");

        // Add dependencies from appfuse-${dao.framework}
        newDependencies =
            addModuleDependencies(newDependencies, daoFramework, "data/" + daoFramework);

        // Add dependencies from appfuse-service
        newDependencies = addModuleDependencies(newDependencies, "service", "service");

        createFullSourcePom(newDependencies);
      }

      if (project.getPackaging().equals("war")) {
        newDependencies.clear();

        newDependencies = addWebDependencies(appfuseVersion, newDependencies, webFramework);

        createFullSourcePom(newDependencies);
      }
    }
  }
예제 #5
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();
    }
  }