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 Mar 29, 2019
1 parent e54a961 commit e1cff97
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import java.io.File;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PomUpdater {

Expand Down Expand Up @@ -92,6 +95,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 @@ -121,13 +139,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.*:.*"));
}

}
2 changes: 1 addition & 1 deletion src/test/java/scaffolding/TestProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class TestProject {

private static final MvnRunner defaultRunner = new MvnRunner(null);
private static final String PLUGIN_VERSION_FOR_TESTS = "3.0-SNAPSHOT";
private static final String PLUGIN_VERSION_FOR_TESTS = "3.1-SNAPSHOT";

public final File originDir;
public final Git origin;
Expand Down

0 comments on commit e1cff97

Please sign in to comment.