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

Feature/full wildcard support #68

Merged
merged 11 commits into from
Jun 10, 2019
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
</developer>
</developers>

<contributors>
<contributor>
<name>Chuckame</name>
<email>[email protected]</email>
</contributor>
</contributors>

<dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,42 @@ public class ArtifactFilter {
protected String classifier;

public boolean match(Artifact artifact) {
boolean artifactMatch = WILDCARD.equals(artifactId) || artifact.getArtifactId().equals(this.artifactId) ||
(artifactId != null && getMatcher(artifact).matches());
boolean groupMatch = artifact.getGroupId().equals(this.groupId);
boolean artifactMatch = WILDCARD.equals(this.artifactId) || artifact.getArtifactId().equals(this.artifactId) ||
(artifactId != null && getArtifactIdMatcher(artifact).matches());
boolean groupMatch = artifact.getGroupId().equals(this.groupId) ||
(groupId != null && getGroupIdMatcher(artifact).matches());
boolean classifierMatch = ((this.classifier == null) && (artifact.getClassifier() == null)) || ((this.classifier != null) && this.classifier.equals(artifact.getClassifier()));
return artifactMatch && groupMatch && classifierMatch;
}

private Matcher getMatcher(Artifact artifact) {
private Matcher getArtifactIdMatcher(Artifact artifact) {
try {
Pattern compile = Pattern.compile(artifactId);
Pattern compile = Pattern.compile(escapeRegex(this.artifactId));
return compile.matcher(artifact.getArtifactId());
} catch (PatternSyntaxException e) {
throw new IllegalArgumentException("Invalid regex artifact filter: " + this, e);
throw new IllegalArgumentException("Invalid regex artifactId filter: " + this, e);
}
}

private Matcher getGroupIdMatcher(Artifact artifact) {
try {
Pattern compile = Pattern.compile(escapeRegex(this.groupId));
return compile.matcher(artifact.getGroupId());
} catch (PatternSyntaxException e) {
throw new IllegalArgumentException("Invalid regex groupId filter: " + this, e);
}
}

/**
* Escape regex and keep wildcard.<br>
* {@link Pattern#quote(String)} method wrap string between '\Q' for starting ignoring and '\E' for ending ignoring,<br>
* so we don't want to escape wildcard.<br>
* 'myregexpart1*myregexpart2' becomes '\Qmyregexpart1\E.*\Qmyregexpart2\E'.
*/
private String escapeRegex(String str) {
return Pattern.quote(str).replace(WILDCARD, "\\E.*\\Q");
}

@Override
public String toString() {
return "groupId:" + groupId + ", artifactId:" + artifactId + ", classifier:" + classifier;
Expand Down
107 changes: 63 additions & 44 deletions src/main/java/com/github/wvengen/maven/proguard/ProGuardMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.maven.archiver.MavenArchiveConfiguration;
Expand Down Expand Up @@ -448,39 +451,55 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

Set<String> inPath = new HashSet<String>();
Map<Artifact, Inclusion> injars = new HashMap<Artifact, Inclusion>();
Map<Artifact, Inclusion> libraryjars = new HashMap<Artifact, Inclusion>();
boolean hasInclusionLibrary = false;
if (assembly != null && assembly.inclusions != null) {
for (Inclusion inc : assembly.inclusions) {
if (!inc.library) {
File file = getClasspathElement(getDependency(inc, mavenProject), mavenProject);
inPath.add(file.toString());
log.debug("--- ADD injars:" + inc.artifactId);
StringBuilder filter = new StringBuilder(fileToString(file));
filter.append("(!META-INF/MANIFEST.MF");
if (!addMavenDescriptor) {
filter.append(",");
filter.append("!META-INF/maven/**");
}
if (inc.filter != null) {
filter.append(",").append(inc.filter);
}
filter.append(")");
args.add("-injars");
args.add(filter.toString());
} else {
hasInclusionLibrary = true;
log.debug("--- ADD libraryjars:" + inc.artifactId);
// This may not be CompileArtifacts, maven 2.0.6 bug
File file = getClasspathElement(getDependency(inc, mavenProject), mavenProject);
inPath.add(file.toString());
if(putLibraryJarsInTempDir){
libraryJars.add(file);
for (Artifact artifact : getDependencies(inc, mavenProject)) {
if (inc.library) {
if (!injars.containsKey(artifact)) {
libraryjars.put(artifact, inc);
}
} else {
args.add("-libraryjars");
args.add(fileToString(file));
injars.put(artifact, inc);
if (libraryjars.containsKey(artifact)) {
libraryjars.remove(artifact);
}
}
}
}

for (Entry<Artifact, Inclusion> entry : injars.entrySet()) {
log.info("--- ADD injars:" + entry.getKey().getArtifactId());
File file = getClasspathElement(entry.getKey(), mavenProject);
inPath.add(file.toString());
StringBuilder filter = new StringBuilder(fileToString(file));
filter.append("(!META-INF/MANIFEST.MF");
if (!addMavenDescriptor) {
filter.append(",");
filter.append("!META-INF/maven/**");
}
if (entry.getValue().filter != null) {
filter.append(",").append(entry.getValue().filter);
}
filter.append(")");
args.add("-injars");
args.add(filter.toString());
}

for (Entry<Artifact, Inclusion> entry : libraryjars.entrySet()) {
log.info("--- ADD libraryjars:" + entry.getKey().getArtifactId());
File file = getClasspathElement(entry.getKey(), mavenProject);
hasInclusionLibrary = true;
inPath.add(file.toString());
if (putLibraryJarsInTempDir) {
libraryJars.add(file);
} else {
args.add("-libraryjars");
args.add(fileToString(file));
}
}
}

if (inJarFile.exists()) {
Expand Down Expand Up @@ -641,18 +660,15 @@ public void execute() throws MojoExecutionException, MojoFailureException {

try {
jarArchiver.addArchivedFileSet(baseFile);
for (Inclusion inc : assembly.inclusions) {
if (inc.library) {
File file;
Artifact artifact = getDependency(inc, mavenProject);
file = getClasspathElement(artifact, mavenProject);
if (file.isDirectory()) {
getLog().info("merge project: " + artifact.getArtifactId() + " " + file);
jarArchiver.addDirectory(file);
} else {
getLog().info("merge artifact: " + artifact.getArtifactId());
jarArchiver.addArchivedFileSet(file);
}
for (Entry<Artifact, Inclusion> entry : libraryjars.entrySet()) {
File file;
file = getClasspathElement(entry.getKey(), mavenProject);
if (file.isDirectory()) {
getLog().info("merge project: " + entry.getKey() + " " + file);
jarArchiver.addDirectory(file);
} else {
getLog().info("merge artifact: " + entry.getKey());
jarArchiver.addArchivedFileSet(file);
}
}

Expand Down Expand Up @@ -841,16 +857,19 @@ private boolean deleteFileOrDirectory(File path) throws MojoFailureException {
}
}


private Artifact getDependency(Inclusion inc, MavenProject mavenProject) throws MojoExecutionException {
private Set<Artifact> getDependencies(final Inclusion inc, MavenProject mavenProject) throws MojoExecutionException {
@SuppressWarnings("unchecked")
Set<Artifact> dependency = mavenProject.getArtifacts();
for (Artifact artifact : dependency) {
Set<Artifact> dependencies = mavenProject.getArtifacts();
Set<Artifact> result = new HashSet<Artifact>();
for (Artifact artifact : dependencies) {
if (inc.match(artifact)) {
return artifact;
result.add(artifact);
}
}
throw new MojoExecutionException("artifactId Not found " + inc.artifactId);
if (result.isEmpty()) {
log.warn(String.format("No artifact found : %s:%s", inc.artifactId, inc.groupId));
}
return result;
}

private boolean isExclusion(Artifact artifact) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.wvengen.maven.proguard;


import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.versioning.VersionRange;
Expand All @@ -12,56 +13,83 @@ public class ArtifactFilterTest {
private ArtifactFilter artifactFilter = new ArtifactFilter();

@Test
public void wildcardMatch() {
public void noMatch() {
artifactFilter.groupId = "com.mahifx";
artifactFilter.artifactId = "*";
Assert.assertTrue(artifactFilter.match(getArtifact()));
artifactFilter.artifactId = "libB";
Assert.assertFalse(artifactFilter.match(getArtifact()));
}

@Test
public void noMatch() {
public void emptyArtifactDoesNotMatch() {
artifactFilter.groupId = "com.mahifx";
artifactFilter.artifactId = "libB";
artifactFilter.artifactId = "";
Assert.assertFalse(artifactFilter.match(getArtifact()));
}

@Test
public void noMatchWithRegexTokens() {
public void wildcardMatch_allArtifact() {
artifactFilter.groupId = "com.mahifx";
artifactFilter.artifactId = "libB-utils";
Assert.assertFalse(artifactFilter.match(getArtifact()));
artifactFilter.artifactId = "*";
Assert.assertTrue(artifactFilter.match(getArtifact()));
}

@Test
public void regexMatch() {
public void wildcardMatch_partOfArtifact() {
artifactFilter.groupId = "com.mahifx";
artifactFilter.artifactId = "lib.*";
artifactFilter.artifactId = "lib*";
Assert.assertTrue(artifactFilter.match(getArtifact()));
}

@Test
public void regexNoMatch() {
public void wildcardMatch_escapeArtifactDots() {
artifactFilter.groupId = "com.mahifx";
artifactFilter.artifactId = "foo.+";
Assert.assertFalse(artifactFilter.match(getArtifact()));
artifactFilter.artifactId = "li.*";
Artifact artifact = getArtifact();
artifact.setArtifactId("li.b");
Assert.assertTrue(artifactFilter.match(artifact));
}

@Test
public void emptyArtifactDoesNotMatch() {
public void wildcardNoMatch_escapeArtifactDots() {
artifactFilter.groupId = "com.mahifx";
artifactFilter.artifactId = "";
artifactFilter.artifactId = "li.b";
Assert.assertFalse(artifactFilter.match(getArtifact("com.mahifx", "liTb")));
}

@Test
public void noMatchWithRegexTokens() {
artifactFilter.groupId = "com.mahifx";
artifactFilter.artifactId = "libB-utils";
Assert.assertFalse(artifactFilter.match(getArtifact()));
}

@Test(expected = IllegalArgumentException.class)
public void invalidRegex() {
@Test
public void wildcardMatch_partOfGroupId() {
artifactFilter.groupId = "com.ma*";
artifactFilter.artifactId = "libA";
Assert.assertTrue(artifactFilter.match(getArtifact()));
}

@Test
public void simpleMatch() {
artifactFilter.groupId = "com.mahifx";
artifactFilter.artifactId = "+";
artifactFilter.match(getArtifact());
artifactFilter.artifactId = "libA";
Assert.assertTrue(artifactFilter.match(getArtifact()));
}

@Test
public void wildcardMatch_subGroup() {
artifactFilter.groupId = "com.mahifx.*";
artifactFilter.artifactId = "libA";
Assert.assertTrue(artifactFilter.match(getArtifact("com.mahifx.subgroup", "libA")));
Assert.assertTrue(artifactFilter.match(getArtifact("com.mahifx.subgroup.subsubgroup", "libA")));
}

private DefaultArtifact getArtifact() {
return new DefaultArtifact("com.mahifx", "libA", VersionRange.createFromVersion("1.0.0"), "compile", "jar", null, new DefaultArtifactHandler());
return getArtifact("com.mahifx", "libA");
}

private DefaultArtifact getArtifact(String groupId, String artifactId) {
return new DefaultArtifact(groupId, artifactId, VersionRange.createFromVersion("1.0.0"), "compile", "jar", null, new DefaultArtifactHandler());
}
}