Skip to content

Commit

Permalink
Allow usage of regular expressions to resolve snapshot dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ascheman committed Sep 23, 2016
1 parent cb9ebec commit 7f4f7af
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.WriterFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.codehaus.plexus.util.WriterFactory;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PomUpdater {

Expand Down Expand Up @@ -93,6 +94,21 @@ public boolean success() {
}
}

/**
* Check whether the given dependencySnapshot is matched by the resolveSnapshotPattern (regular expression).
*
* @param dependencySnapshot a dependency string in a gradle like notation, i.e., groupId:artifactId:version
* @param resolveSnapshotPattern a regular expression pattern which is used in the plugin configuration, e.g.,
* ^org\.slf4j:slf4j-api:1\.7\..*
* @return if the dependency string matches the regex
*/
protected static boolean snapshotResolves(String dependencySnapshot, String resolveSnapshotPattern) {
Pattern resolveSnapshotPatternAsPattern = Pattern.compile(resolveSnapshotPattern);
Matcher matcher = resolveSnapshotPatternAsPattern.matcher(dependencySnapshot);

return matcher.matches();
}

private List<String> alterModel(MavenProject project, String newVersion) {
Model originalModel = project.getOriginalModel();
originalModel.setVersion(newVersion);
Expand Down Expand Up @@ -120,13 +136,13 @@ private List<String> alterModel(MavenProject project, String newVersion) {
} catch (UnresolvedSnapshotDependencyException e) {
boolean resolveSnapshotFound = false;
if (null != resolveSnapshots) {
String snapshot = dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + version;
log.debug(" Trying to resolve snapshot: '" + snapshot + "' with ...");
String dependencySnapshot = dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + version;
log.debug(" Trying to resolve snapshot: '" + dependencySnapshot + "' with ...");
for (String resolveSnapshot : resolveSnapshots) {
log.debug(" ...: '" + resolveSnapshot + "'");
if (snapshot.equals(resolveSnapshot)) {
if (snapshotResolves(dependencySnapshot, resolveSnapshot)) {
String resolvedVersion = resolveSnapshotDependency (dependency);
log.debug(" Resolving snapshot dependency: '" + snapshot + "' to '" + resolvedVersion + "'");
log.debug(" Resolving snapshot dependency: '" + dependencySnapshot + "' to '" + resolvedVersion + "'");
if (null != resolvedVersion) {
resolveSnapshotFound = true;
dependency.setVersion(resolvedVersion);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.danielflower.mavenplugins.release;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

/*
In fact, this is currently just a test for Java pattern matching ... Hence, you could easily add much more patterns,
but would hardly increase the module quality. In the end the plugin user has to provide the right pattern
configuration.
*/
public class PomUpdaterTest {

@Test
public void testGroupIdPrefix () {
assertTrue ("groupIdPrefix",
PomUpdater.snapshotResolves("net.aschemann.test:test-module:1.3-SNAPSHOT", "^net\\.aschemann.*"));
}

@Test
public void testArtifactIdInfix () {
assertTrue ("artifactIdInfix",
PomUpdater.snapshotResolves("net.aschemann.test:test-module:1.3-SNAPSHOT", "^net\\.aschemann.*:.*st-modu.*:.*"));
}

}

0 comments on commit 7f4f7af

Please sign in to comment.