Skip to content

Commit

Permalink
remove direct guava usage
Browse files Browse the repository at this point in the history
The only direct guava usage was a single function, so reimplented.

This prevents the downlaod of an extra dependency when running in a
clean environment like CI.
  • Loading branch information
jtnord committed Mar 15, 2023
1 parent 87d8540 commit b4d46b4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 10 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<dependency>
<groupId>com.sun.codemodel</groupId>
<artifactId>codemodel</artifactId>
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/jenkinsci/maven/plugins/hp/util/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jenkinsci.maven.plugins.hp.util;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Utils {

/**
* Return an unmodifiable set whose contents are the unions of all the specified sets.
*/
@SafeVarargs
public static <T> Set<T> unionOf(Set<T>... sets) {
Set<T> unionSet = new HashSet<>(sets[0]);
for (int i = 1; i < sets.length; i++) {
unionSet.addAll(sets[i]);
}
return Collections.unmodifiableSet(unionSet);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* limitations under the License.
*/

import com.google.common.collect.Sets;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.Extension;
import java.io.BufferedReader;
Expand All @@ -35,6 +34,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -63,6 +63,7 @@
import org.codehaus.plexus.util.InterpolationFilterReader;
import org.codehaus.plexus.util.PropertyUtils;
import org.codehaus.plexus.util.StringUtils;
import org.jenkinsci.maven.plugins.hp.util.Utils;

public abstract class AbstractHpiMojo extends AbstractJenkinsMojo {
/**
Expand Down Expand Up @@ -509,7 +510,7 @@ public void buildWebapp(MavenProject project, File webappDirectory)
// List up IDs of Jenkins plugin dependencies
Set<String> jenkinsPlugins = new HashSet<>();
Set<String> excludedArtifacts = new HashSet<>();
for (MavenArtifact artifact : Sets.union(artifacts, dependencyArtifacts)) {
for (MavenArtifact artifact : Utils.unionOf(artifacts, dependencyArtifacts)) {
if (artifact.isPluginBestEffort(getLog()))
jenkinsPlugins.add(artifact.getId());
// Exclude dependency if it comes from test or provided trail.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.jenkinsci.maven.plugins.hpi;

import com.google.common.collect.Sets;
import hudson.util.VersionNumber;
import java.io.File;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

import org.jenkinsci.maven.plugins.hp.util.Utils;
import java.io.IOException;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
Expand All @@ -24,7 +23,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
MavenArtifact maxCoreVersionArtifact = null;
VersionNumber maxCoreVersion = new VersionNumber("0");

for (MavenArtifact artifact : Sets.union(getProjectArtfacts(), getDirectDependencyArtfacts())) {
for (MavenArtifact artifact : Utils.unionOf(getProjectArtfacts(), getDirectDependencyArtfacts())) {
try {
if (artifact.isPluginBestEffort(getLog())) {
VersionNumber dependencyCoreVersion = getDependencyCoreVersion(artifact);
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/org/jenkinsci/maven/plugins/hpi/util/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.jenkinsci.maven.plugins.hpi.util;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import java.util.Collections;
import java.util.Set;
import org.jenkinsci.maven.plugins.hp.util.Utils;
import org.junit.Test;

public class UtilsTest {

@Test
public void setUnionCalculation() {
checkUnion("union of a set is itself",
new String[] {"one", "two", "three"},
Set.of("one", "two", "three"));

checkUnion("union of 2 sets is correct",
new String[] {"one", "two", "three", "four", "five", "six"},
Set.of("one", "two", "three"),
Set.of("four", "five", "six"));

checkUnion("union of 3 sets is correct",
new String[] {"one", "two", "three", "four", "five", "six", "seven"},
Set.of("one", "three", "five"),
Set.of("two", "four", "six"),
Set.of("one", "seven"));

checkUnion("union of overlapping sets",
new String[] {"one", "two", "three"},
Set.of("one", "two", "three"),
Set.of("one", "two", "three"),
Set.of("one", "two", "three"),
Set.of("one", "two", "three"),
Set.of("one", "two", "three"),
Set.of("one", "two", "three"));

checkUnion("union of an empty set and something is something",
new String[] {"four", "five", "six"},
Collections.emptySet(),
Set.of("four", "five", "six"));

checkUnion("union of something and an empty set and is somethine",
new String[] {"four", "five", "six"},
Set.of("four", "five", "six"),
Collections.emptySet());

checkUnion("union of 2 empty sets is empty",
new String[] {},
Collections.emptySet(),
Collections.emptySet());

}

@SafeVarargs
public static <T> void checkUnion(String reason, T[] expected, Set<T>... unions) {
assertThat(reason, Utils.unionOf(unions), containsInAnyOrder(expected));
}

}

0 comments on commit b4d46b4

Please sign in to comment.