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()) {
renamePackagesTool.setBaseDir(project.getBasedir() + "/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("&")) {
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>", "");
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