Skip to content

Commit

Permalink
Introduce Plugin API for ChangeRecorder
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Nov 20, 2022
1 parent 7e7ed21 commit 63b7074
Show file tree
Hide file tree
Showing 59 changed files with 824 additions and 401 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@
</contributors>

<modules>
<module>versions-maven-plugin</module>
<module>model-ruleset</module>
<module>model-report</module>
<module>model-ruleset</module>
<module>versions-api</module>
<module>versions-common</module>
<module>versions-maven-plugin</module>
</modules>

<scm>
Expand Down
15 changes: 15 additions & 0 deletions versions-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.codehaus.mojo.versions</groupId>
<artifactId>versions</artifactId>
<version>2.14.0-SNAPSHOT</version>
</parent>

<artifactId>versions-api</artifactId>
<name>Versions API</name>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.codehaus.mojo.versions.api.change;

public interface VersionChange
{

String getGroupId();

String getArtifactId();

String getOldVersion();

String getNewVersion();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.codehaus.mojo.versions.api.recording;

import org.codehaus.mojo.versions.api.change.VersionChange;

public interface ChangeRecord
{
enum ChangeKind
{
COMPARE_DEPENDENCIES( "compareDependencies" ),
FORCE_RELEASES( "forceReleases" ),
UNLOCK_PARENT_VERSION( "unlockParentVersion" ),
UNLOCK_SNAPSHOT( "unlockSnapshot" ),
UPDATE_PARENT( "updateParent" ),
UPDATE_PROPERTY( "updateProperty" ),
USE_DEPENDENCY_VERSION( "useDependencyVersion" ),
USE_LATEST_RELEASES( "useLatestReleases" ),
USE_LATEST_SNAPSHOTS( "useLatestSnapshots" ),
USE_LATEST_VERSIONS( "useLatestVersions" ),
USE_NEXT_RELEASES( "useNextReleases" ),
USE_NEXT_SNAPSHOTS( "useNextSnapshots" ),
USE_NEXT_VERSIONS( "useNextVersions" ),
USE_RELEASES( "useReleases" );

private final String label;

ChangeKind( String label )
{
this.label = label;
}

public String getLabel()
{
return label;
}
}

ChangeKind getKind();

VersionChange getVersionChange();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.codehaus.mojo.versions.recording;
package org.codehaus.mojo.versions.api.recording;

/*
* Licensed to the Apache Software Foundation (ASF) under one
Expand All @@ -20,7 +20,7 @@
*/

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;

/**
* A recorder of version updates.
Expand All @@ -31,21 +31,21 @@ public interface ChangeRecorder
/**
* Record that a dependency was updated.
*
* @param kind The kind of version change
* @param groupId The dependency group ID
* @param artifactId The dependency artifact ID
* @param oldVersion The old version of the dependency
* @param newVersion The new version of the dependency
* @param changeRecord a record described change
*/

void recordUpdate( String kind, String groupId, String artifactId, String oldVersion, String newVersion );
void recordUpdate( ChangeRecord changeRecord );

/**
* Serialize the current set of changes to the given output stream.
* Serialize the current set of changes to the given output path.
* <p>
* Implementation is responsible for creating all missing directories.
* <p>
* Output should not be created for empty record sets.
*
* @param outputStream The output stream
* @param outputPath The output path, can be null
* @throws IOException On serialization and/or I/O errors
*/

void serialize( OutputStream outputStream ) throws IOException;
void serialize( Path outputPath ) throws IOException;
}
22 changes: 22 additions & 0 deletions versions-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<description>Common components for the Versions Maven Plugin</description>

<dependencies>
<dependency>
<groupId>org.codehaus.mojo.versions</groupId>
<artifactId>versions-api</artifactId>
<version>2.14.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo.versions</groupId>
<artifactId>model-ruleset</artifactId>
Expand Down Expand Up @@ -120,4 +125,21 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
<version>0.9.0.M1</version>
<executions>
<execution>
<id>generate-index</id>
<goals>
<goal>main-index</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Arrays;
import java.util.List;

import org.codehaus.mojo.versions.api.change.VersionChange;

/**
* Created by IntelliJ IDEA.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

import java.util.Objects;

import org.codehaus.mojo.versions.api.change.VersionChange;

/**
* Represents a change of an artifact's version.
*
* @author Stephen Connolly
* @since 15-Sep-2010 14:48:10
*/
public final class VersionChange
public final class DefaultVersionChange implements VersionChange
{
private final String groupId;

Expand All @@ -37,7 +39,7 @@ public final class VersionChange

private final String newVersion;

public VersionChange( String groupId, String artifactId, String oldVersion, String newVersion )
public DefaultVersionChange( String groupId, String artifactId, String oldVersion, String newVersion )
{
this.groupId = groupId;
this.artifactId = artifactId;
Expand Down Expand Up @@ -76,7 +78,7 @@ public boolean equals( Object o )
return false;
}

VersionChange versionChange = (VersionChange) o;
DefaultVersionChange versionChange = (DefaultVersionChange) o;

if ( !Objects.equals( artifactId, versionChange.artifactId ) )
{
Expand Down Expand Up @@ -104,6 +106,6 @@ public int hashCode()

public String toString()
{
return "VersionChange(" + groupId + ':' + artifactId + ":" + oldVersion + "-->" + newVersion + ')';
return "DefaultVersionChange(" + groupId + ':' + artifactId + ":" + oldVersion + "-->" + newVersion + ')';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.maven.model.Model;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.change.VersionChange;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.maven.model.Model;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.change.VersionChange;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.maven.model.Model;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.change.VersionChange;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.maven.model.Model;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.change.VersionChange;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import javax.xml.stream.XMLStreamException;

import org.codehaus.mojo.versions.api.change.VersionChange;

/**
* Created by IntelliJ IDEA.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
* under the License.
*/

import java.io.OutputStream;
import javax.inject.Named;

import java.nio.file.Path;

import org.codehaus.mojo.versions.api.recording.ChangeRecord;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;

/**
* A recorder that ignores updates.
*/

@Named( "none" )
public class ChangeRecorderNull implements ChangeRecorder
{
/**
Expand All @@ -35,13 +40,12 @@ public ChangeRecorderNull()
}

@Override
public final void recordUpdate( final String kind, final String groupId, final String artifactId,
final String oldVersion, final String newVersion )
public final void recordUpdate( ChangeRecord changeRecord )
{
}

@Override
public final void serialize( final OutputStream outputStream )
public final void serialize( final Path outputPath )
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* under the License.
*/

import javax.inject.Named;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand All @@ -31,16 +33,24 @@

import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;
import java.nio.file.Files;
import java.nio.file.Path;

import org.codehaus.mojo.versions.api.recording.ChangeRecord;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;

/**
* A recorder of version updates.
*/

@Named( "xml" )
public class ChangeRecorderXML implements ChangeRecorder
{
/**
Expand Down Expand Up @@ -72,31 +82,40 @@ public ChangeRecorderXML()
}

@Override
public final void recordUpdate( final String kind, final String groupId, final String artifactId,
final String oldVersion, final String newVersion )
public final void recordUpdate( ChangeRecord changeRecord )
{
Objects.requireNonNull( kind, "kind" );
Objects.requireNonNull( groupId, "groupId" );
Objects.requireNonNull( artifactId, "artifactId" );
Objects.requireNonNull( oldVersion, "oldVersion" );
Objects.requireNonNull( newVersion, "newVersion" );

final Element update = this.document.createElementNS( CHANGES_NAMESPACE, "update" );
update.setAttribute( "kind", kind );
update.setAttribute( "groupId", groupId );
update.setAttribute( "artifactId", artifactId );
update.setAttribute( "oldVersion", oldVersion );
update.setAttribute( "newVersion", newVersion );
update.setAttribute( "kind", changeRecord.getKind().getLabel() );
update.setAttribute( "groupId", changeRecord.getVersionChange().getGroupId() );
update.setAttribute( "artifactId", changeRecord.getVersionChange().getArtifactId() );
update.setAttribute( "oldVersion", changeRecord.getVersionChange().getOldVersion() );
update.setAttribute( "newVersion", changeRecord.getVersionChange().getNewVersion() );
this.root.appendChild( update );
}

@Override
public final void serialize( final OutputStream outputStream ) throws IOException
public final void serialize( final Path outputPath ) throws IOException
{
try
if ( outputPath == null )
{
throw new IOException( "changeRecorderOutputFile not provided" );
}

if ( root.getChildNodes().getLength() == 0 )
{
// don't generate empty file
return;
}

Files.createDirectories( outputPath.getParent() );

try ( OutputStream outputStream = Files.newOutputStream( outputPath, CREATE, TRUNCATE_EXISTING, WRITE ) )
{
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
final Source source = new DOMSource( this.document );
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute( XMLConstants.ACCESS_EXTERNAL_DTD, "" );
transformerFactory.setAttribute( XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "" );
Transformer transformer = transformerFactory.newTransformer();
Source source = new DOMSource( this.document );
transformer.transform( source, new StreamResult( outputStream ) );
outputStream.flush();
}
Expand Down
Loading

0 comments on commit 63b7074

Please sign in to comment.