Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #93: plugin should be able to generate reports in xml format #112

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.plexus.util.StringUtils;

import java.io.File;
import java.util.Collections;
import java.util.Iterator;
import java.util.Locale;
Expand All @@ -47,6 +48,14 @@
public class DependencyUpdatesReport
extends AbstractVersionsReport
{

/**
* Report formats (html and/or xml).
* HTML by default.
* @parameter expression="${versions.report.formats}"
*/
private String[] formats = new String[] { "html" };

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -86,10 +95,25 @@ protected void doGenerateReport( Locale locale, Sink sink )
getHelper().lookupDependenciesUpdates( dependencies, false );
Map<Dependency, ArtifactVersions> dependencyManagementUpdates =
getHelper().lookupDependenciesUpdates( dependencyManagement, false );
DependencyUpdatesRenderer renderer =
new DependencyUpdatesRenderer( sink, getI18n(), getOutputName(), locale, dependencyUpdates,
dependencyManagementUpdates );
renderer.render();
for (String format : formats)
{
if ("html".equals(format))
{
DependencyUpdatesRenderer renderer = new DependencyUpdatesRenderer(sink, getI18n(), getOutputName(),
locale,
dependencyUpdates,
dependencyManagementUpdates);
renderer.render();

} else if ("xml".equals(format))
{
String outputFile = getProject().getBuild().getDirectory() + File.separator + getOutputName()
+ ".xml";
DependencyUpdatesXmlRenderer xmlGenerator = new DependencyUpdatesXmlRenderer(dependencyUpdates,
dependencyManagementUpdates, outputFile);
xmlGenerator.render();
}
}
}
catch ( InvalidVersionSpecificationException e )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
package org.codehaus.mojo.versions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.model.Dependency;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.UpdateScope;
import org.codehaus.mojo.versions.utils.DependencyComparator;

/**
* XML renderer for DependencyUpdatesReport creates an xml file in target directory and writes report about available
* dependency/dependency management updates.
* @author Illia Dubinin
* @since 2.4
*
*/
public class DependencyUpdatesXmlRenderer
{

private static final String GROUP_ID = "groupId";
private static final String ARTIFACT_ID = "artifactId";
private static final String SCOPE = "scope";
private static final String CLASSIFIER = "classifier";
private static final String TYPE = "type";

private static final String CURRENT_VERSION = "currentVersion";
private static final String NEXT_VERSION = "nextVersion";

private static final String STATUS = "status";

private static final String OPEN_TAG = "<";
private static final String CLOSE_TAG = ">";
private static final String OPEN_CLOSING_TAG = "</";

private static final String NL = "\n";
private static final String TAB = "\t";

private Map<Dependency, ArtifactVersions> dependencyUpdates;
private Map<Dependency, ArtifactVersions> dependencyManagementUpdates;
private String outputFileName;

public DependencyUpdatesXmlRenderer(Map<Dependency, ArtifactVersions> dependencyUpdates,
Map<Dependency, ArtifactVersions> dependencyManagementUpdates, String outputFileName) {
this.dependencyUpdates = dependencyUpdates;
this.dependencyManagementUpdates = dependencyManagementUpdates;
this.outputFileName = outputFileName;
}

/**
* Makes report file with given name in target directory.
* @throws MavenReportException if something went wrong
*/
public void render() throws MavenReportException
{
StringBuilder sb = new StringBuilder();
sb.append("<DependencyUpdatesReport>").append(NL);
Map<Dependency, ArtifactVersions> allUpdates = new TreeMap<Dependency, ArtifactVersions>(
new DependencyComparator());
allUpdates.putAll(dependencyManagementUpdates);
allUpdates.putAll(dependencyUpdates);
sb.append(getSummaryBlock(allUpdates.values()));
sb.append(getDependencyInfoBlock(dependencyManagementUpdates, "dependencyManagements",
"dependencyManagement"));
sb.append(getDependencyInfoBlock(dependencyUpdates, "dependencies", "dependency"));
sb.append("</DependencyUpdatesReport>").append(NL);
PrintWriter pw;
try
{
pw = new PrintWriter(outputFileName, "UTF8");
pw.print(sb.toString());
pw.close();
} catch (IOException e)
{
throw new MavenReportException("Cannot create xml report.", e);
}
}

/**
* Method wraps value in xml tag.
* In ex: to wrap foo in tag bar you have to pass foo as value and bar as tag.
* As a result you will get: <bar>foo</bar>
* @param value - string to wrap
* @param tag - name of tag
* @return value wrapped in xml tag
*/

public static String wrapElement(String value, String tag)
{
return OPEN_TAG + tag + CLOSE_TAG + value + OPEN_CLOSING_TAG + tag + CLOSE_TAG;
}

/**
* Returns summary of dependency analysis result in xml format: current version, next available,
* next incremental, next minor and next major versions.
* @param allUpdates all dependencies versions
* @return summary in xml format
*/
public static String getSummaryBlock(Collection<ArtifactVersions> allUpdates)
{
int numInc = 0;
int numMin = 0;
int numMaj = 0;
int numAny = 0;
int numCur = 0;
for (ArtifactVersions details : allUpdates)
{
if (details.getOldestUpdate(UpdateScope.SUBINCREMENTAL) != null)
{
numAny++;
} else if (details.getOldestUpdate(UpdateScope.INCREMENTAL) != null)
{
numInc++;
} else if (details.getOldestUpdate(UpdateScope.MINOR) != null)
{
numMin++;
} else if (details.getOldestUpdate(UpdateScope.MAJOR) != null)
{
numMaj++;
} else
{
numCur++;
}
}

String result = "\t<summary>%n"
+ "\t\t<usingLastVersion>%d</usingLastVersion>%n"
+ "\t\t<nextVersionAlailable>%d</nextVersionAlailable>%n"
+ "\t\t<nextIncremetalAvailable>%d</nextIncremetalAvailable>%n"
+ "\t\t<nextMinorAvailable>%d</nextMinorAvailable>%n"
+ "\t\t<nextMajorAvailable>%d</nextMajorAvailable>%n"
+ "\t</summary>%n";

return String.format(result, numCur, numAny, numInc, numMin, numMaj);
}

/**
* Returns xml report for current dependency state with following info: current version, next available version,
* next incremental/minor/major if available and status ('incremental available', 'minor available',
* 'major available' or 'no new available')
* @param versions version info for dependency
* @return xml reports about current possible updates.
*/
public static String getVersionsBlocks(ArtifactVersions versions)
{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(TAB).append(TAB).append(TAB)
.append(wrapElement(versions.getCurrentVersion().toString(), CURRENT_VERSION)).append(NL);
ArtifactVersion nextVersion = versions.getOldestUpdate(UpdateScope.ANY);
if (nextVersion != null)
{
sBuilder.append(TAB).append(TAB).append(TAB)
.append(wrapElement(nextVersion.toString(), NEXT_VERSION)).append(NL);

String incrementalsBlock = getVersionsInScopeBlock(versions, UpdateScope.INCREMENTAL);
sBuilder.append(incrementalsBlock);
String minorsBlock = getVersionsInScopeBlock(versions, UpdateScope.MINOR);
sBuilder.append(minorsBlock);
String majorsBlock = getVersionsInScopeBlock(versions, UpdateScope.MAJOR);
sBuilder.append(majorsBlock);

String status = null;
if (incrementalsBlock.length() > 0)
{
status = "incremental available";
} else if (minorsBlock.length() > 0)
{
status = "minor available";
} else if (majorsBlock.length() > 0)
{
status = "major available";
}

sBuilder.append(TAB).append(TAB).append(TAB)
.append(wrapElement(status, STATUS)).append(NL);
} else
{
sBuilder.append(TAB).append(TAB).append(TAB)
.append(wrapElement("no new available", STATUS)).append(NL);
}

return sBuilder.toString();
}

private static String getDependencyInfoBlock(Map<Dependency, ArtifactVersions> dependencyUpdates,
String blockName, String subblockName)
{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(TAB).append(OPEN_TAG).append(blockName).append(CLOSE_TAG).append(NL);
for (Entry<Dependency, ArtifactVersions> entry : dependencyUpdates.entrySet())
{
sBuilder.append(TAB).append(TAB)
.append(OPEN_TAG).append(subblockName).append(CLOSE_TAG).append(NL);

Dependency dep = entry.getKey();
sBuilder.append(TAB).append(TAB).append(TAB)
.append(wrapElement(dep.getGroupId(), GROUP_ID)).append(NL);
sBuilder.append(TAB).append(TAB).append(TAB)
.append(wrapElement(dep.getArtifactId(), ARTIFACT_ID)).append(NL);
sBuilder.append(TAB).append(TAB).append(TAB)
.append(wrapElement(dep.getScope(), SCOPE)).append(NL);
sBuilder.append(TAB).append(TAB).append(TAB)
.append(wrapElement(dep.getClassifier(), CLASSIFIER)).append(NL);
sBuilder.append(TAB).append(TAB).append(TAB)
.append(wrapElement(dep.getType(), TYPE)).append(NL);

sBuilder.append(getVersionsBlocks(entry.getValue()));

sBuilder.append(TAB).append(TAB)
.append(OPEN_CLOSING_TAG).append(subblockName).append(CLOSE_TAG).append(NL);
}
sBuilder.append(TAB).append(OPEN_CLOSING_TAG).append(blockName).append(CLOSE_TAG).append(NL);
return sBuilder.toString();
}

private static String getVersionsInScopeBlock(ArtifactVersions av, UpdateScope scope)
{
String versionsTag = scope.toString().toLowerCase() + "s";
StringBuilder sBuilder = new StringBuilder();

ArtifactVersion nextVersion = av.getOldestUpdate(scope);
if (nextVersion != null)
{
sBuilder.append(TAB).append(TAB).append(TAB)
.append(OPEN_TAG).append(versionsTag).append(CLOSE_TAG).append(NL);
ArtifactVersion[] versions = av.getAllUpdates(scope);
for (ArtifactVersion version : versions)
{
sBuilder.append(TAB).append(TAB).append(TAB).append(TAB)
.append(wrapElement(version.toString(), scope.toString().toLowerCase())).append(NL);
}
sBuilder.append(TAB).append(TAB).append(TAB)
.append(OPEN_CLOSING_TAG).append(versionsTag).append(CLOSE_TAG).append(NL);
}

return sBuilder.toString();
}

}
28 changes: 25 additions & 3 deletions src/main/java/org/codehaus/mojo/versions/PluginUpdatesReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.codehaus.mojo.versions.utils.PluginComparator;
import org.codehaus.plexus.util.StringUtils;

import java.io.File;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
Expand All @@ -44,6 +45,14 @@
public class PluginUpdatesReport
extends AbstractVersionsReport
{

/**
* Report formats (html and/or xml).
* HTML by default.
* @parameter
*/
private String[] formats = new String[] { "html" };

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -102,9 +111,22 @@ protected void doGenerateReport( Locale locale, Sink sink )
getHelper().lookupPluginsUpdates( plugins, getAllowSnapshots() );
Map<Plugin, PluginUpdatesDetails> pluginManagementUpdates =
getHelper().lookupPluginsUpdates( pluginManagement, getAllowSnapshots() );
PluginUpdatesRenderer renderer = new PluginUpdatesRenderer( sink, getI18n(), getOutputName(), locale,
pluginUpdates, pluginManagementUpdates );
renderer.render();
for (String format : formats)
{
if ("html".equals(format))
{
PluginUpdatesRenderer renderer = new PluginUpdatesRenderer(sink, getI18n(), getOutputName(), locale,
pluginUpdates, pluginManagementUpdates);
renderer.render();
} else if ("xml".equals(format))
{
String outputFile = getProject().getBuild().getDirectory() + File.separator + getOutputName()
+ ".xml";
PluginUpdatesXmlRenderer xmlGenerator = new PluginUpdatesXmlRenderer(pluginUpdates,
pluginManagementUpdates, outputFile);
xmlGenerator.render();
}
}
}
catch ( InvalidVersionSpecificationException e )
{
Expand Down
Loading